Day 23 of #90DaysOfCloud: Deploying a Python App on AWS EC2

Pratik DasPratik Das
3 min read

Todayโ€™s milestone was all about deploying a Python Flask web application on a live AWS EC2 instance using native tools โ€” no Docker, no containers. This gives a deep dive into what happens under the hood when preparing apps for production.


๐Ÿง  Why This Matters

Flask is one of the most widely used micro-frameworks for Python web development. Hosting it manually teaches essential DevOps skills:

  • Linux server administration

  • Python package management

  • Reverse proxy configuration with NGINX

  • Running apps in the background

  • Production-grade practices with Gunicorn


โ˜๏ธ Infrastructure Overview

โœ… Services & Tools Used:

  • AWS EC2 instance (Amazon Linux 2)

  • Python 3 and pip

  • Flask web framework

  • Gunicorn (WSGI production server)

  • NGINX (as a reverse proxy)


๐Ÿš€ Step-by-Step Deployment

๐Ÿ”น 1. Launch EC2 Instance

  • AMI: Amazon Linux 2

  • Instance type: t2.micro (Free tier)

  • Security Group Rules:

    • SSH (Port 22): Your IP

    • HTTP (Port 80): Anywhere

Assign a public IP and SSH into it:

ssh -i "your-key.pem" ec2-user@<your-ec2-public-ip>

๐Ÿ”น 2. Install Python and pip

Amazon Linux 2 comes with Python 3 pre-installed. Confirm it:

python3 --version

If not installed:

sudo yum install python3 -y

Install pip:

sudo yum install python3-pip -y

๐Ÿ”น 3. Create Flask Application

Create a new directory and Flask app:

mkdir flaskapp && cd flaskapp
vim app.py

Paste the following simple Flask code:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return "Hello from Flask on AWS EC2!"

if __name__ == '__main__':
    app.run(host='0.0.0.0')

Install Flask:

pip3 install flask

Test the app locally:

python3 app.py

Go to http://<your-ec2-public-ip>:5000 to see the app.


๐Ÿ”น 4. Use Gunicorn for Production

Gunicorn allows serving Flask apps more reliably in production:

pip3 install gunicorn

Run app with Gunicorn:

gunicorn -w 3 -b 0.0.0.0:5000 app:app

๐Ÿ”น 5. Install and Configure NGINX

Install NGINX:

sudo amazon-linux-extras enable nginx1
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx

Edit the default config:

sudo vim /etc/nginx/nginx.conf

Add this inside the server {} block:

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;
}

Restart NGINX:

sudo systemctl restart nginx

Now visit http://<your-ec2-public-ip> and your Flask app will load!


๐Ÿ”น 6. Run Flask in the Background

Use pm2 to keep app running:

sudo npm install -g pm2
pm2 start gunicorn --name flaskapp -- gunicorn -w 3 -b 0.0.0.0:5000 app:app

Optional: save process for reboot

pm2 startup
pm2 save

โœ… Summary of Key Commands

TaskCommand
SSHssh -i "key.pem" ec2-user@ip
Install Pythonsudo yum install python3 -y
Install pipsudo yum install python3-pip -y
Flask Installpip3 install flask
Gunicorn Installpip3 install gunicorn
Run Gunicorngunicorn -w 3 -b 0.0.0.0:5000 app:app
Install NGINXsudo yum install nginx -y
Start NGINXsudo systemctl restart nginx
Run Backgroundpm2 start gunicorn ...

๐Ÿ” Best Practices

  • Always use Gunicorn or similar WSGI server in production

  • NGINX helps handle static files and acts as a reverse proxy

  • Use pm2 or systemd to manage processes

  • Restrict inbound rules for safety


๐Ÿ“… Whatโ€™s Next?

Tomorrow is Day 24 โ†’ Iโ€™ll dive into AWS EBS & AMI fundamentals, including:

  • Mounting and formatting EBS volumes

  • Creating and managing custom AMIs

  • Taking EBS snapshots

  • Copying snapshots across regions for backup and recovery

This will strengthen my understanding of data durability, backup strategies, and image-based deployments in real-world cloud setups!


Stay tuned and follow the journey! ๐Ÿ’ปโ˜๏ธ

#Flask #Python #AWS #EC2 #NGINX #Gunicorn #DevOps #Cloud #90DaysOfCloud

0
Subscribe to my newsletter

Read articles from Pratik Das directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pratik Das
Pratik Das