Day 76: Project-5 (AWS ECS & ECR)

Day 76: Project-5 (AWS ECS & ECR)

Node JS app deployment on ECS & ECR

Introduction

This project revolves around the deployment of a Node.js application on Amazon Web Services (AWS) using Elastic Container Service (ECS) Fargate and Elastic Container Registry (ECR). It's a hands-on exploration of how to effectively run containerized applications in the cloud.

In this blog, we'll dive into the nitty-gritty of setting up ECS Fargate to host your Node.js app. We'll also utilize ECR to store and manage container images. Throughout the journey, we'll cover tasks like defining task definitions, configuring networking, and ensuring scalability and high availability.

This project will provide insights into best practices, resource optimization, and the orchestration of containerized apps on AWS. The end goal is a robust and well-structured deployment of your Node.js application. Stay tuned for an in-depth exploration of where AWS ECS Fargate and ECR will be your essential allies in the deployment process.


What is AWS ECS?

Amazon Elastic Container Service (Amazon ECS) is a fully managed container orchestration service provided by Amazon Web Services (AWS). It simplifies the deployment, management, and scaling of containerized applications using Docker containers. ECS is designed to make it easier to run, stop, and manage Docker containers on a cluster.


Difference between ECS and Fargate

While both services are used for managing containerized applications, they have different approaches and use cases:

AWS ECS is a container orchestration service that allows you to run, stop, and manage Docker containers on a cluster of EC2 instances. You need to manage the EC2 instances' scaling, patching, and maintenance giving you more control over the infrastructure.

On the other hand, AWS Fargate simplifies the management of containers by abstracting the infrastructure, letting you focus on your application without the need for server management. This makes it an excellent choice for deploying and scaling containerized apps with ease, especially for developers and teams who want to focus on building and running containers without worrying about the underlying servers. It's a serverless option for container workloads.


What is AWS ECR?

ECR stands for Amazon Elastic Container Registry. It is a fully managed container registry service provided by Amazon Web Services (AWS). In simple terms, it is the DockerHub of Amazon.

ECR is designed to store, manage, and deploy Docker container images. It integrates seamlessly with other AWS services like Amazon Elastic Kubernetes Service (EKS), Amazon Elastic Container Service (ECS), and AWS CodeBuild, making it easier to build, store, and deploy containerized applications in AWS.


Task-01

  • Get a NodeJs application from GitHub.

  • Build the Dockerfile.

  • Setup AWS CLI and AWS Login to tag and push to ECR

  • Setup an ECS cluster

  • Create a Task Definition for the node js project with an ECR image

  • Run the Project

Step 1: Clone app from GitHub

Initialize git on the local machine or in the EC2 instance machine and clone the files from GitHub using the below commands:

git init
git clone <git_url>

Step 2: Build an image using Dockerfile

Using the Dockerfile cloned from GitHub, we can build the image by using the below command:

docker build . -t nodejs

Step 3: Install AWS CLI

Go to the terminal of your local machine or the EC2 instance and install AWS CLI using the command.

sudo apt install awscli

Step 4: Configure AWS CLI

To configure AWS CLI, we will need AWS access key and the Secret Access key. For more detailed information, go to the blog- Day 51: AWS Access Keys & CLI

To configure, use the command

aws configure

Step 5: Create a Repository

Go to Amazon Elastic Container Registry and click on "Get Started"

Enter all the details such as General settings--> Public

Enter your desired Repository name

Select the OS version as per your requirements

You can add "About" & "Usage" descriptions which are optional

Proceed further to "Create Repository"

We have created our ECR repository.

Step 6: Push the Image to ECR

To push the created image from local to ECR, view the push commands located at the top right as mentioned below:

A set of instructions will pop up

To retrieve an authentication token and authenticate your Docker client to your registry use the command from the instructions:

aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/k7c0s1k0

As per the instructions you can proceed further to build the image using the below command

docker build -t name_of_your_repo .
# In this case we already executed the below command previously
# docker build -t nodejs .

OR

If you have built the image, skip this step and follow the next steps:

i.e. tag the image so the image can be pushed to repository. Make sure to enter your desired tag name.

docker tag nodejs:latest public.ecr.aws/k7c0s1k0/my_first_repo:latest

To push the image to the repository, use the command

docker push public.ecr.aws/k7c0s1k0/my_first_repo:latest

We have our first image in the repository

To sum up the above steps, we have retrieved the files from GitHub, created an image, configured AWS CLI and pushed the image to ECR. Moving further with ECS.

Step 7: Configure ECS

Go to AWS ECS and create a Cluster.

Enter the name of the cluster. In this case: DevCluster

Select the infrastructure as per the requirements.

You can refer to the start of this blog to understand the concept and the difference between ECS & Fargate.

In this case, we will use the serverless model- Fargate

While "Monitoring" & "Tags" are optional, you can proceed further to create the Cluster.

Creation is in progress.

The progress can be viewed in CloudFormation.

We have created the Cluster successfully.

Step 8: Create a Task Definition for the cluster

Just as we are required to run a container using an image, in the same way, we need to create a task definition for the cluster.

Go to Task definitions --> Create a new task definition

Enter the unique task definition name

Enter the Name, Container Image URI, and Port Mapping (the exposed port should match with the one in the Dockerfile that was used to create an image i.e. 8000)

Allocate resources such as CPU, GPU, Memory hard limit, Memory soft limit

Scroll down and proceed further to create the task leaving the rest of the fields as default values.

We have successfully created a Task Definition.

Step 9: Execute the task

Run the task we created.

Select the Cluster and the launch type as "Fargate"

Scroll down and proceed further to create the task leaving the rest of the fields as default values.

We have successfully launched a task.

After some time, we achieve the desired state of the cluster

Step 10: The application is live

Once the desired state is achieved, select the task --> Configuration --> click on ENI ID

Click on Security groups

Update the inbound rules and set it to port: 8000

Go Back and copy <Public_IP_Address>:8000 and paste it on the web browser.

The application has been deployed successfully.


Conclusion:

In this comprehensive guide, we explored the world of Amazon Web Services (AWS) to deploy a Node.js application using Elastic Container Service (ECS) Fargate and Elastic Container Registry (ECR). We navigated through the ins & outs of configuring ECS Fargate, creating a task definition, and running containerized applications in the cloud. This hands-on exploration provided insights into best practices and resource optimization, ensuring a robust deployment of the Node.js application on AWS.

Hope you like my post. Don't forget to like, comment, and share.