DEV Community

Rajit Paul for AWS Community Builders

Posted on

Dockerize an API based Flask app and deploy on Amazon ECS

Hi Folks!
This is the first blog of the series Dockerize Your Application.

In the age of microservices we want our application code and requirements to be packed in an image and use that in a suitable container orchestration tool for better scalability and availability.


  • An EC2 Server with git and docker installed
  • Amazon ECR Repository
  • AWS CLI configured on your server with sufficient permissions (to push image to ecr) or you can attach a role to your server with sufficient permissions.
  • Region: Mumbai(ap-south-1)

About the API

It's a todo app with two API's one to get the list of to-do tasks and another to post your tasks.
GET-POST api path - /todo/api/v1.0/tasks

Source: [Flask_API_App]



FROM alpine:latest

RUN apk add py3-pip
RUN pip3 install flask




ENTRYPOINT ["/usr/bin/flask","run"]

CMD ["--host=", "--port=80"]
Enter fullscreen mode Exit fullscreen mode

Login to your EC2 Server and clone the repo

git clone -b collate

Change directory and build docker image

cd AWS_workshop_2022_data/python_flask_code_in_aws_linux_restful_GET_POST
docker build -t flask_api_app:v1 .

Tag your docker image

docker tag flask_api_app:v1

Login to your ECR Repo

aws ecr get-login-password --region ap-south-1 | docker login --username AWS --password-stdin

Push the docker image

docker push

Deploy to ECS

Select Services and then select ECS
Image description

Create an ECS Cluster

We shall be creating an ECS Cluster with EC2
Image description

Cluster Config

Choose a suitable name for your cluster, and select a provisioning model, in this case we shall go for Spot.
Choose diversified spot instance allocation strategy so the instances are spread across az's.
Select two instance types on a or basis.
Image description

Specify the Storage spec and select an existing key pair, so that you can ssh later to the EC2 instance and do some modification or troubleshoot from the terminal.
Create a new VPC or you can select an existing VPC.
Image description

Cluster created!
Image description

Create a Task Definition

Select the launch type as EC2
Image description
Provide a suitable task def name, and select the Task Role and network mode (we shall be looking into the different network modes in an upcoming blog, for now let's go ahead with bridge), select a task execution role.
Image description
Allocate sufficient task memory and cpu based on your application requirements.
Image description

Add a container

Provide a container name and the ecr repo uri along with the version, you can set hard limit for the container in case you have set the task cpu and memory req this is not required, if you want dynamic port mapping, keep the host port as 0, in this case we have set it to 80 same as the container port
Image description

As per requirement you can explore advanced details and set container healthcheck, container timeouts, storage, logging and more.

Create a Service

Select the launch type as EC2, select your task definition and it's version you can see the latest suffix to denote the latest version, select your cluster and provide a suitable service name, select a service type (in this case we shall go with replica), provide the number of tasks you want to run (keep in mind the instance type you chose and the resource allocated to each task while you designate the number of tasks)
Image description
Select the deployment strategy (we shall go into depth on this in an upcoming blog, in this case we choose Rolling Update), select a Task Placement strategy (AZ Balanced spread will help to spread tasks across instances in different AZ's for high availability)
Image description

Configure a Load Balancer

Select your load balancer type(in this case we choose application load balancer), Create a new service IAM role, and select your existing Load Balancer.
Image description
In your target group you can register the existing ECS instance, and set the health check path as /todo/api/v1.0/tasks
Image description
If you want to scale your tasks you can enable autoscaling, in this case we do not want to scale our tasks so we won't enable auto scaling.
Image description
Select your listener, target group name for the Load Balancer and rest shall be populated
Image description
Service Created and Task Running!
Image description
Image description
Image description

Allow Port 80 in EC2 and ALB
Image description
Image description

Test API using ALB URL

GET Request

Image description

PUT Request

Image description

If you have any queries you can connect with me on LinkedIn

Top comments (0)