Deploying a flask application image through AWS ECR to AWS Fargate and ECS

Deploying a flask application image through AWS ECR to AWS Fargate and ECS

In this article, we will walk you through the process of deploying a Flask application on AWS EC2 using AWS ECR (Elastic Container Registry), AWS Fargate, and AWS ECS (Elastic Container Service). We will cover the installation of necessary tools, creating an EC2 instance, cloning the GitHub repository, configuring the image in AWS ECR, pushing the image to ECR, configuring AWS ECS, using AWS Fargate as the launch type, creating a task definition, deploying the task to the cluster, opening ports in the security group, and launching the application. By following these steps, you will be able to deploy your Flask application and make it accessible to users.

           ┌───────────────────────┐                 ┌───────────────────────┐
                 GitHub                                  AWS ECR          
               Repository                              (Container        
                                                        Registry)         
           └────────────┬──────────┘                 └────────────┬──────────┘
                                                                    
                         Push Docker image                          
                                                                    
                                                                    
             ┌───────────────┐                           ┌─────────────────┐
                 AWS EC2                                   AWS ECS      
                 Instance                                (Fargate/ECS)  
             └───────────────┘                           └─────────────────┘
                                                                    
                         Deploy Docker image                       
                                                                    
                                                                    
             ┌───────────────┐                           ┌─────────────────┐
                User's                                   Flask App      
                Browser                                  (Container)    
             └───────────────┘                           └─────────────────┘

Table of Contents:

  1. Introduction

  2. Install AWS CLI and docker.io

  3. Create an EC2 Instance

  4. Clone the Repository from GitHub

  5. Configure the Image in AWS ECR
    5.1 Creating an ECR Repository
    5.2 Pushing the Image to ECR

  6. Configuring AWS ECS
    6.1 Creating an ECS Cluster

  7. Using AWS Fargate
    7.1 Selecting AWS Fargate for the Cluster

  8. Creating a Task Definition

  9. Deploying the Task to the Cluster

  10. Opening Ports in the Security Group

  11. Accessing the Live Application

  12. Conclusion

So, let's start with me !!!!

  1. Install the AWS CLI, docker.io in the AWS EC2 instance.

  1. Create an EC2 instance in the AWS management console. Navigate to the EC2 instance.

  2. Let's go to the GitHub repository and copy the source code url.

  3. Clone the repository from GitHub to the EC2 instance.

Configuring image in AWS ECR

  1. Now go to AWS ECR and create an ECR repository.

  2. Create a repository. I have selected the public repository and set repository name.

  3. Select all the operating systems and versions according to the OS of Fargate you would select. Finally, create the repository.

Now, you can check the public repository for the created repository.

Pushing the image to ECR

  1. Navigate to the AWS ECR and view the push commands. Earlier, we had pushed the image to DockerHub in my previous blogs. The steps and concepts are similar but the platform differs. Generally in the IT industry, ECR is used as an image registry.

  2. Execute these 4 commands in the EC2 instance.

    Command 1: This will take the ecr token and provide it to docker for the connection to the repository.

    Command 2: This will build the image from the Dockerfile.

    Execute commands 3 and 4 in EC2 instance.

    Command 3: This tags the image that we have built using docker.

    Command 4: This command will push the image from the EC2 instance to ECR.

After the image push, you can check the ECR for the image.

Configuring AWS ECS

  1. Now configure the ECS cluster in the AWS console and create a cluster.

Provide the Cluster name, VPC and subnet you want your application to be available on.

Using AWS Fargate

One of the major disadvantages of using AWS EC2 for an application is we have to create multiple instances in case of an application load increase that will have a cost impact.

To avoid this, we will use AWS Fargate which is a serverless technique of providing the specification of applications that will be automatically managed without spinning off any instance.

  1. Now, we will select AWS Fargate fo the cluster to run on.

  2. Cluster is now ready!!!

  3. We used to run docker run to create a container in the EC2 instance out of the docker image. That was nothing but a task. Therefore let's create a task definition for our cluster.

  4. Provide the task name, Container image details and ports for the application to be run on. We must have exposed a port in our Dockerfile.

  1. Select the CPU and memory configuration according to the application load.

  2. Let the rest be the default and click on next. Review the details and create the task.

  1. Finally, task was created!!!!

  2. Click on deploy and then Run task to run the task on the cluster we had created.

    Choose the cluster and Select the launch type to be Fargate.

    Finally, click on to Create button, that's it.

  1. Finally, the task is now deployed to the cluster.

    Open port in the security group.

  2. Here is ENI ID, public IP address

  3. Click on the ENI ID address link, then go to the security group URL.

  4. click on network interface id number→then open this interface

  5. click to security group ip address →

    Navigate to the Inbound rule in the security group and open the Port 80 which is HTTP and select anywhere or you can only access your self by select My IP

Project is now live server!!!!!!

  1. Go to the task that we created. copy the public IP and hit enter.

Boooommmmm!!!!!!!!!!!!!

  1. You can see our application is live!!!!!!!

In conclusion, deploying a Flask application on AWS EC2 using AWS ECR, AWS Fargate, and AWS ECS offers a scalable and efficient solution for hosting your application. By utilizing containerization and serverless technologies, you can easily manage and deploy your application without the need for managing underlying infrastructure. We hope this article has provided you with a comprehensive guide to successfully deploying your Flask application on AWS EC2. Enjoy running your application on the AWS cloud!

Thank you for reading this blog. I hope you learned something new today! If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future.

— Happy Learning !!!

Let’s connect !!!

Linkedin

Github

Mail

Medium