Running a Flask app on a Serverless architecture using AWS ECR, AWS ECS & AWS Fargate
BY - GJS
Overview :
In this blog, we will create and push the image of our code(Flask app) into the AWS ECR repository. Then using AWS ECS and AWS Fargate we will run the image (from the ECR repository) in a serverless architecture inside the containers.
Setting up our system:
In this tutorial I will be using an ec2 instance with Ubuntu as OS in place of my machine, just to show the things needed to be installed :
Step 1: Install Docker in it. Follow this link.
Step 2: Install and configure the AWS cli. You can follow from 6th line of this link.
Step 3: Check if "git" is installed in your system or not. For Ubuntu, it's pre-installed.
Step 4: Now clone the repo - https://github.com/LondheShubham153/flask-app-ecs.git
git clone https://github.com/LondheShubham153/flask-app-ecs.git
Step 5: Now change the directory to flask-app-ecs :
cd flask-app-ecs
Creating an ECS cluster :
Go to ECS and click on Create cluster :
Give the cluster a name :
Modify the rest of the information if needed :
Creating a repository in ECR and pushing an image in it :
Go to ECR and click on Create a repository :
Fill in the necessary details like visibility and repository name :
the repository looks like this :
Open the repository and click on view push commands :
the commands look something like this :
Now copy the following command in the terminal to connect docker to ECR :
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/e8a2x2r7
Now build the image. Make sure you are in the right directory.
docker build -t flask-app-devops .
Now change the name of the image before pushing it to ECR :
docker tag flask-app-devops:latest public.ecr.aws/e8a2x2r7/flask-app-devops:latest
Now push the image to the ECR repository :
docker push public.ecr.aws/e8a2x2r7/flask-app-devops:latest
Creating and running a Task in ECS :
Click on the Task Definitions on the left panel and select Create new task definition :
Fill in the details like Task name, Container name and Image URL(From the ECR repository) :
After configuring click on Create :
Now click on Deploy and select Run Task :
Select the cluster name for the task :
Click on Create :
Before going to the URL for the running app make sure that the Security Group has port 80 enabled for anywhere.
Go to the ENI ID of task :
You will find the security group :
Configure it correctly :
Now paste the Public address of the running task in the browser :
You will see your app running inside the container at port 80 :
Thank you :
Dear readers,
I want to express my heartfelt gratitude for your continuous support and engagement with my blog. Your readership and valuable feedback have been instrumental in shaping the content and direction of my writing. Thank you for being a part of this journey and for inspiring me to keep sharing my thoughts and ideas. Your presence means the world to me, and I am truly grateful for your unwavering support.
Warmest regards, Gaurav Ji Srivastava