Run a Flask App Locally for Elastic Beanstalk¶
This tutorial prepares a Flask project for AWS Elastic Beanstalk Python by matching local behavior to the documented deployment model. The target runtime is Flask behind Gunicorn, with application.py exporting application.
Prerequisites¶
- Python 3.11+ installed.
- A clean project folder.
pipavailable.- Optional:
venvmodule available from your Python installation.
What You'll Build¶
You will build a minimal Flask app that:
- Runs with Flask development server for quick iteration.
- Runs with Gunicorn using the same callable format expected by Elastic Beanstalk.
- Includes a
Procfilealigned with AWS guidance for process startup.
Project target structure:
Steps¶
- Create and activate a virtual environment.
- Install Flask and Gunicorn, then freeze dependencies.
- Create
application.pywith the required callable name.
from flask import Flask
application = Flask(__name__)
@application.get("/")
def index():
return {"message": "ok"}
if __name__ == "__main__":
application.run(host="0.0.0.0", port=5000, debug=True)
- Create a
Procfilefor Gunicorn startup.
- Run Flask development mode.
- Run Gunicorn for production-parity local testing.
sequenceDiagram
participant Dev as Developer
participant Flask as Flask Dev Server
participant Gunicorn as Gunicorn Worker Model
participant EB as Elastic Beanstalk Runtime
Dev->>Flask: python3 -m flask run
Dev->>Gunicorn: gunicorn --bind :8000 --workers 2
Gunicorn->>EB: Same WSGI callable (application:application) Verification¶
Run these checks before any Elastic Beanstalk deployment:
python3 -m flask --app application.py routes
gunicorn --check-config --bind :8000 --workers 2 application:application
python3 -m pip show Flask
python3 -m pip show gunicorn
Expected results:
- Flask route table includes
/. - Gunicorn config validation succeeds.
- Both packages are installed in your virtual environment.
application.pyexportsapplicationexactly.