Deploy your Flask application to Amazon EC2

Sharmila S
featurepreneur
Published in
5 min readJun 15, 2021

In this article, I am going to explain the steps involved in deploying a flask application to an Amazon EC2 instance.

EC2 — Elastic Compute Cloud service is a compute service based on the cloud by AWS which scales up automatically based on the traffic it receives.

Create an EC2 instance

Select EC2 from the services.

Select ‘Instances’ in the EC2 dashboard

Now, click on ‘launch instances’ button

Configure an instance

1. The first step is to choose an Image for your instance. Here, I’m choosing the Amazon Linux AMI.

2. The next step is to select the type of instance. Choose the type which suits your needs, I am going to leave it to the default one. Then, click Next.

3. In this step, you can decide the number of instances to be launched and configure the VPC and Subnet. Then, click Next.

4. In this step, you have to configure storage for your instance. You would have a root volume added to your instance by default. If you want to add additional storage for your instance, you can add one by clicking on the ‘Add New Volume’ button.

Here, I am not including any extra volume, and continuing with the default features. Click Next.

5. You can add tags if you want and it is optional.

6. The last step is to add a security group. A security group allows you to provide access only to a particular range of address and protocol.

My app will be running on port 5000, so I am adding a new rule(Custom TCP Rule) for port 5000. And I’m allowing access from anywhere to my app.
After configuring the security group, click ‘Review and Launch’.

Review your settings and click Launch. Once you launch your instance, you will be prompted with a dialog box as shown below.

If you don’t have a key pair already, select ‘Create a new key pair’ and provide a name for the new key pair, then download and save it securely in your system. Then click ‘Launch Instances’.

It would take a few minutes for the instance to be up and running.

Your instance is ready when the instance state changes from ‘pending’ to ‘running’.

Select the instance (checkbox on the left) and click on the ‘Connect’ option from ‘Actions’.

Under the SSH client tab, the instructions to connect to your instance are provided.

Open the terminal in your system (Only for Linux and macOS)

In the terminal, go to the folder containing the key pair file and enter

$ chmod 400 <key-pair-file>.pem

Now, connect to the instance using the key file and Public IPv4 DNS

$ ssh -i "<key-file>.pem" ec2-user@<Public-DNS>

Once you enter the above command, you will be connected to your ec2 instance.

Now, we need to create/copy our application to this instance and run it.

We need python3 and pip3 for our Flask application to run, so check whether python3 and pip3 are installed. (If not, we have to install manually).

python3 in instance
pip3 in instance

Run a flask application in the instance

Let’s try to run a simple Flask application in our instance. I am going to clone the application from Github.

If git is not installed in the instance, use sudo yum install git to install git.

python3, pip3 and git installed

Let’s clone a simple flask application from GitHub

$ git clone https://github.com/Hilda24/Flask-Hello_World
$ cd Flask-Hello_World/

Now let’s install the dependencies specified in the requirements.txt.

$ pip3 install -r requirements.txt

Now, let’s run our app.

$ python3 main.py
App running

Our app is running. To view our app, copy the Public IPv4 address of your instance from the EC2 dashboard.

Go to the IP address at port 5000 in your browser.

The application running in EC2

Happy learning!

--

--

Sharmila S
featurepreneur

Software Engineer | Writes about Full Stack Web Development | 1 X AWS | CKA http://sharmilas.bio.link/