Deploying a flask application on AWS EC2 instance (free tier) for Pose-Estimation

Yogesh Maan
4 min readSep 6, 2021

--

In this blog, we’ll be learning about deploying a sample flask web-app on the EC2 instance of AWS.

Get source code for flask web-app here.

1. Creating and Activating a new AWS account

Please refer to this official step by step guide link.

2. Launching an EC2 instance

STEP 1: Open AWS Management Console & select a zone from one of the available zones according to your location. I live in India so I will be choosing Asia Pacific (Mumbai).

Select EC2 from -> All services -> Compute -> EC2
Select EC2 from [ All services -> Compute -> EC2 ]

STEP 2: After being redirected to EC2 dashboard -> Click on “Launch instance” to launch an EC2 instance.

STEP 3: Setting up an Instance according to our app requirements:
a) Choose AMI — select Ubuntu Server 20.04 LTS (HVM), SSD Volume Type (free tier eligible)

b) Choose Instance type

c) Configure Instance — you can change settings based on your requirements. However, we’ll keep these settings as default.
d) Add storage — default 8 GB would be sufficient for our Pose-Estimation project. You can extend the storage as per your requirement, as free tier eligible customers can get up to 30 GB of EBS General Purpose (SSD) or Magnetic storage. Learn more about free usage tier eligibility and usage restrictions.
e) Review and launch — click on the “Review and Launch” button on bottom right of the screen.

Note: We’ll be configuring the security groups later.

3. Downloading the key pair.

  • After clicking on the launch button — a dialog box(as shown below) will appear.
  • Select “Create a new key pair” from the drop down menu, give a name to the key pair .pem file and download the file on your local machine.

4. Connect to your Linux instance using an SSH client

  • Go to EC2 dashboard-> Instances(running)-> select the created instance.
  • Click on “Connect” button. A dialog box (as shown below) will appear.
  • Open your local machine’s terminal and run the following commands —
navigate to the directory of your .pem file
to ensure that our key is not publicly viewable
Tip : copy and run the command from “Connect to instance” dialog box
After successful connection you will have access to the ec2 instance’s linux server
  • Run the following commands on the cloud server —
sudo apt-get update
python3 -V #ensure that python version is 3.8 or above
sudo apt install python3-pip
sudo apt-get install nginx
  • Clone git repository —
sudo apt install git
git clone https://github.com/YogeshMaan/PoseEstimation.git
  • Install the required dependencies — Navigate to PoseEstimation/ directory
cd PoseEstimation
pip3 install -r requirements.txt

5. Configuring Nginx

  • Run the following commands — to navigate to the config files of nginx.
cd
cd /etc/nginx/sites-enabled/
sudo vim flaskapp
  • Below is a very basic configuration for nginx configuration file. Paste the below block in the config file.
server {
listen 80;
server_name <your EC2 instance's public ip4 address>;

location / {
proxy_pass http://127.0.0.1:8000;
}

}
  • Save the above file and restart the nginx service.

sudo service nginx restart

  • Now, navigate to the PoseEstimation directory and run the gunicorn.
cd
cd PoseEstimation
gunicorn app:app

6. Configuring Security groups

  • Navigate to the security group of your running instance —
  • Edit inbound Rules —
  • Add rule & save rule —

7. Run your flask app

  • Open url http://<your Public ip4 address>:80

Hope you found this helpful!

--

--