Host a Flask web app on aws EC2

Flask is a python framework that many people learn as their first web framework as it is simple, lightweight, and easy to learn.It has gained popularity over time.

AWS is one of the most popular cloud services available today, providing amazing solutions for many different problems which businesses face.

In this tutorial, I will give steps on how you can deploy your first Flask application to AWS. Lets geddittt!!

First, understand the working of our app

There are 6 steps involved to make your Flask app run on EC2.

1] Creating Ubuntu EC2

2] SSH into Ubuntu

Go to your key location and start cmd there. and past the ssh command to log in to your EC2.

3] Create a Flask App inside EC2

After Logging in, We will create a simple Flask app or you can yours.

sudo su // Changing to root user
apt-get update

apt-get install python3-venv  // installing python virtual environment to our EC2

mkdir helloworld

cd helloworld

python3 -m venv venv  // This will create a virtual environment inside the folder called venv

source venv/bin/activate // This will activate the virtual environment

Now it's time to install Flask framework.

pip install Flask

Now the Flask is installed into our machine.Lets create a simple Flask app

sudo nano app.py

// Add this to app.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == "__main__":
    app.run()

verify it by

python app.py

4] Gunicorn server to serve the Flask app

Gunicorn stands for Green Unicorn. It is a standard server to run python web apps in a production environment. This server will directly interact with the web servers like Nginx or Apache2.

pip install gunicorn
gunicorn -b 0.0.0.0:8000 app:app

(Cntrl + C to exit gunicorn)

5] Using systemd to manage Gnicorn

We will create a file <projectname>.service in directory /etc/systemd/system. This file will help to restart gunicorn if EC2 shuts down/restart for any reason.

sudo nano /etc/systemd/system/helloworld.service

// Add this to file

[Unit]
Description=Gunicorn instance for a simple hello world app
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/helloworld
ExecStart=/home/ubuntu/helloworld/venv/bin/gunicorn -b localhost:8000 app:app
Restart = always

[Install]
WantedBy=multi-user.target

It should look like this:

 

To enable service:

sudo systemctl daemon-reload
sudo systemctl start helloworld
sudo systemctl enable helloworld

 

Check whether the app is running or not:

curl localhost:8000

 

6] Running Nginx server

sudo apt-get nginx
sudo systemctl start nginx
sudo systemctl enable nginx

 

Edit the default file in sites-available folder

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

Add this code to the top of the file

upstream flaskhelloworld {

server 127.0.0.1:8000;

}

It should look like this:

 

Add proxy_pass to flaskhelloworld at location /

location / {

proxy_pass

http://flaskhelloworld;

}

Restart Nginx:

sudo systemctl restart nginx

 

This is the bonus step to get an SSL certificate for your web app. The CertBot provides free SSL certificates. You can head over to the website and follow the instructions accordingly by choosing your server and OS.

 

 

if you are running Ubuntu you execute the following commands to get certified easily.

sudo snap install core

sudo snap refresh core

sudo apt-get remove certbot

sudo snap install --classic certbot

sudo ln -s /snap/bin/certbot /usr/bin/certbot

sudo certbot --nginx

Leave a Comment