AWS Builder Center
Build and Deploy a Python Flask App on AWS EC2

Build and Deploy a Python Flask App on AWS EC2

Learn how to deploy a simple Python Flask application on an AWS EC2 instance using a security group, virtual environment, and Nginx. This beginner-friendly walkthrough takes you from local Flask code to a publicly accessible web application.

i build stuff on cloud

Build and Deploy a Python Flask App on AWS EC2

Deploying your first web application on AWS can seem complicated, but a simple Flask application is a great way to understand the basics of cloud deployment.
In this guide, we'll deploy a Python Flask application on an Amazon EC2 instance and use Nginx as a web server.

What we'll build

The architecture is straightforward:
User → Nginx → Flask → EC2
We'll use:
  • Amazon EC2 — runs our application
  • Python + Flask — application framework
  • Nginx — web server and reverse proxy
  • Security Groups — control network access

1. Create an EC2 instance

Create an EC2 instance using a recent Ubuntu LTS AMI.
For a learning project, a small instance is sufficient.
Make sure the security group allows:
1
2
SSH : 22
HTTP : 80
Avoid opening unnecessary ports to the public internet.

2. Connect to the instance

After connecting through SSH, update the system:
1
sudo apt update && sudo apt upgrade -y
Install Python and the required packages:
1
sudo apt install python3 python3-pip python3-venv nginx -y

3. Create the Flask application

Create a project directory:
1
2
mkdir ~/flask-app
cd ~/flask-app
Create a virtual environment:
1
2
python3 -m venv venv
source venv/bin/activate
Install Flask:
1
pip install flask gunicorn
Create app.py:
1
from flask import Flaskapp = Flask(__name__)@app.route("/")def home(): return "Hello from AWS! 🚀"if __name__ == "__main__": app.run(host="0.0.0.0", port=5000)
Test the application:
1
python3 app.py
At this point, Flask should be running on port 5000.
For production-style access, we'll put Nginx in front of it instead of exposing Flask directly.

4. Configure Gunicorn

Stop the development server and test Gunicorn:
1
gunicorn --bind 127.0.0.1:5000 app:app
Gunicorn will run the Flask application locally on the EC2 instance.

5. Configure Nginx

Create an Nginx configuration:
1
sudo nano /etc/nginx/sites-available/flask-app
Add:
1
2
3
4
5
6
7
8
9
10
11
server {
listen 80;
server_name _;

location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Enable the configuration:
1
2
3
sudo ln -s /etc/nginx/sites-available/flask-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
Now Nginx receives requests on port 80 and forwards them to Flask.

6. Test the application

Find your EC2 instance's public IPv4 address from the AWS console.
Open:
1
http://YOUR_EC2_PUBLIC_IP
You should see:
Hello from AWS! 🚀
And that's it — your Flask application is now running on AWS.

What I learned

This small project demonstrates several important cloud concepts:
  • Launching and accessing an EC2 instance
  • Configuring security groups
  • Creating isolated Python environments
  • Running Flask with Gunicorn
  • Using Nginx as a reverse proxy
  • Connecting multiple layers of a web application
From here, the project can be extended with HTTPS using AWS Certificate Manager, a domain name, systemd for automatic startup, an Application Load Balancer, S3 for static files, and a managed database such as Amazon RDS.
Cloud development becomes much easier when you start with a small application and gradually add each component.
Next step: Try deploying your own Flask project and replace the simple "Hello from AWS" route with something useful.
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