Deployment of Flask & Django Applications on AWS, GCP, and Heroku

  Add to Bookmark

Once your web application is ready, deploying it to a cloud platform allows users to access it globally. In this tutorial, we will cover how to deploy Flask and Django applications on AWS (EC2), Google Cloud (App Engine), and Heroku.


1. Choosing the Right Deployment Platform

  • AWS (Amazon Web Services) – Suitable for scalable, production-ready applications with full control over the infrastructure.
  • GCP (Google Cloud Platform) – Offers managed services with seamless integration into Google’s ecosystem.
  • Heroku – Simple, easy-to-use platform ideal for quick deployment and small to medium-scale applications.

2. Deploying a Flask Application

2.1 Deploying Flask on AWS EC2

Step 1: Set Up an EC2 Instance

  1. Log in to AWS and go to EC2 Dashboard.
  2. Launch a new Ubuntu 22.04 instance.
  3. Configure security groups to allow SSH (port 22) and HTTP (port 80).
  4. Connect to the instance using SSH:
ssh -i your-key.pem ubuntu@your-ec2-public-ip

Step 2: Install Dependencies

sudo apt update
sudo apt install python3-pip python3-venv nginx

Step 3: Set Up the Flask App

mkdir flask-app && cd flask-app
python3 -m venv venv
source venv/bin/activate
pip install flask gunicorn

Create a simple Flask app (app.py):

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Flask App Deployed on AWS"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

Step 4: Run Gunicorn for Production

gunicorn --bind 0.0.0.0:5000 app:app

Step 5: Configure Nginx as a Reverse Proxy

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

Add the following content:

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

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

Enable and restart Nginx:

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

Your Flask app is now deployed on AWS!


2.2 Deploying Flask on Google Cloud (GCP App Engine)

Step 1: Install Google Cloud SDK

curl https://sdk.cloud.google.com | bash
exec -l $SHELL
gcloud init

Step 2: Create a Flask App

Ensure your project structure is:

flask-app/
│── app.py
│── requirements.txt
│── app.yaml

Step 3: Define the App Engine Configuration (app.yaml)

runtime: python39
entrypoint: gunicorn -b :$PORT app:app

handlers:
- url: /.*
  script: auto

Step 4: Deploy to GCP

gcloud app deploy

Your Flask app is live on Google App Engine.


2.3 Deploying Flask on Heroku

Step 1: Install Heroku CLI

curl https://cli-assets.heroku.com/install.sh | sh
heroku login

Step 2: Prepare the Flask App

Create Procfile:

web: gunicorn app:app

Step 3: Deploy to Heroku

git init
heroku create flask-app-name
git add .
git commit -m "Deploy Flask app"
git push heroku master

Your Flask app is live on Heroku!


3. Deploying a Django Application

3.1 Deploying Django on AWS EC2

Follow similar steps as Flask, but:

  • Install Django: pip install django gunicorn
  • Use Gunicorn to serve the app:
gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
  • Update Nginx config to listen on port 8000 instead of 5000.

3.2 Deploying Django on GCP (App Engine)

Follow the same steps as Flask, but update app.yaml:

runtime: python39
entrypoint: gunicorn -b :$PORT myproject.wsgi

Then, deploy using:

gcloud app deploy

3.3 Deploying Django on Heroku

Follow the same steps as Flask, but:

  • Add Procfile:
web: gunicorn myproject.wsgi
  • Deploy using Git:
git push heroku master

4. Conclusion

We explored how to deploy Flask and Django applications on AWS EC2, Google Cloud App Engine, and Heroku.

  • AWS is best for full control and large-scale applications.
  • GCP provides an easy-to-use managed service.
  • Heroku is the fastest way to deploy small projects.