AWS Builder Center

Deploying a Flask Application on AWS

A Beginner’s Journey to Deploying a Flask Application on AWS

From Localhost to the Cloud: A Beginner’s Journey to Deploying a Flask Application on AWS

Introduction

For many beginners, cloud computing can initially feel overwhelming.
Terms such as EC2, VPC, security groups, IAM, RDS, Nginx, and Gunicorn can seem like a completely different language when you are used to running applications on localhost.
But one of the best ways to understand cloud computing is not by memorizing services—it is by building and deploying something real.
In this article, I will walk through the journey of taking a Python Flask application from a local development environment to an AWS environment. Along the way, we will understand the role of different AWS services and some of the important decisions involved in deploying a web application.
The goal is not just to deploy an application, but to understand what happens behind the scenes when an application moves from localhost to the cloud.

1. Starting Point: The Application on Localhost

Let's begin with a simple Flask application.
A typical Flask application might look like this:
1
2
3
4
5
6
7
8
9
10
my-flask-app/
│
├── app.py
├── requirements.txt
├── templates/
│ └── index.html
├── static/
│ ├── css/
│ └── js/
└── .env
During development, the application can be started with:
1
python app.py
and accessed through:
1
http://127.0.0.1:5000
At this point, everything works on the developer's machine.
But there is a problem.
Only the developer can access the application.
If we want other people to access it over the internet, we need infrastructure capable of hosting the application.
This is where AWS becomes useful.

2. Understanding the AWS Architecture

Before deploying anything, it is important to understand the basic architecture.
A simple architecture for our Flask application can look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Internet
|
|
Public IP Address
|
v
+--------------+
| EC2 |
| Ubuntu |
+--------------+
|
Gunicorn
|
v
Flask App
|
|
Optional DB
|
v
Amazon
RDS
Each component has a specific responsibility.

Amazon EC2

EC2 provides a virtual server where the Flask application can run.

Amazon VPC

The VPC provides the networking environment in which AWS resources operate.

Security Groups

Security groups control which network traffic can reach the EC2 instance.

Gunicorn

Gunicorn acts as a production WSGI server for the Flask application.

Nginx

Nginx can work as a reverse proxy in front of Gunicorn and can also serve static files efficiently.

Amazon RDS

If the application requires a relational database, Amazon RDS can provide a managed MySQL database.

3. Creating the Network with Amazon VPC

One of the first concepts I learned while working with AWS was that networking is fundamental to cloud deployment.
A VPC allows us to create an isolated network for our AWS resources.
For example:
1
2
VPC
10.0.0.0/16
Inside the VPC, we can create subnets.
A simplified structure could be:
1
2
3
4
5
6
7
VPC: 10.0.0.0/16
│
├── Public Subnet
│ └── EC2
│
└── Private Subnet
└── RDS
The public subnet can contain resources that need to receive traffic from the internet.
The database should generally not be directly exposed to the public internet.
This separation is one of the first important security concepts to understand when learning AWS.

4. Launching an EC2 Instance

The next step is to create an EC2 instance.
For a beginner project, an Ubuntu-based EC2 instance is a convenient choice.
After launching the instance, we connect using SSH:
1
ssh -i my-key.pem ubuntu@<PUBLIC-IP>
Once connected, the EC2 instance becomes our remote Linux server.
This is an important moment in the deployment process.
Instead of running:
1
Flask → My Laptop
we now have:
1
Internet → AWS EC2 → Flask Application

5. Configuring the Security Group

One of the most common beginner mistakes is thinking that having a public IP automatically makes an application accessible.
It doesn't.
The EC2 security group controls inbound traffic.
For example, we might allow:
1
2
3
SSH → Port 22
HTTP → Port 80
HTTPS → Port 443
During development, Flask may also run on:
1
Port 5000
We could temporarily allow port 5000 for testing.
However, in a production-style deployment, it is better to expose HTTP/HTTPS through Nginx instead of directly exposing Flask's development server.
A simple rule to remember is:
Open only the ports that are actually required.
This reduces the attack surface of the server.

6. Installing the Application Dependencies

After connecting to EC2, we need to prepare the environment.
First, update the packages:
1
2
sudo apt update
sudo apt upgrade -y
Then install Python and other required packages.
A virtual environment is useful because it keeps the project's dependencies isolated:
1
python3 -m venv venv
Activate it:
1
source venv/bin/activate
Then install the application's dependencies:
1
pip install -r requirements.txt
This is where the requirements.txt file becomes important.
It allows us to reproduce the application's Python environment on the server.

7. Testing Flask on EC2

Before introducing additional components, it is useful to verify that the Flask application itself works.
For example:
1
python app.py
The application may listen on:
1
0.0.0.0:5000
Then we can test:
1
http://<EC2-PUBLIC-IP>:5000
If the security group and Flask configuration are correct, the application should now be accessible from the internet.
At this point, we have successfully moved our application from:
1
localhost
to:
1
AWS EC2
But we are not finished yet.

8. Why We Shouldn't Use Flask's Development Server in Production

Flask's built-in server is excellent for development.
However, it is not intended to be our production application server.
For a more production-oriented architecture, we can use Gunicorn.
Install it with:
1
pip install gunicorn
Then run:
1
gunicorn --bind 0.0.0.0:8000 app:app
The basic flow becomes:
1
2
3
4
5
6
7
Client
|
v
Nginx
|
v
G
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article