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:
Introduction
Install AWS CLI and docker.io
Create an EC2 Instance
Clone the Repository from GitHub
Configure the Image in AWS ECR
5.1 Creating an ECR Repository
5.2 Pushing the Image to ECRConfiguring AWS ECS
6.1 Creating an ECS ClusterUsing AWS Fargate
7.1 Selecting AWS Fargate for the ClusterCreating a Task Definition
Deploying the Task to the Cluster
Opening Ports in the Security Group
Accessing the Live Application
Conclusion
So, let's start with me !!!!
Install the AWS CLI, docker.io in the AWS EC2 instance.
Create an EC2 instance in the AWS management console. Navigate to the EC2 instance.
Let's go to the GitHub repository and copy the source code url.
Clone the repository from GitHub to the EC2 instance.
Configuring image in AWS ECR
Now go to AWS ECR and create an ECR repository.
Create a repository. I have selected the public repository and set repository name.
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
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.
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
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.
Now, we will select AWS Fargate fo the cluster to run on.
Cluster is now ready!!!
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.
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.
Select the CPU and memory configuration according to the application load.
Let the rest be the default and click on next. Review the details and create the task.
Finally, task was created!!!!
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.
Finally, the task is now deployed to the cluster.
Open port in the security group.
Here is ENI ID, public IP address
Click on the ENI ID address link, then go to the security group URL.
click on network interface id number→then open this interface
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!!!!!!
- Go to the task that we created. copy the public IP and hit enter.
Boooommmmm!!!!!!!!!!!!!
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 !!!