Running a Flask app on a Serverless architecture using AWS ECR, AWS ECS & AWS Fargate

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 :

  1. Go to ECS and click on Create cluster :

  2. Give the cluster a name :

  3. Modify the rest of the information if needed :

Creating a repository in ECR and pushing an image in it :

  1. Go to ECR and click on Create a repository :

  2. Fill in the necessary details like visibility and repository name :

    the repository looks like this :

  3. Open the repository and click on view push commands :

    the commands look something like this :

  4. 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
    

  5. Now build the image. Make sure you are in the right directory.

     docker build -t flask-app-devops .
    

  6. 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
    

  7. 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 :

  1. Click on the Task Definitions on the left panel and select Create new task definition :

  2. Fill in the details like Task name, Container name and Image URL(From the ECR repository) :

  3. After configuring click on Create :

  4. Now click on Deploy and select Run Task :

  5. Select the cluster name for the task :

  6. Click on Create :

  7. 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 :

  8. Now paste the Public address of the running task in the browser :

  9. 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

0
Subscribe to my newsletter

Read articles from Gaurav Ji Srivastava directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gaurav Ji Srivastava
Gaurav Ji Srivastava

Hi!! my name is Gaurav Ji Srivastava and I am a DevOps follower. Currently I am learning the different tools and practices of DevOps and I love to write about the same.