Build Interactive Data Apps: Streamlit + Snowflake on AWS (Part 2) 🚀☁️

Data Engineering Blogpost
6 min readFeb 24, 2024

--

Welcome back to the second installment of our thrilling project journey! 🌐. In Part 1, we created a powerful Streamlit web app that analyzes online retail data from Snowflake. In this blog, we’ll guide you through the step-by-step process of deploying your powerful Streamlit web app to an AWS EC2 instance.By the end of this blog, your interactive web app will be up and running in the cloud.

Let’s dive into the exciting world of cloud deployment! 💻🌍

Resources Section:

Step 1: - Launch an EC2 instance on AWS ☁️.

Launching an EC2 instance is a fundamental step in deploying your Streamlit application on AWS. This process involves creating a virtual server that will host your application.

The video tutorial shows how to launch an EC2 instance on AWS

Key Steps:

  1. Navigate to EC2 service.
  2. Click on Launch instance for creating a new instance: - Click the Launch Instances button to start the creation process.
  3. Name the instance.
  4. Create a new key pair for security: -
  • Generate a new key pair, acting as your digital key to unlock access.
  • Download and securely store the key pair file (.pem). ⚠️ Keep it confidential!

5. Allow necessary protocols like HTTP and HTTPS.

6. Launch the EC2 instance: - Review your choices and click Launch Instances.

Next, learn how to connect to the instance from your local computer.

Step 2: - Connect to AWS EC2 Console in your Local Computer 💻.

In this step, you’ll learn how to connect your AWS EC2 instance to your local computer. The connection process involves using Git Bash and SSH to establish a secure connection.

The video tutorial above, you’ll learn how to Connect your EC2 instance to your local computer.

Key Steps:

  1. Open Git Bash terminal
  2. Select your instance and start it: - Go to aws console and select instance.
  3. Connect to the instance: - With your EC2 instance selected, click the Connect button at the top of the EC2 Dashboard.
  4. Copy the provided SSH command: - Copy the provided SSH command from the dialog. The command will look something like this:
ssh -i "your-key-pair.pem" ec2-user@your-ec2-instance-ip

6. Paste the SSH command into Git Bash.

  • Return to your Git Bash terminal.
  • Paste the copied SSH command into Git Bash and press Enter.

7. Voilà! You’re now connected to the EC2 console.

Next, learn how to Create a Python Environment in EC2 console.

Step 3: - Creating a Python Environment 🐍.

In this step, you’ll create a Python environment on your AWS EC2 instance.

The video tutorial above, you’ll learn how to Create a Python Environment

Key steps:

  1. Open Git Bash terminal connected to your EC2 instance.
  2. Run the following command to create a Python virtual environment named myenv:
python3 -m venv myenv

3. Activate the Python environment

source myenv/bin/activate

4. Successfully created the Python environment.

Step 4: — Uploading Files to the EC2 Environment 📤.

In this step, you’ll be using the Secure Copy (SCP) command to transfer files from your local machine to your EC2 instance. The primary files you’ll be transferring are streamlit_app1.py (your Streamlit application) and requirements.txt (the file containing dependencies).

The video tutorial above shows how to smoothly transfer files to your EC2 environment.

Key Steps:

  1. Navigate to Command Prompt: - Open your command prompt or terminal on your local machine.

2. Upload Streamlit App to EC2 using SCP:

Execute the following SCP command to transfer your Streamlit application file (streamlit_app1.py) to your EC2 instance

scp streamlit_app1.py <ec2-user>@<your-ec2-instance-ip>:/path/to/destination

3. Upload requirements.txt to EC2 using SCP:

Execute the following SCP command to transfer your requirements.txt(requirements.txt) to your EC2 instance

scp requirements.txt <ec2-user>@<your-ec2-instance-ip>:/path/to/destination

4. Successfully Uploading Files to the EC2 Environment

▪️ SCP(Secure Copy command) Command Breakdown:

scp <your app name> <ec2-user>@<your-ec2-instance-ip>:/path/to/destination
  • <your app name>: The file to upload
  • <ec2-user@your-ec2-instance-ip>: Replace with your EC2 instance’s username and IP address .
  • /path/to/destination: Replace with the destination path on the EC2 instance (obtained using pwd on EC2 CLI)

Next Up: Installing Streamlit and Dependencies

Step 5: - Install Streamlit and Dependencies 📦.

In this step, you’ll be installing Streamlit and the required dependencies for your application by executing the contents of the requirements.txt file that you previously uploaded to your EC2 instance.

The video tutorial above shows How to Install python dependency in ec2.

Keys Steps:

  1. Launch Git Bash with EC2 Home Activated :- Ensure that your Git Bash terminal is connected to your EC2 instance. If you’re unsure, review the second step for establishing this connection.
  2. Installing all the dependency stored in requirements.txt: - Run the following command to install the dependencies:
pip install -r requirements.txt

3. Dependency installed successfully.

Next Up: Edit Inbound Rules and Test the App

Step 6: - Edit Inbound Rules and Test the App 🧪

In this step, you’ll configure the inbound rules on your EC2 instance to allow traffic on the specified port i.e. 8501 and test the functionality of your Streamlit application.

The video tutorial shows how to Edit Inbound Rules and Test the App

Keys Steps:

  1. Testing the webapp.
  2. Edit inbound rule to allow port 8501:-
  • Go to your AWS Management Console.
  • Navigate to the EC2 Dashboard.
  • Select Your EC2 Instance
  • Select Security Groups from the navigation pane.
  • Find and select the security group associated with your EC2 instance.
  • Click on the Inbound Rules tab.
  • Add a new rule allowing inbound traffic on port 8501 (or the port your Streamlit app is configured to run on). Set the source to “0.0.0.0/0” for all incoming traffic.
  • Save💾 the changes.

3. Congratulations on successfully running your web app!

Next Up: Configures the application for deployment ⚙️ and launches it🛰️.

Step 7: - Configures the application for deployment ⚙️ and launches it🛰️.

In this step, you’ll configure your Streamlit application for deployment and ensuring continuous operation.

The video tutorial shows how to ensure continuous app operation and successfully deploy the application.

Key Steps

  1. Keeping Your Website Alive with nohup:-Use the nohup command to keep your Streamlit application running even after you log out of your EC2 instance.
nohup streamlit run <streamlit_python_file.py> &

2. Testing your website.

3. Terminating the terminal: You can close the terminal using the ctrl+c.

4. Testing your website: -After closing the terminal, verify that your Streamlit application is still accessible and running as expected.

Your web application is now deployed and ready for the world. Congratulations on a successful deployment 🎉!

Thanks for Reading📖💙

if you like my work the way to support me ….

  1. The best way is to support me is by following me on medium.
  2. Feel free to give claps so I know how helpful this post was to you.

--

--

Data Engineering Blogpost

Data engineering enthusiast ️ Building pipelines, wrangling data, & unlocking insights. AWS, Snowflake, Python . Join me on the journey!