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
- 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
Step 3: Create a Simple Flask Application
If you don’t have a Flask application yet, here’s a simple example:
Python
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:
Bash
Step 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:
Dockerfile
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:
Bash
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
- Tag your Docker image: Tag your image with your ECR repository URI:
Bash
- Push your Docker image: Push your image to ECR:
Bash
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.