How to Deploy a Python Flask Application to AWS EC2 Instance


Introduction

This AWS EC2 tutorial shows how to Deploy Flask to AWS using a simple, production-friendly setup: Ubuntu, Gunicorn, and Nginx. If you want a practical Python cloud deployment workflow, this guide covers the essential steps without extra complexity.

Prerequisites

  • An AWS account
  • An EC2 Ubuntu instance
  • A key pair for SSH access
  • A basic Flask app ready to upload

Step 1: Launch and Connect to Your EC2 Instance

Create an Ubuntu EC2 instance in the AWS console. In the security group, allow:

  • SSH on port 22
  • HTTP on port 80
  • HTTPS on port 443 if needed

Then connect with SSH:

ssh -i your-key.pem ubuntu@your-ec2-public-ip

Step 2: Install Python and Required Packages

Update the server and install Python tools, Nginx, and virtual environment support:

sudo apt update && sudo apt upgrade -y
sudo apt install python3-pip python3-venv nginx -y

Step 3: Upload Your Flask App

Move your project to the server using Git or SCP. A minimal Flask app might look like this:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return 'Flask app running on AWS EC2'

Create a virtual environment and install dependencies:

python3 -m venv venv
source venv/bin/activate
pip install flask gunicorn

Step 4: Test Gunicorn

Before configuring system services, test that Gunicorn can serve your app:

gunicorn --bind 0.0.0.0:8000 app:app

Open http://your-ec2-public-ip:8000 in a browser. If it works, your Flask app is ready for the next step in this Python cloud deployment process.

Step 5: Configure systemd for Gunicorn

Create a service so Gunicorn starts automatically:

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

Add a minimal service configuration:

[Unit]
Description=Gunicorn for Flask app
After=network.target

[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/your-app-folder
ExecStart=/home/ubuntu/your-app-folder/venv/bin/gunicorn --bind 127.0.0.1:8000 app:app

[Install]
WantedBy=multi-user.target

Then enable and start it:

sudo systemctl daemon-reload
sudo systemctl enable flaskapp
sudo systemctl start flaskapp

Step 6: Configure Nginx as Reverse Proxy

This Nginx Gunicorn Flask setup lets Nginx handle public traffic and forward requests to Gunicorn.

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

Add a simple server block:

server {
    listen 80;
    server_name your-ec2-public-ip;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Enable the config and restart Nginx:

sudo ln -s /etc/nginx/sites-available/flaskapp /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx

Step 7: Verify Deployment

Visit your EC2 public IP in the browser. Your Flask application should now load through Nginx. This completes the core steps to Deploy Flask to AWS on an EC2 instance.

Troubleshooting Tips

Check service status

sudo systemctl status flaskapp
sudo systemctl status nginx

View logs

journalctl -u flaskapp --no-pager

Conclusion

This AWS EC2 tutorial covered a clean deployment path for Flask using Gunicorn and Nginx. For many small to medium projects, this Nginx Gunicorn Flask approach is a reliable foundation for Python cloud deployment on AWS.