Host a flask application on ec2

Published on December 17, 2021
Last updated October 02, 2023

Flask is a lightweight web framework for Python based on the Tornado web server.

The goal of the framework is to make it easy to write web applications without having to write a lot of code.

In this post we will look at how to host a flask application on ec2.

Prepare your flask application for production

I will be using a simple flask application that I have created you can find it in the following link:

github.com/Nikhil1920/sample-flask-app

It is already configured to run on gunicorn.

The server that your flask app runs on when you give the flask run command is not suitable for production use.

So instead we will be using gunicorn to run our application

Install gunicorn with pip install gunicorn

create a requirments.txt file in the root directory of the project and add all your requirements in it.

The requirements file of the sample app I am using looks like this:

flask
gunicorn
psutil

Create a ec2 instance

click here to go to your ec2 console

In the top right corner of the screen click on the Launch instances button to create new instance.

ec2-launch-instance

Select your preferred image I will be using ubuntu 20.04 LTS.

ec2-ubuntu-20.04-LTS

I will be using t2.micro instance type as it is free tier eligible.

After selecting instance type click on review and launch button. If you need to any other configuration changes for your use case like adding more storage you can do so by clicking next and editing the configuration.

Click on Review and launch button.

Now review the instance details and click on Launch button. ec2 Launch final

You will get a popup to define your key-pair.

You can either select an existing key-pair or create a new key-pair.

If you are selecting an existing key-pair make sure you have access to the key-pair you are selecting.

I will be creating a new key-pair for this use case.

Select the Create new key-pair option and enter a name for the key-pair.

Then click on Download key-pair button to download your key-pair.

make sure you keep the key-pair file in a safe place using this anyone can access your ec2 instance.

ec2-key-pair

After this click Launch Instances to launch your ec2 instance.

You can see that you instance is being created.

Go to your ec2 console you can see a new instance is being created after some time it’s state will be running.

Connect to EC2 instance from windows using putty

Prerequisites

  1. Make sure your Instance state is Running. After Launching a ec2 instance it can take some time for the instance to be ready. So check the state of your instance after some time. and make sure it is in running state before proceeding.

  2. Make sure your Instance is publicly accessible Check and make sure that you have added the necessary rules to your security group to allow ssh traffic.

  3. Make sure you have the public key of your ec2 instance.

  4. You need to have putty installed on your windows machine you can download and install it from putty website.

If you have all the prerequisites then proceed to the next step.

Convert your private key into .ppk format using PuTTYgen

In this step we will be converting our private key from a .pem into .ppk file format.

If your private key is already in .ppk format then you can skip this step.

Search for PuTTYgen in your windows search bar. and click on PuTTYgen to open PuTTYgen.

windows search puttygen

Click on Load to load your private key. puttygen load private key

In the file selection window that appears set the file type to All Files and select your private key file. puttygen private key selection

After you have successfully loaded your private key you will get the following dialog puttygen private key loaded click on OK.

Select Save private key PuTTYgen displays a warning about saving the key without a passphrase. Choose Yes. puttygen save private key

In the file explorer window that appears select the location where you want to save your private key and give it a name. and click on Save. puttygen save private key location

You have successfully converted your private key into .ppk format now you can use it to connect to your ec2 instance from windows using putty. puttygen private key converted

Connect to EC2 instance from windows using putty

Open putty

Enter the hostname box enter the public ipv4 DNS of your ec2 instance.

Make sure the port number is 22 and the connection type is set to SSH.

In the category pane In the Connection section Expand the SSH section and select Auth.

In the right pane select browse and select your private key file in .ppk format using the file explorer. putty add private key for auth

Now come back to the Session section by clicking on Session.

If you want to save this details for later use you can enter a name for the session and click on Save. putty save session

Check the details in the Session section and click on Open to connect to your ec2 instance. putty open session

You will get a putty Security notice Click Accept to continue.

Host falsk app on a ubuntu ec2 instance

In the login as prompt dialog enter ubuntu and click enter.

First let us update the ubuntu system using the following commands

sudo apt update
sudo apt upgrade

Python 3 is pre-installed on the ubuntu image.

Check the python version using the following command python3 -V to see if it is installed if you are using other images. Make sure you are using python 3. python3 -V

Install pip and venv using the following commands:

sudo apt install python3-pip
sudo apt install python3-venv

Clone the sample flask app from github using the following command:

git clone https://github.com/Nikhil1920/sample-flask-app.git

The output will look like this: git clone

cd into the sample flask app directory and create a virtualenv. Activate the virtualenv and install requirements using the following commands:

cd sample-flask-app/
python3 -m venv venv
. venv/bin/activate
pip install -r requirements.txt

Start the flask app on gunicorn server using the following command:

gunicorn wsgi:app -b 0.0.0.0:8080

After starting the gunicorn server you can see the output in the terminal will look like this: gunicorn started

To view the output find the Public IPv4 address of your ec2 instance in your ec2 console and open a browser and navigate to the ip address and port number you have specified in the gunicorn command.

The browser output will look like this: browser output



Tags :