Deploy Python Fast API using AWS EC2 Instance

Step by step approach (including screenshots & code) on how to create an AWS EC2 instance and deploy your code for production purpose
What is the EC2 Instance?
Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, and resizable compute capacity in the cloud. To make web-scale cloud computing easier for developers. Amazon EC2’s simple web service interface allows you to obtain and configure capacity with minimal friction.
Index
1. How to configure an AWS account
2. Security Groups
3. SSH into the EC2 instance
4. Install different dependencies
5. Copy the Python Code to EC2
6. Run the Application on the server
Configure an AWS account
Creating a new account on AWS is quite easy.
→ Go to “https://signin.aws.amazon.com/.”
→ “Create a new AWS account.”
→ Enter the necessary information like first name, email address, password and you are done.
You get 12 Months of Free Tier Access to AWS Accounts including EC2 instance, Lambda, S3 buckets and Sagemaker. For more info on limits, you can check the following: aws.amazon.com/free.
Create an EC2 Instance
After login, proceed to the console by finding the console section or enter https://console.aws.amazon.com/ in the URL
→ Under “Find the service” enter “EC2.”
→ “Launch a new instance.”
→ Choose “Ubuntu Server 18.04 LTS (HVM), SSD Volume Type” under “Free Tier only.”
→ Instance Type is “t2.micro”. Provides one vCPU support and 1gb of RAM, which is enough for small to medium algorithms.
→ You can keep the rest of the features as default and launch the EC2 instance. It will ask you to create a “Private” & “Public” key pair. The Pair helps you to connect to the EC2 instance from your laptop. To create a new pair and save it securely somewhere. (We will need it shortly)
Voila! You have created your first EC2 instance. Congratulations!
Security Groups
Only port 22 is open for both inbound and outbound traffic. That is the reason we can SSH into the EC2 instance. (More on that in a while). However, we would want our code to run through a port 80 on HTTP type.
To make this change, navigate to Security Groups in the console section and add the following in both inbound rules
→ Port = 80
→ Type = HTTP
Now code can be accessed by port 80
Now that we have created the EC2 instance, you can go to the EC2 console and check if it is running or not. There are a couple of things that will be needed from this screen.
1. Instance State — tells us whether the instance is running or not. (When you are not working on anything, remember to stop it. Otherwise, it will keep consuming hours in free tier)
2. Public DNS IPv4
3. IPv4 Public IP
SSH into the EC2 instance
Next is SSH into the EC2 instance. However, first — what is SSH?
The SSH protocol (also referred to as Secure Shell) is a method for secure remote login from one computer to another. It provides several alternative options for strong authentication, and it protects the communications security and integrity with secure encryption. Here SSH will be used to connect our local pc or laptop to Linux based EC2 instance
Steps:
→ Copy the IPv4 Public IP
→ Open the terminal on MAC or Windows
→ Head to the directory where you have the “Key Pair.”
→ Change the permission of the pem file so that it can be “read.”
→ ssh -i <KeyPairFile.pem> ubuntu@<IPv4 Public IP>
Example: If the key-pair name is “Pair.pem”
chmod 400 Pair.pem
ssh -i Pair.pem ubuntu@ 99.999.999.99
You are now inside the Linux terminal of AWS
Install different dependencies
Once inside the Linux terminal — we need to download all the different libraries associated with our Python code.
sudo apt-get update
sudo apt install python3-pip
sudo apt-get install gunicorn
sudo apt update
Download other libraries in a similar fashion
Copy the Python Code to EC2
First, create a new directory in the instance. This is where we will copy our Python script.
mkdir PythonDir
cd PythonDir
Now open a new terminal & enter the following:
scp -i ~/ Pair.pem ~/Desktop/pythoncode.py ubuntu@ec2- 99–999–999–99.compute-1.amazonaws.com:~/PythonDir/
Above code will access EC2 instance using Public DNS and copy the code from your system to the directory in EC2 instance
cd /etc/nginx/sites-enabled/
sudo vim myFastApi
Configure “myFastApi” file.
80 is the port where the web server should connect.
server{
listen 80;
server_name 99.999.999.99; <-your server name or ip
location / {
proxy_pass http://127.0.0.1:8000;
}
}
Restart Nginx terminal to reflect the changes.
sudo service nginx restart
Install Environment
— Run the Application sever
We are using pipenv for our environment, but you can use your preferred environment.
sudo pip3 install pipenv
Go to the app directory and enter the following to start the application server.
Setting the python path for the environment:-
pipenv — python /usr/bin/python3
pipenv shell
pipenv install -r requirements.txt
Finally — Run the Application on the server
Go to the app directory and enter the following to start the application server.
cd PythonDir
gunicorn -w 1 -k uvicorn.workers.UvicornWorker <fileName>:app
Conclusion
Go to your public IP, and you can view the live fast API
The server has started, and we can test the app. Test it on 99.999.999.99/docs