How to deploy a Flask application on AWS EC2 instance
This is a tutorial to help you host your python flask application onto the EC2 instance. I will be explaining how to create an EC2 instance and also a sample flask program!
What is Amazon EC2?
Amazon Elastic Compute Cloud (Amazon EC2) provides scalable computing capacity in the Amazon Web Services (AWS) Cloud. Using Amazon EC2 eliminates your need to invest in hardware upfront, so you can develop and deploy applications faster. You can use Amazon EC2 to launch as many or as few virtual servers as you need, configure security and networking, and manage storage. Amazon EC2 enables you to scale up or down to handle changes in requirements or spikes in popularity, reducing your need to forecast traffic. For more information please refer to this page.
Deployment steps
Step 1: Create an AWS account using this link.
Step 2:
Log in to your aws account and search for EC2 service
Click on ‘Launch Instance’ once you are in the EC2 dashboard
Step 3:
Choose an AMI from the options available. Here I have chosen a free tier Ubuntu AMI. Standard rates may apply if you choose a non-free tier AMI
An Amazon Machine Image (AMI) is a template that contains a software configuration (for example, an operating system, an application server, and applications). From an AMI, you launch an instance, which is a copy of the AMI running as a virtual server in the cloud. You can launch multiple instances of an AMI, as shown in the following figure.
Step 4: Choose an instance type
When you launch an instance, the instance type that you specify determines the hardware of the host computer used for your instance. Each instance type offers different compute, memory, and storage capabilities, and is grouped in an instance family based on these capabilities. Select an instance type based on the requirements of the application or software that you plan to run on your instance.
Step 5:
You may click on ‘Review and Launch’ or click on ‘Next’ to choose the next configurations relating to Storage, Tags, and Security Groups.
Step 6:
Once the above configurations are selected click on ‘Launch’. Select ‘create a new key pair’ and save the downloaded ‘.pem’ file in your project folder.
Step 7: Click on ‘Launch Instances’ and wait till all the checks are completed
Step 8:
Go to ‘Security Groups’ (from the left pane) and create a new security group named ‘FullAccess’ with the following inbound rule
This will allow traffic from all the sources.
Step 9:
Right-click on your instance and select ‘Change Security Groups’
Add the newly created ‘FullAccess’ security group and click Save
Step 10:
Click on ‘Connect’ and then copy
ssh -i “key_name.pem” instance_name@ec2…aws.com
Step 11: Open your cmd and navigate to the project folder where you have pasted the ‘.pem’ file
Step 12: Execute the following commands one by one in the ubuntu instance
sudo apt-get update
sudo apt-get install python3
sudo apt-get install python3-pip
sudo pip3 install flask
sudo apt-get install nginx
sudo apt-get install gunicorn
sudo apt-get install git
Step 13:
Clone your GIT repository in which you have your project code
git clone git_repo_url
Step 14:
Install the project dependencies from the requirements file from your repository
pip3 install -r requirements.txt
The requirements file can be created in the local machine's command prompt using pip freeze > requirements.txt
Step 15:
Run the flask project
python3 NAME_of_webapp.py
Step 16:
Paste the public IPv4 DNS along with port 8080 in the browser
Public_DNS_Name:8080/
Sample deployed project: