Learn how to deploy a Streamlit data application on an AWS EC2 instance, making your interactive dashboard accessible via a public URL.
What it is
Deploying a Streamlit app on EC2 involves running the Streamlit server on a virtual machine in the cloud. Unlike local development where you use localhost, EC2 provides a static IP address and persistent storage, allowing users to access your app from anywhere. The core components are the EC2 instance (the computer), the Security Group (the firewall rules), and the Streamlit runtime environment.
Why it matters
- Accessibility: Stakeholders can view dashboards without installing Python or dependencies locally.
- Persistence: The app remains online 24/7, unlike local instances that stop when your laptop closes.
- Scalability: You can upgrade the EC2 instance type if your data processing requires more CPU or RAM.
- Cost Control: Pay only for the compute time used, with options for free-tier eligible instances.
Syntax or steps
- Launch Instance: Choose an Amazon Linux 2 or Ubuntu AMI in the AWS Console.
- Configure Security: Open port 8501 (default Streamlit) and port 22 (SSH) in the Security Group.
- Connect via SSH: Use your private key to log into the instance terminal.
- Install Dependencies: Update package managers and install Python, pip, and Streamlit.
- Run App: Start Streamlit with specific flags to bind to all network interfaces.
Example
# 1. Connect to EC2 via SSH
ssh -i my-key.pem ec2-user@ec2-xx-xx-xx-xx.compute.amazonaws.com
# 2. Install Python and Streamlit (Amazon Linux example)
sudo yum update -y
sudo yum install python3-pip -y
pip3 install streamlit
# 3. Create a simple app file
echo "import streamlit as st
st.title('Hello EC2')
st.write('This app is live!')" > app.py
# 4. Run Streamlit bound to all interfaces
streamlit run app.py --server.port=8501 --server.address=0.0.0.0
Explanation: The command --server.address=0.0.0.0 is critical; by default, Streamlit binds to 127.0.0.1 (localhost), which blocks external access. Binding to 0.0.0.0 allows traffic from any IP, provided the AWS Security Group permits it. Port 8501 is the standard default but can be changed.
Common mistakes
- Forgetting Security Groups: If the app runs but the browser times out, check that port 8501 is open to
0.0.0.0/0(or your specific IP) in the AWS console. - Localhost Binding: Running
streamlit run app.pywithout--server.address=0.0.0.0will make the app invisible to the internet. - Process Termination: Closing the SSH terminal kills the Streamlit process. Use
nohuporsystemdto keep it running in the background. - Dependency Conflicts: Installing packages globally can break system tools. Always consider using a virtual environment (
venv) for production apps.
When to use it
| Method | Best For | Complexity |
|---|---|---|
| EC2 Deployment | Full control over environment, custom ports, internal company networks. | Medium (Requires Linux/AWS knowledge) |
| Streamlit Cloud | Quick prototypes, public demos, zero-config deployment. | Low (Git push only) |
Use EC2 when you need specific hardware configurations, access to private VPC resources, or strict security compliance. Use Streamlit Cloud for rapid iteration and public sharing.
Practice
Guided Exercise: Launch a t2.micro EC2 instance (free tier eligible). Install Streamlit and run a basic "Hello World" app. Verify you can access it via http://<your-public-ip>:8501.
Challenge: Modify the launch command to run in the background so the app stays alive after you disconnect SSH. Hint: Look up the nohup command syntax.
Quick check
Q: Why must you specify --server.address=0.0.0.0 when deploying Streamlit on EC2?
A: By default, Streamlit listens only on localhost (127.0.0.1). Specifying 0.0.0.0 tells the server to accept connections from any network interface, making it accessible externally through the EC2 public IP.
Summary
Deploying Streamlit on EC2 transforms a local script into a web-accessible service by configuring network bindings and firewall rules. Mastering this workflow gives you full control over your data app's infrastructure, balancing flexibility with the operational overhead of managing a server.