How I Made a Website with Python and AWS | Towards Data Science

As coders and programmers, we must be able to present the projects and applications that we’ve created to various individuals without having a laptop on us at all times. In order to do so, we can host our applications on our own websites to be viewed by anyone with access to the internet. The first step in creating our own websites is to find a website hosting service such as Amazon Web Services (AWS). These hosting services will make sure our websites are available at anytime from any computer with internet access.
The look of our website will be entirely dependent on the code we utilize. In our case, we implemented the Streamlit Python library to build a user-interface for a Data Science based application. To see how we created our web application with Streamlit, check out the article below:
towardsdatascience.com
This article delves into the development and design of a web application that we created using Streamlit and its respective functions.
Sign up for a Medium Membership here to gain unlimited access and support content like mine! With your support I earn a small portion of the membership fee. Thanks!
Let’s begin the process of creating a website. First, we will need to find a website hosting service that we find appropriate for us. There are numerous web hosting services out there but we will be utilizing AWS for now.
To start, you must first create an AWS account. This is a separate account different from a normal Amazon account used for shopping or selling. There are free tier accounts, which is the one we will be using. We will need this account in order to create a server to host our website.
Once an account has been created, we can proceed to creating an AWS EC2 Instance. An AWS EC2 Instance or Amazon Elastic Compute Cloud is used to launch an Instance, which is a virtual server in the cloud.
To create an Instance, from the homepage, navigate to the My Account tab and click on the AWS Management Console.
AWS Mangement Console
Once you find yourself in the Management Console, navigate to the All Services section, and click on EC2.
EC2 Dashboard
Here you will find many different resources and options. However, we will be focusing on the Launch Instance section. Click the Launch Instance button to proceed.
Next, we will be presented with a page to customize that Instance. The first step here is to choose an Amazon Machine Image (AMI).
Here we select the Amazon Linux AMI 2018.03.0 (HVM). We selected this one because it already includes numerous programming languages (i.e. Python) and it is free tier eligible.
Choosing an Instance Type
Once we have selected the appropriate AMI, we move on to the next step, choosing the right instance type. Select the free tier eligible option: t2.micro.
Configure Security Group
Proceed past the next steps all the way to step 6. There we will need to configure the security group. Click Add Rule.
After clicking Add Rule, you will see a new row added. We need to configure this new row for our Streamlit app.
Under Type, select Custom TCP.
Under Port Range, enter 8501.
Under Source, select Anywhere.
Finish it up by clicking Review and Launch on the bottom right corner.
Launch the Instance
After clicking Review and Launch, you will see a Review page. Ensure that everything is correct, then click Launch on the bottom right.
Once Launch is clicked, the following pop-up appears.
Here you will need to create a new key pair by navigating to Choose an existing key pair and select Create a new key pair.
Enter any name you wish for the Key pair name. Then, click Download Key Pair. You will need this .pem file in order to access our Instance from the command terminal. (Move the .pem file into an .ssh folder in a secure location, which you can create if you don’t have one).
After you have downloaded the private key file (.pem), you can click Launch Instances. This will bring you to the next page displaying the Launch Status.
Now that our EC2 Instance is up and running, we need to find the Public DNS address to access the Instance in the command terminal. To do so, navigate to the Management Console, click on EC2, click on Instances, then select the Instance we just created. You will find the Public DNS (IPv4) address on the lower right. Copy the address because you will need it for later.
If the steps were followed, you have successfully launched and created an EC2 Instance using AWS. Next, you will need to SSH into the Instance in order to establish the website.
SSH or Secure Shell is a network protocol that will allow us to operate our EC2 instance through the terminal using a command line interface. If you have a Mac or Linux, you will then already have SSH. Windows users will probably need to use PuTTY.
For the following steps, we will be using the MacOS command terminal to access our EC2 instance. To SSH into our Instance, we must first open the command terminal, then navigate (CD) into the same folder that contains the .pem file we downloaded before. (Make sure the .pem file is in a .ssh folder, which is stored in a secure location on your computer).
Once we are in that folder, run the following command:
$ ssh -i key_pair.pem ec2-user@your_public_dns_address
Use the .pem file you created and downloaded.
Add “ec2-user@” to the beginning of your PublicDNS (IPv4) address.
After entering this command, you should see the following in your command terminal:
This means we are successfully in our EC2 Instance!
Now we will need to install all the packages and files we need for our Streamlit app. Even though our AMI contains some required packages, we still need some more. While we are still in our command terminal of the EC2 instance, run the following commands:
The Python included in the AMI is Python 2 not Python 3, which is what our Streamlit app runs on. So we will need to install Python 3.
$ sudo yum install python36
We will also need to install Git in order to download the repository that contains our Streamlit app.
$ sudo yum install git
We will be cloning the Github repository that contains our Streamlit web application:
$ git clone your_github_repo
After we have successfully cloned the Github repo into our EC2 Instance, navigate (CD) into the repo folder.
Navigate into the folder containing a requirements.txt file. Once we are in the folder containing our requirements.txt file, we can install all the libraries necessary to run our Streamlit app.
$ python36 -m pip install -r requirements.txt --user
This will install all the libraries specified in the requirements.txt file. The --user command will make sure our libraries are installed with the correct permissions.
With everything installed, we can finally run our app. In the folder that contains the .py file, enter the Streamlit command:
$ streamlit run your_app.py
Now our web app is available online by using the External URL to view in our preferred browser. However, the link will no longer be available when we log out of our EC2 instance, which closes the current session.
In order for the URL to be active even when we log out, we will need to install one more thing: TMUX.
TMUX gives us the ability to keep a session running even when we logout. But first, close out of the current Streamlit session with ctrl+C. Then, navigate back to the beginning of the Instance.
To install, enter the following command while still in the EC2 instance:
$ sudo yum install tmux
Then, create a new session with:
$ tmux new -s name_of_session
This starts a new session with any name that you wish from which we can run our app. From there just do as done before and move to the folder with your app.py file.
Once there, you can run Streamlit again:
$ streamlit run your_app.py
Now you have the Streamlit app running inside the TMUX session. To exit out of the tmux session but still keep it running you’ll have to detach the tmux session. You can do this by pressing: ctrl+B, then D.
Once detached you can check on the status of the tmux session by running:
$ tmux ls
You should see that you have one tmux session still live. Now you can logout from the EC2 instance without having to worry about the URL going down. Just press: ctrl+D.
Success! We were able to get our Streamlit app live by using an AWS EC2 Instance. The specific web application we have up is our Dating App that utilizes Data Science and Machine Learning:
towardsdatascience.com
The URL for the website may not be pretty but that can be solved by creating our own custom domain name for our instance. However, that process requires some more steps and is more complicated than expected. It also may cost some money to register a domain name.
At the end of this we hope you were able to successfully create a website that will host your own web application by utilizing Streamlit and an AWS EC2 Instance. Feel free to check out the other articles that show the development of our web app with Streamlit or the development of the dating application.
Follow me on Twitter: @_Marco_Santos_