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


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
Task | Command |
SSH | ssh -i "key.pem" ec2-user@ip |
Install Python | sudo yum install python3 -y |
Install pip | sudo yum install python3-pip -y |
Flask Install | pip3 install flask |
Gunicorn Install | pip3 install gunicorn |
Run Gunicorn | gunicorn -w 3 -b 0.0.0.0:5000 app:app |
Install NGINX | sudo yum install nginx -y |
Start NGINX | sudo systemctl restart nginx |
Run Background | pm2 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
orsystemd
to manage processesRestrict 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
Subscribe to my newsletter
Read articles from Pratik Das directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
