Chapter 73 – Ultimate Guide to Deploy Flask App on AWS EC2

This piece you don’t want to miss out notably because it’s all about the ultimate, juicy, and creamy guide to deploying your Flask app on AWS for free. By the end of this piece, you can have one more resource to consider to leverage, and the most important thing is going to be free.

This piece you don’t want to miss out notably because it’s all about the ultimate, juicy, and creamy guide to deploying your Flask app on AWS for free. By the end of this piece, you can have one more resource to consider to leverage, and the most important thing is going to be free.

Table of Contents: Ultimate Guide to Deploy Flask App on AWS EC2

Create AWS EC2 Free Tier Account Using Ubuntu 22.4 LTS

As one of the best cloud eco-system worldwide, it’s surprisingly easy to sign up for a free AWS account by using email and adding one payment card. It’s just like as easy as eating straight up a piece of Miyazaki beef. After this, you just need to search EC2 and create an instance. More details are as follows and here we go.

  • Create a new Instance
  • Select Ubuntu 22.04 LTS
  • Go to instance type and you just can see lots of options. And I think just select t2.micro if you are going to get the free tier
  • Tick the checkbox of network setting including these options: SSH, HTTPS, and HTTP traffic
  • Create a key pair for connecting with your instance

Connect with SSH on Ubuntu

That’s. We have created an EC2 instance, in which the steps are thin, clear, and light. No difficulties! Then, it’s going to connect with your instance using your local machine immediately. As usual, I would take Mac OS as an example:

  • Download the key perm from the instance connect page
  • Save the key perm file to the path you’re going to use and connect with AWS EC2
  • Open the terminal
  • cd to the path
  • $ chmod 400 <your key name>.pem
  • $ ssh -i <your key name>.pem ubuntu@<Public DNS of your EC2>

Both commands can be copied from the instance connect page. Just go and explore man! You might find more updated features than this article. AWS is just like a piece fattier and oilier of a tube compared to competitors.

Add a Flask App on EC2

Now you are in. Now it’s just a regular common way to set up Flask and deploy flask Here you go:

  • Install Python virtualenv

$ sudo apt-get update

$ sudo apt-get install python3-venv

  • Activate the new virtual environment in a new directory

// Create directory

$ mkdir lovely

$ cd lovely

// Create the virtual environment

$ python3 -m venv venv

// Activate the virtual environment

$ source venv/bin/activate

// Install Flask

$ pip install Flask

  • Create a straight and simple Flask app script

$ sudo nano

// Add this to

from flask import Flask

app = Flask(__name__)


def lovely():

return 'Lovely!!!'

if __name__ == "__main__":

  • Test if the script is working or not just running python

Activate Gunicorn WSGI server to run Flask App

Running Flask in fact is running Werkzeugh’s development WSGI server, which forward requests from a web server.

Since Werkezeug is only for development, we have to use Gunicorn, which is a production-ready WSGI server-to-server Flask application. Here are the steps as follows and here we go!

  • Install Gunicorn using — $ pip install gunicorn
  • Run Gunicorn — $ gunicorn -b app:app .
  • Gunicorn is running (Ctrl + C to exit gunicorn)!

Update system settings with Gunicorn

Systemd is a boot manager for Linux. We are using it to restart gunicorn if the EC2 restarts or reboots for some reason.

We create a <projectname>.service file in the /etc/systemd/system folder, and specify what would happen to gunicorn when the system reboots.

We will be adding 3 parts to systemd Unit file — Unit, Service, Install

  • With that said, create a unit file in the /etc/systemd/system directory

$ sudo nano /etc/systemd/system/lovely.service

  • Then add this to the file.


Description=Gunicorn instance for a simple lovely app





ExecStart=/home/ubuntu/lovely/venv/bin/gunicorn -b localhost:8000 app:app



  • Then enable the service:

$ sudo systemctl daemon-reload

$ sudo systemctl start lovely

$ sudo systemctl enable lovely

  • Check if the app is running with — $ curl localhost:8000

Run the Nginx web server to accept route requests to Gunicorn

Finally, we set up Nginx as a reverse proxy to accept the requests from the user and route it to gunicorn. 

  • Install Nginx — $ sudo apt-get nginx
  • Start the Nginx service and go to the Public IP address of your EC2 on the browser to see the default nginx landing page

$ sudo systemctl start nginx

$ sudo systemctl enable nginx

  • Edit the default file in the sites-available folder

$ sudo nano /etc/nginx/sites-available/default

  • Add the following code at the top of the file

upstream lovely {



  • Add a proxy_pass to flaskhelloworld atlocation /

# Some code above

location / {

   proxy_pass http://lovely;


  • Restart Nginx — $ sudo systemctl restart nginx

Connect domain with EC2 Instance without using Route53

Tastes delicate and delicious right? Basically your app is already up online. However, things are not yet to go because we need to connect it with your domain.

Route 53 is one of the most popular solutions mentioned out there, but it’s not free which is opposite to our theme. In fact, it’s going to be free which is possible. Here is the way to go

  • Go to the instance page and copy the public IPV4 address
  • Add two A records in your DNS using the same IPV4 address for respectively @host and * host
  • Sudo nano the Nginx file you created earlier above and replace the server_name with your domain. Please be sure to include the top-level domain type, such as www.

And it’s done! So amazingly delicious

Set up SSL Certificate

Selecting an SSL ceritificate provider kind of blows you away as there are so many options out there no matter if it’s free or not. This piece I recommend is to use a free Alibabacloud SSL certificate to add to your app. What you are waiting for? Let’s go!

  • Please note that this approach needs you to sign up for the domain in the Alibaba cloud
  • If so, go to the free digital certificate and add your domain to apply for the free SSL certificate.
  • Once finished, download the set of SSL certificates which includes respectively key perm and body perm.
  • Go to the AWS certificate manager to import the SSL certificate you downloaded just now. Please be sure to import based on the required format. There is an instruction there. No worries.
  • Next, we need to find a way to connect the SSL to the instance. That is the load balancer. It’s so free!! Just like tasting buttery, rich, and fresh yellow tail Sashimi. In the load balancer, we need to create two listeners. One is for https 443 and the other is for http 80.
  • Last but not least, go back to the Nginx file by connecting with your instance. Then replace some settings with the permanent redirect and SSL certificate. Below is the code sample to serve you.

server {

    listen 80;


    rewrite ^(.*)$ https://$server_name$1 permanent;


server {

    listen       443 ssl;

    server_name your domain;

    #  ssl on;

    ssl_certificate /etc/nginx/ssl/your digital certificate body file name.pem;

    ssl_certificate_key /etc/nginx/ssl/your digital certificate key file.key;

    ssl_session_timeout 5m;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;

    ssl_prefer_server_ciphers on;

    location / {

        root /data/web/;

        index index.jsp index.html index.htm;


Full Scripts & Commands of Flask App Deployment on AWS EC2

If you are interested in Chapter 73 – Ultimate Guide to Deploy Flask App on AWS EC2, please subscribe to our newsletter by adding the message ‘Chapter 73 + AWS EC2’. We would send you the script immediately to your mailbox.

I hope you enjoy reading Chapter 73 – Ultimate Guide to Deploy Flask App on AWS EC2. If you did, please support us by doing one of the things listed below, because it always helps out our channel.


Q1: What is AWS EC2?

A: AWS EC2 is a web service that provides resizable compute capacity in the cloud. It allows you to quickly scale computing resources to meet your application demands.

Q2: How can I launch an EC2 instance?

A: To launch an EC2 instance, you need to first select an Amazon Machine Image (AMI), choose an instance type, configure your instance details, add storage, configure security groups, and launch the instance.

Q3: What is an Amazon Machine Image (AMI)?

A: An Amazon Machine Image (AMI) is a pre-configured template that contains the operating system, software, and configurations required to launch an EC2 instance.

Q4: What is an instance type?

A: An instance type determines the hardware of the host computer used for your instance. It defines the amount of memory, the number of virtual CPUs, and the network performance of your instance.

Q5: How can I manage the security of my EC2 instances?

A: You can manage the security of your EC2 instances by using security groups, which act as virtual firewalls to control inbound and outbound traffic.

Q6: What is Elastic IP?

A: Elastic IP is a static IPv4 address that you can allocate to your AWS account. You can associate an Elastic IP with your EC2 instance to ensure that it remains the same even if the instance is stopped or terminated.

Q7: How can I scale my EC2 instances?

A: You can scale your EC2 instances horizontally by adding or removing instances from an Auto Scaling group, or vertically by changing the instance type to one with more or less resources.

Q8: What is Amazon EBS?

A: Amazon Elastic Block Store (EBS) provides block-level storage volumes for use with EC2 instances. It allows you to create, attach, and detach persistent storage to your instances.

Q9: Can I use my own Windows Server license on EC2?

A: Yes, you can bring your own Windows Server license and use it on EC2. This is known as licensing mobility and allows you to leverage your existing licenses in the cloud.

Q10: What is the pricing model for EC2 instances?

A: EC2 instances are charged on a pay-as-you-go basis, where you only pay for the resources you use. The pricing varies based on the instance type, region, and usage.