How to Deploy Your Python Flask App to the Cloud
Building a Flask application locally is only half the job. Getting it running reliably in production — accessible to real users, scalable under load, and properly configured — is where most developers hit their first serious wall. This guide walks you through exactly how to deploy Flask app projects to three popular cloud platforms: Render, Heroku, and AWS Elastic Beanstalk. No hand-waving. Just concrete steps and working code.
1. Prepare Your Flask App for Production
Before touching any cloud platform, your application needs a few production-ready changes. First, never run Flask's built-in development server in production. Use a production WSGI server like Gunicorn instead.
Install it and add it to your dependencies:
pip install gunicorn
pip freeze > requirements.txt
Your requirements.txt is critical — every cloud platform reads it to install your dependencies. Also ensure your app uses environment variables for secrets. Never hardcode API keys or database credentials.
import os
from flask import Flask
app = Flask(__name__)
app.secret_key = os.environ.get("SECRET_KEY", "fallback-dev-key")
2. Create a Procfile and Runtime File
Platforms like Heroku and Render use a Procfile to know how to start your application. Create one in your project root:
web: gunicorn app:app
Here, the first app refers to your Python file (app.py) and the second refers to the Flask instance inside it. If your file is named wsgi.py and your instance is application, adjust accordingly.
Optionally, add a runtime.txt to pin your Python version:
python-3.11.6
3. Deploy to Render (Recommended for Beginners)
Render is one of the easiest platforms for Python web development deployments. It offers a generous free tier and automatic deploys from GitHub.
- Push your project to a GitHub repository.
- Sign up at render.com and click "New Web Service."
- Connect your GitHub repo and select the branch to deploy.
- Set the Build Command to
pip install -r requirements.txt. - Set the Start Command to
gunicorn app:app. - Add environment variables (like
SECRET_KEY) in the Environment tab.
Render handles SSL certificates automatically and redeploys on every push to your chosen branch. For most software engineering projects and side applications, this is all you need.
4. Deploy to Heroku
Heroku pioneered platform-as-a-service deployments and remains a solid choice with its CLI-driven workflow. After installing the Heroku CLI and logging in:
heroku create my-flask-app
git push heroku main
heroku config:set SECRET_KEY=your-secret-value
heroku open
Heroku reads your Procfile and requirements.txt automatically. Use heroku logs --tail to debug startup issues. Note that Heroku's free dynos were discontinued in 2022 — you'll need a paid plan for persistent hosting.
5. Deploy to AWS Elastic Beanstalk
For production-grade deployments with more control, AWS Elastic Beanstalk lets you deploy Flask app containers to managed EC2 infrastructure. Install the EB CLI and initialize your project:
pip install awsebcli
eb init -p python-3.11 my-flask-app
eb create my-flask-env
eb deploy
Elastic Beanstalk requires a file at .ebextensions/python.config to configure the WSGI path:
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: app:app
AWS gives you VPC configuration, load balancing, auto-scaling, and RDS integration — all essential for serious web development and enterprise applications.
6. Environment Variables and Database Configuration
Regardless of platform, keep configuration out of your codebase. Use python-dotenv locally and platform-specific environment variable dashboards in production. For databases, configure your connection string via an environment variable:
DATABASE_URL = os.environ.get("DATABASE_URL")
app.config["SQLALCHEMY_DATABASE_URI"] = DATABASE_URL
If you're using PostgreSQL on Heroku or Render, both platforms provide a DATABASE_URL automatically when you attach a Postgres add-on or service.
7. Common Deployment Pitfalls
Even experienced developers hit these issues. Watch out for:
- Missing dependencies: Run
pip freeze > requirements.txtinside your virtual environment, not globally. - Port binding: Flask must bind to the port provided by the platform. Gunicorn handles this automatically; never hardcode port 5000.
- Static files: In production, serve static assets via a CDN or configure WhiteNoise:
pip install whitenoiseand wrap your WSGI app. - Debug mode on: Ensure
DEBUG=Falsein production. Exposing the debugger is a critical security vulnerability.
With these foundations, you can confidently deploy Flask app projects from your laptop to a live cloud environment. Whether you choose Render for simplicity, Heroku for its ecosystem, or AWS for enterprise scale, the core principles of production readiness — WSGI servers, environment variables, and proper configuration — remain the same across all platforms.