AskHandle Blog
The Easiest Way to Deploy a Python Flask App to AWS

The Easiest Way to Deploy a Python Flask App to AWS
You have a Python Flask app ready to go, and now you want to get it online. Deploying to Amazon Web Services (AWS) can be straightforward. This guide will help you deploy your web application step by step.
Step 1: Set Up Your AWS Account
You need an AWS account to start. If you don’t have one, sign up at aws.amazon.com. AWS offers a free tier, which is great for testing and launching your app without upfront costs.
Step 2: Install the AWS CLI
You need the AWS Command Line Interface (CLI) tool to interact with your AWS services from the terminal. To install it, follow these steps:
- Install AWS CLI: Install it via pip by running:
bash
1pip install awscli - Configure AWS CLI: After installation, run:
Enter your AWS Access Key ID, Secret Access Key, region, and default output format. You can find this information in your AWS Management Console under "My Security Credentials."bash
1aws configure
Step 3: Create a Simple Flask Application
If you don’t have a Flask application yet, here’s a simple example:
1from flask import Flask
2
3app = Flask(__name__)
4
5@app.route('/')
6def home():
7 return "Hello, world!"
8
9if __name__ == '__main__':
10 app.run(debug=True)Save this file as app.py.
Step 4: Install and Configure Flask
Ensure Flask is installed in your development environment. Use pip to install Flask:
1pip install flaskStep 5: Create a Dockerfile
A Dockerfile contains commands to build an image for your Flask app. Create a file named Dockerfile in the same directory as app.py and add the following content:
1# Use the official lightweight [Python](/glossary/python) image.
2FROM python:3.8-slim
3
4# Set the working directory.
5WORKDIR /app
6
7# Copy the current directory contents into the container.
8COPY . /app
9
10# Install the required packages.
11RUN pip install flask
12
13# Set the environment variable.
14ENV FLASK_APP=app.py
15
16# Make ports accessible.
17EXPOSE 5000
18
19# Run the application.
20CMD ["flask", "run", "--host=0.0.0.0"]Step 6: Create a Docker Image
To create a Docker image from the Dockerfile, open your terminal, navigate to the directory with app.py and Dockerfile, and run:
1docker build -t flask-app .Step 7: Push Your Docker Image to Amazon ECR
Amazon Elastic Container Registry (ECR) is a managed Docker container registry. To push your Docker image to ECR:
- Create a repository: In the AWS Management Console, go to ECR and create a new repository named
flask-app. - Authenticate Docker with ECR: Use the AWS CLI:
bash
1$(aws ecr get-login --no-include-email --region your-region) - Tag your Docker image: Tag your image with your ECR repository URI:
bash
1docker tag flask-app:latest your-aws-account-id.dkr.ecr.your-region.amazonaws.com/flask-app:latest - Push your Docker image: Push your image to ECR:
bash
1docker push your-aws-account-id.dkr.ecr.your-region.amazonaws.com/flask-app:latest
Step 8: Create an ECS Cluster
AWS Elastic Container Service (ECS) allows you to run Docker containers at scale.
- Create a Cluster: In your AWS Management Console, go to ECS and create a new cluster using the "Networking only" template.
- Create a Task Definition: In ECS, create a new task definition using the EC2 launch type and configure it to use the Docker image from ECR.
- Run a Task: Go back to your ECS cluster and run a new task using the task definition you created. Ensure proper port mappings.
Step 9: Set Up a Load Balancer
To make your Flask app accessible from the internet, set up a load balancer:
- Create a Load Balancer: In the EC2 section of your AWS Management Console, go to "Load Balancers" and create a new Application Load Balancer.
- Configure Security Groups: Allow inbound traffic on port 80 (HTTP).
- Register Your ECS Service: Create a target group and register your ECS tasks with this target group.
Step 10: Access Your Flask Application
Get the DNS name of your load balancer from the EC2 console. Open your web browser and go to this DNS name. You should see "Hello, world!" displayed, indicating that your Flask app is up and running!
Deploying a Flask app to AWS can be simple. With these steps, you can set up your AWS account, configure Docker, and get your application running in the cloud.