Python > Deployment and Distribution > Deployment Platforms > Cloud Platforms (e.g., AWS, Google Cloud, Azure)
Deploying a Simple Flask App to AWS Elastic Beanstalk
This snippet demonstrates deploying a basic Flask application to AWS Elastic Beanstalk. Elastic Beanstalk simplifies the process of deploying and managing web applications and services on AWS. It automatically handles the infrastructure provisioning and operating system updates, allowing you to focus on your application.
Project Setup (app.py
)
This is a very basic Flask application that defines a single route, /
, which returns 'Hello, World!'. The host='0.0.0.0'
makes the application accessible from outside the container, and port=8080
specifies the port the application listens on.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8080)
requirements.txt
This file lists the Python packages required for your application to run. Elastic Beanstalk uses this file to install the necessary dependencies.
Flask==2.3.2
.ebextensions/python.config
This configuration file, placed in the .ebextensions
directory, tells Elastic Beanstalk how to run your application. Specifically, it specifies the WSGI application path, which is application:app
in this case, referring to the app
object in the application.py
file (we'll rename app.py to application.py in the next step). This is crucial for Elastic Beanstalk to find and run your Flask application.
option_settings:
- namespace: aws:elasticbeanstalk:container:python
option_name: WSGIPath
value: application:app
Deployment Steps (AWS CLI)
python.config
file usually expects the main application file to be named `application.py`. Alternatively you can modify `python.config` to match your filename.eb create
command initializes a new Elastic Beanstalk application. Replace my-flask-app
with your desired application name. Choose a Python platform that suits your needs (e.g., "Python 3.8 running on 64bit Amazon Linux 2").eb deploy
command packages your application and uploads it to Elastic Beanstalk, where it is deployed.
# 1. Rename app.py to application.py (Important for Beanstalk)
mv app.py application.py
# 2. Create an Elastic Beanstalk application (if you haven't already)
eb create my-flask-app --platform "Python 3.8 running on 64bit Amazon Linux 2"
# 3. Deploy your application
eb deploy
Concepts Behind the Snippet
This snippet leverages Platform as a Service (PaaS) principles. Elastic Beanstalk handles the underlying infrastructure, allowing developers to focus solely on writing and deploying code. It uses WSGI (Web Server Gateway Interface) to interface between the web server (e.g., Apache or Nginx) and the Python application.
Real-Life Use Case Section
Deploying simple APIs or web applications. For example, a microservice that handles user authentication, or a basic data processing pipeline.
Best Practices
Interview Tip
When discussing Elastic Beanstalk, highlight its PaaS nature and the benefits it provides in terms of reduced operational overhead and faster deployment cycles. Also, mention the importance of understanding the underlying infrastructure principles even when using PaaS.
When to Use Them
Use Elastic Beanstalk when you want to quickly deploy and manage web applications without having to manually configure servers and infrastructure. It's a good choice for simple to moderately complex applications where you need a balance between control and ease of use.
Memory Footprint
The memory footprint depends on the instance type you choose for your Elastic Beanstalk environment. It's important to select an instance type that is appropriate for the resource requirements of your application.
Alternatives
Alternatives to Elastic Beanstalk include:
Pros
Cons
FAQ
-
What if I get a WSGI error when deploying?
Double-check that the WSGIPath in your.ebextensions/python.config
file is correct. Make sure it points to the correct application object in your Python file (e.g.,application:app
). -
How do I configure environment variables in Elastic Beanstalk?
You can configure environment variables through the Elastic Beanstalk console or using the AWS CLI with theeb setenv
command. -
How do I scale my Elastic Beanstalk environment?
Elastic Beanstalk supports auto-scaling. You can configure scaling triggers based on metrics like CPU utilization and network traffic.