flowchart LR A[1. VS Code] --> B(2. Github) B --> C{3. AWS EC2}
Deploy a Streamlit app
Jakob Johannesson
June 15, 2022
In the previous blogpost I showed you how to create a Streamlit application and now it is ready to be digested by the team. We will do this by deploying it to Github and AWS EC2.
To get a grasp of what the architecture looks like it is the following.
flowchart LR A[1. VS Code] --> B(2. Github) B --> C{3. AWS EC2}
First step is to create a new github repo for the Streamlit app. It does not matter if it is public or private. I will make my repo for this example public.
Connect your VS Code to the repo and push your application. You should see your code in the repo.
If you have a public repo with your Streamlit app and want to keep it public, then it is not necessary to create a PAT. Skip to the next step.
Github personal access token (PAT) is used to access your Github account from AWS EC2. In order to do this you need to do the following steps:
Go to AWS, login to your account. Go to AWS EC2. Here we have lots of options: Size of the machine, security protocols and network settings.
AWS EC2 does have options for a free tier. The conditions changes, but you should see some option that does not cost anything for a year or so. This means you can do this without having it costing you anything, at least for the testing.
Give your instance a name, make sure you can understand what the machine does. Choose a machine that fits your needs or simply the cheapest one if you are just testing. It is possible to change this later as well if you need to scale up.
You will need a key pair to access your machine. Press create new pair, add a simple name and go with the defaults.
Press create key pair and it will download instantly.
You should change the “Allow SSH traffic from” to only include your own ip address. Click on the dropdown and here you can see your own ip address. This is important since anyone will be allowed to access your machine otherwise.
After the network settings are finished, press “Launch instance”. It should send you back to the overview of all the instances you have.
The default group is not allowing connections to the Streamlit port, therefore we need to created a new security group and assign it to the EC2 machine.
First step is to go to AWS, search for security groups. Go to it.
Go to “Create security group”, the orange button on the top-left corner. Give it name, a small description and create two inbound rules:
Leave outbound ports to default (all traffic to anyone).
Return to EC2, go to your machine you just created. Go to Actions –> Security groups –> Change Security groups –> Add the security group you created in the previous step and save changes.
Next step is to connect to the EC2 machine. Go to the overview of instances in AWS EC2. Go into the instance you just created and look in the upper right corner for a button called “Connect”. Go to “SSH client” and you should see something like the following:
1. Open an SSH client.
2. Locate your private key file. The key used to launch this instance is streamlitpair.pem
3. Run this command, if necessary, to ensure your key is not publicly viewable.
chmod 400 streamlitpair.pem
3. Connect to your instance using its Public DNS:
{INSTANCE}.eu-north-1.compute.amazonaws.com
Example:
ssh -i "streamlitpair.pem" ec2-user@{INSTANCE}.eu-north-1.compute.amazonaws.com
We can now use this information to connect to the AWS EC2 machine.
Open up a CLI (terminal/cmd), find the path to the key pair we downloaded earlier. I have my key in the downloads folder. You will need to find your path the pem file and also change the AWS instance link. In order to connect we will run something like the following:
chmod 400 will protect your key so it is not publicly viewable. If you do not run it, you might run into issues with connecting. Next chunk is the connection.
When connecting, you might get a warning for fingerprint for the host, informing you it is not a known host. Since you created it, you can accept it.
Congratulations, you should now be inside the AWS EC2 instance! Save this command as you will need it for later. Now that we are inside the linux machine we can fetch the Streamlit app from github. If you are not familiar with linux, I can recommend using a few commands to navigate around. Skip this if you feel comfortable with Linux.
Here are some generic Linux commands that will help you in the Linux jungle.
Now we are inside the EC2 instance. In order to fetch the Streamlit app, we first have to install git on the instance. Do it by running the following:
Accept the download, it should be a 30-50 mb download.
The first time we connect to the repo, we have to clone it. After the first time we can use “git pull” instead.
Pro tip: If your Repo is public you can pull it without the PAT.
Congratulations, you now have the Streamlit app on the machine.
Check that the folder with the app is there as expected and change directory to it.
You should see the following.
Go to the external URL, in this case: http://13.48.47.0:8501/
Boom there it is!
One issue is that the app will close if you shut down the terminal. The solution to this is to run TMUX, install it by:
Boom boom! Great, now we can shut down the terminal and it will run until you shut it down.
If you want to reconnect to the tmux session you can do so by:
If you want to push a new version of the app from VS Code, then push it to github. Next, connect to your AWS EC2 machine using ssh again. Change directory to the Streamlit app, then run a git pull:
If your repo for some reason is on different versions, you can reset it.
Thanks for reading my post about how to deploy a Streamlit app. I hope it will deliver value to your data science workflow.
If you enjoyed this guide, please reach out and tell me.
In the future we will do another blogpost which we dive into a CI/CD workflow to make the process of application development a lot smoother and fun! This will be achieved through implementing Github actions to the repo, allowing you to keep the same version across systems.
This means you do not need to login to your AWS EC2 each time you want to push a new version of the Streamlit app. That is just boring anyway, so we are happy to get rid of the boring parts of development.
Thanks for the inspiration from this guide. It might give you a different point of view.