Table of contents
No headings in the article.
Navigate to the ECS service by searching for "ECS" in the AWS Management Console search bar or by locating it under the "Compute" section.
In the ECS dashboard, click on the "Create Cluster" button to start creating a new cluster
Provide a name for your cluster and just select the default VPC
In infrastructure, AWS Fargate (serverless) is selected by default.
In Monitoring, enable Cloudwatch for getting logs. (Optional) and Click "Create"
A cluster is created.
The next step is to Create a "Task Definition".
After creating the cluster, you'll need to define and configure task definitions and services to run your containers on the ECS cluster
But, First, we will create ECR for uploading Docker Images.
In the ECR dashboard, click on the "Create repository" button to start creating a new repository.
Select the repository type - private or public and Provide a name for your repository.
Select the OS and architecture for image compatibility. Here I just selected all.
Click "Create repository"
Once the repository is created, you can view it in the ECR dashboard.
Now if you go to your Repository, (it is empty).
The next, step is to go to IAM And select the user and give or attach ECS and ECR full access permission, I have also given some additional permission for other previous projects
Also, get the Access key for the user to connect to the EC2 instance for CLI (command line interface).
Now, Copy the git clone URL from the GitHub repository to get the code to your system.
Code Credit: Shubham Londhe
Connect your EC2 Instance and Connect your User with the access key by Running aws configure
command.
Next, Git Clone the code
As this is a serverless project we only need the EC2 instance to clone the code and build the docker image and push it to ECR.
Next, In ECR Select the "View push commands"
You will see these 4 commands, Next
In Your EC2 instance - Install Docker and give Docker permission to the user using the - sudo usermod -aG docker $USER
and reboot the system after granting permission so that the system updates the changes.
After doing all that - run all the commands one after another in your EC2 instance from ECR. Commands as follows:
1- log in with the console
2- Build a docker image
3- Tag the Docker image
4- Push the docker image to the ECR repository
Docker Image is being pushed to the ECR repository.
Next, Select "View public Listing" - on the top right of the screen
We will get an Image URL, Copy the URL
Next, Go Back to ECS - Task definition - "Create new task definition"
Provide the task name and in the container section - give - the container name and paste the Image URL from ECR.
Environment - select- AWS Fargate and OS Linux and, CPU and memory (default) And Create.
Task Definition is created - For Deploy - Select - Deploy option - Run task
Select the ECS cluster that we create and every setting is the default.
Now for confirmation copy the default subnet in the Task definition and check it in the VPC if the required ports are open. (optional)
In VPC the subnet security group shows "all traffic ". But just in case I added port 80 for our Flask app as our code exposes port 80 for deployment.
Back to the Task definition deployment page. If everything is good then Click "Create"
Your Tasks launched and might take a few minutes to run. that's it
the task is running and if go inside the task.
And copy the public IP from the "configuration" option
and run the IP address with port 80 in a new tab. And as we can see our application is running.
And Remember that we opt for cloud watch while creating the ECS cluster. So, now we go to Cloud Watch And check the logs in the "logs group"
That's it, we have successfully deployed our Flask application using ECS and ECR using AWS Fargate (serverless).
Thank you for taking the time to read my blog! I appreciate your interest and hope you found the information valuable.