Containerize and Deploy a Simple Flask App on AWS Using ECR and ECS

Chesta Dhingra
Data And Beyond

--

In this blog post, we will walk you through the process of creating a simple “Hello World” Flask application, containerizing it using Docker, and deploying it on AWS ECS with ECR. Whether you are a beginner or looking to refine your deployment skills, this step-by-step guide will help you get started with modern application deployment practices.

Prerequisites

Before we dive in, make sure you have the following prerequisites:

  1. An AWS account.
  2. AWS CLI configured on your local machine.
  3. Docker installed on your local machine.
  4. Basic knowledge of Flask and Python.

Steps for First Simple Flask Deployment

1. Create a Simple Flask Application Printing “Hello-World”

Code for the Flask Application; all the files for example app.py (containing the code), Dockerfile(to dockerize the flask application) and lastly the requirements.txt file, these all files should be in one folder.

  1. Set up your Flask application and save it with the name of app.py:
from flask import Flask

app = Flask(__name__)

@app.route('/')

def hello_world():
return "Hello World"

if __name__=='__main__':
app.run(host='0.0.0.0',port=80)

2. · Dockerize your application via creating the Dockerfile

•#use the python runtime 
FROM python:3.9.19-slim

• # set the working directory to /app
• WORKDIR /app

• # copy the current directory content into the container at /app
• COPY . /app

• #install any neede packages specified in requirements.txt
• RUN pip install --no-cache-dir -r requirements.txt

• #Make port 80 available to the world outside this container
• EXPOSE 80

• #Define the environment variable
• ENV NAME=World

• #Run app.py when the container Launches
• CMD ["python","app.py"]

3. Create a requirements.txt file with Flask installed; command would be

pip freeze > requirements.txt

4. Build the Docker image and run the docker container using below commands

# build the image
docker build -t hello-world-flask

# run the docker command
docker run -d -p 4000:80 hello-world-flask

## now navigate to 'http://localhost:4000' to see your Flask app running inside the Docker container.

## above url will display the flask application.
By author

This ensures that our docker container is working absolutely fine on the local server.

We have successfully containerized the Python Flask app. The next steps involve deploying the Flask application on AWS ECR and ECS.

Deploying on AWS Using ECR and ECS

1. Creating the IAM User with Appropriate Policies

Before we deploy our Dockerized Flask application on AWS ECS, we need to set up an IAM user with the necessary permissions. This user will have the ability to manage ECS services and interact with ECR. Follow these steps to create the IAM user:

a. Create a new IAM user and attach the necessary permissions 1)AdministratorAccess, 2) AmazonEC2ContainerRegistryFullAccess, 3) AmazonECS_FullAccess, 4)AmazonElasticContainerRegistryPublicFullAccess and last is 5) ECS-docker-service (this is the custom inline policy that is attached with theIAM user)

## the custom inline policy that needs to be attached 

i. {
ii. "Version": "2012-10-17",
iii. "Statement": [
iv. {
v. "Effect": "Allow",
vi. "Action": [
vii. "ecs:CreateCluster",
viii. "ecs:DescribeClusters",
ix. "ecs:ListClusters",
x. "ecs:DeleteCluster"
xi. ],
xii. "Resource": "*"
xiii. }
xiv. ]
xv. }

Now that the IAM user is created, log in with the provided permissions. Search for ECR in the AWS Management Console to create the repository. Follow these steps to create the ECR repository:

  1. In the General Settings, set the visibility to Private for this app.
  2. For the repository name, use the same name as the Docker image we created, which is hello-world-flask.
  3. Click on Create Repository.

Once the ECR repository is created, we’ll push the Docker image to AWS using the Windows command line. Follow these steps:

  1. In the Security Credentials section of the IAM user, generate the Access Key ID and Secret Access Key. These will be used for AWS credential configuration using the command aws configure on the command line.
  2. Ensure you have access to Docker by logging in via the Docker CLI from the command line. It will ask for your user credentials (Username and Password) and, upon successful authentication, will display “Login Succeeded.”
  3. Navigate to the folder containing your application, including the Dockerfile and requirements.txt.
  4. Click on the ECR repository you created and view the Push Commands. Follow the commands provided by ECR to push your Docker container to AWS ECR.

Once the Docker container is successfully pushed to ECR, you will see an image tag with the Image URI.

By Author

Now, for the third and final part of deploying and running the Docker containerized Flask application image on an AWS server, we’ll use ECS (Elastic Container Service). Follow these steps:

  1. Create the Cluster: Use AWS Fargate as the infrastructure to create the cluster.
  2. Create Task Definitions: 1) In Task Definitions, create a new task using AWS Fargate. 2) Provide the Image URI that was created during the ECR step for the Docker container. 3) Leave the rest of the configuration as default or as it is in the task definition.
  3. Run the Task: 1) Click on the cluster you created (e.g., demo-cluster2), 2) Navigate to the Tasks tab and click on the task ID, which is a unique alphanumeric value. This will open a new page with tabs like Configuration, Logs, Networking, Volumes, and Tags, 3) Click on the Networking tab, then on “Security Groups”.
  4. Configure Security Groups: You will be directed to the Security Groups page. In the Inbound Rules section, click on Edit Inbound Rules and add a rule as shown below, then save it.
By Author

By following these steps and using the public IP, you will be able to see the “Hello World” page running on your AWS server.

By Author

I hope you enjoyed this detailed, comprehensive article on deploying a Dockerized Flask web application on AWS using ECR and ECS. Please follow for more detailed articles and feel free to provide your feedback

--

--