AWS Builder Center
Deploying a Flask Web Application on AWS ECS Using Docker and Amazon ECR

Deploying a Flask Web Application on AWS ECS Using Docker and Amazon ECR

Learn how to containerize a Python Flask application using Docker, push images to Amazon ECR, and deploy the application on Amazon ECS with AWS Fargate. A practical introduction to cloud deployment and container orchestration.

Introduction

Cloud computing has transformed the way developers build, deploy, and manage applications. Instead of managing physical servers, developers can use cloud platforms to deploy applications with better scalability, flexibility, and reliability.
In this article, I will explain how to deploy a Python Flask web application on Amazon Web Services (AWS) using Docker, Amazon Elastic Container Registry (ECR), and Amazon Elastic Container Service (ECS).
The main objective is to understand how containerization works and how AWS services can be integrated to deploy a web application.

1. Understanding the AWS Services

Before starting the deployment, let's understand the services involved.
Amazon EC2: Provides virtual computing resources in the AWS cloud.
Docker: Packages an application and its dependencies into a container, ensuring consistent execution across environments.
Amazon ECR: A managed container registry used to store and manage Docker images.
Amazon ECS: A fully managed container orchestration service that helps deploy, manage, and scale containerized applications.
AWS Fargate: Allows us to run containers without managing the underlying servers.

2. Application Architecture

The deployment follows this workflow:
  1. Develop a Flask application using Python.
  1. Create a Dockerfile to containerize the application.
  1. Build a Docker image.
  1. Push the image to Amazon ECR.
  1. Create an ECS cluster.
  1. Configure an ECS task definition.
  1. Deploy the application using an ECS service.
  1. Access the application through its public endpoint.

3. Creating a Flask Application

First, create a simple Flask application.
Create a file named  app.py .
from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
return "Hello! My Flask Application is running on AWS ECS."

if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
Create a  requirements.txt  file:
Flask
gunicorn
This application runs on port 5000 and provides a simple response when a user visits the homepage.

4. Containerizing the Application Using Docker

Create a file named  Dockerfile .
FROM python:3.11-slim

WORKDIR /app

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 5000

CMD ["gunicorn", "--bind", "0.0.0.0:5000", "app:app"]
The Dockerfile defines the environment required to execute the Flask application.
Build the Docker image:
docker build -t quickshare .
Run the container locally:
docker run -d -p 5000:5000 quickshare
Open the browser and access:
 http://localhost:5000 
If the application responds successfully, the Docker container is running correctly.

5. Creating an Amazon ECR Repository

Amazon ECR is used to store the Docker image.
Follow these steps:
  1. Open the AWS Management Console.
  1. Navigate to Amazon Elastic Container Registry.
  1. Select Create Repository.
  1. Enter a repository name.
  1. Select the appropriate repository visibility and settings.
  1. Create the repository.
Authenticate Docker with Amazon ECR:
aws ecr get-login-password --region REGION | docker login --username AWS --password-stdin ACCOUNT_ID.dkr.ecr.REGION.amazonaws.com
Tag the Docker image:
docker tag quickshare:latest ACCOUNT_ID.dkr.ecr.REGION.amazonaws.com/quickshare:latest
Push the image:
docker push ACCOUNT_ID.dkr.ecr.REGION.amazonaws.com/quickshare:latest
After completing this step, the Docker image will be available in the ECR repository.

6. Deploying the Application on Amazon ECS

Now, we can deploy the container using Amazon ECS.
Step 1: Create an ECS Cluster
  • Open Amazon ECS.
  • Select Clusters.
  • Create a new cluster.
  • Choose AWS Fargate as the infrastructure option.
Step 2: Create a Task Definition
A task definition describes how the container should run.
Configure the following:
  • Launch type: AWS Fargate
  • Operating system: Linux
  • Container image: Amazon ECR image URI
  • Container port: 5000
  • CPU and memory: Select suitable values for the application
  • Task execution role: Configure the required ECR image-pull permissions.
Step 3: Create an ECS Service
Create a service using the task definition.
Configure:
  • Desired number of tasks.
  • Fargate launch type.
  • VPC and subnet configuration.
  • Security group allowing the required inbound traffic.
For a production-style deployment, configure an Application Load Balancer and route HTTP traffic to the container on port 5000.

7. Accessing the Deployed Application

Once the ECS service reaches a stable running state, access the application through the Application Load Balancer DNS endpoint.
The request follows this path:
User Browser → Application Load Balancer → ECS Service → Flask Container.
The Flask application can now be accessed over the internet through the configured public endpoint.

8. Important Security Considerations

When deploying applications on AWS, security should be considered from the beginning.
  • Follow the principle of least privilege when configuring IAM roles.
  • Never store AWS credentials inside Docker images.
  • Use security groups to restrict unnecessary network access.
  • Enable HTTPS using an appropriate TLS certificate.
  • Store sensitive application configuration in AWS Secrets Manager or Systems Manager Parameter Store.
  • Monitor application activity and infrastructure using Amazon CloudWatch.
  • Avoid exposing container ports directly to the public internet when using a load balancer.

9. Challenges and Learning Outcomes

During this implementation, understanding the relationship between Docker, ECR, and ECS is particularly important.
Some common challenges include:
  • Docker image tagging and pushing errors.
  • ECR authentication issues.
  • Incorrect container port mappings.
  • ECS task startup failures.
  • IAM execution role permission issues.
  • Security group and network configuration problems.
Troubleshooting these issues provides practical experience in cloud deployment and container management.

Conclusion

Deploying a Flask application using Docker, Amazon ECR, and Amazon ECS demonstrates how modern cloud infrastructure simplifies application deployment.
Through this implementation, we can understand containerization, image management, container orchestration, IAM permissions, and cloud networking.
This project provided me with practical exposure to AWS cloud services and helped me understand how applications can move from local development environments to cloud infrastructure.
I look forward to exploring more AWS services, automation techniques, and DevOps practices in future projects.
#AWS #AmazonECS #AmazonECR #Docker #Flask #AWSCloud #DevOps #CloudComputing #Python
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article