Deploying a Streamlit App on AWS EC2

Thu Sep 28, 2023

10 mins streamlit deployment guide

Streamlit has emerged as a popular framework for data scientists and developers looking to turn data scripts into interactive web applications. AWS EC2, on the other hand, offers scalable computing capacity in the cloud. Combining the two, we can deploy Streamlit apps on AWS, making them accessible to a wide audience. 

In this guide, we'll walk you through the process step by step 

Step 1. Setting Up an EC2 Instance

Before deploying your Streamlit app, you need an AWS EC2 instance. Create one from the AWS Management Console, choosing an appropriate instance type (e.g., t2.micro for testing). Ensure your instance has a security group allowing inbound traffic on port 8501 (Streamlit's default port) and port 22 for SSH.

Step 2. Connecting to the EC2 Instance

Use the following command to establish an SSH connection using any terminal software that supports SSH commands like Git bash:

ssh -i "path_to_your_key.pem" ubuntu@YOUR-EC2-IP-ADDRESS
Make sure to replace "path_to_your_key.pem" with the path to your private key and YOUR-EC2-IP-ADDRESS with your instance's IP address 

Step 3. Preparing the Instance

Update and upgrade the system packages:
sudo apt update && sudo apt upgrade -y
Install Python and Pip:
sudo apt install python3 python3-pip -y
Install Streamlit:
pip3 install streamlit
Step 4. Transferring Files to the EC2 Instance

To move your Streamlit app and associated files to the EC2 instance, you can use WinSCP. It provides a graphical interface, making file transfers between your local machine and the EC2 instance straightforward. Ensure you connect using the same .pem file used for SSH.

Step 5. Installing Dependencies
If your Streamlit app requires specific libraries, list them in a requirements.txt file. Once uploaded, install them on the EC2 instance:
pip3 install -r requirements.txt
Step 6. Running the Streamlit App
Before running the app, ensure the path includes the location of the Streamlit binary:
export PATH=$PATH:/home/ubuntu/.local/bin
Now, launch your app:
streamlit run
Step 7. Running the App in the Background
If you close your terminal, the app will stop. To keep it running, use:
nohup streamlit run > streamlit.log 2>&1 &
This command ensures the app runs in the background, and logs are written to streamlit.log.

Potential Error Points and Solutions
  1. ModuleNotFoundError: Ensure all required libraries are listed in requirements.txt and installed on the EC2 instance.
  2. Streamlit Version Issues: Version mismatches can lead to unexpected behaviors. Ensure the Streamlit version on the EC2 instance matches the one used during development.
  3. Protobuf Errors: Compatibility issues between Streamlit and protobuf can arise. Consider downgrading the protobuf package if faced with such errors.
  4. Networking Issues: Ensure the EC2 security group allows inbound traffic on the necessary ports.
  5. File Path Issues: Ensure all paths (e.g., for data files or models) in your Streamlit app are correct relative to the EC2 instance's directory structure.

Deploying a Streamlit app on AWS EC2 is a straightforward process, allowing you to share your interactive data applications with a broader audience. While the steps provided get you started, remember that for a full-fledged production environment, consider more robust deployment methods, including containerization and orchestration tools.


Launch your GraphyLaunch your Graphy
100K+ creators trust Graphy to teach online
CampusX 2024 Privacy policy Terms of use Contact us Refund policy