Day 85 - Deploying a Node.js App on AWS ECS Fargate with ECR πŸŒπŸš€

Nilkanth MistryNilkanth Mistry
5 min read

Welcome to Day 85 of the #90DaysOfDevOps Challenge

Today, we’ll embark on an exciting project where we will deploy a Node.js app using AWS ECS Fargate and AWS ECR. This project will allow us to leverage the power of Amazon ECS (Elastic Container Service) and ECR (Elastic Container Registry) to efficiently manage and deploy containerized applications.

Project Description πŸ“œ

In this project, our goal is to deploy a Node.js application on AWS ECS Fargate and store its Docker image in AWS ECR. Amazon ECS (Elastic Container Service) is a fully managed container orchestration service that enables you to easily run, scale, and secure Docker containers on AWS. AWS Fargate is a serverless compute engine for containers, allowing you to focus on deploying your applications without having to manage the underlying infrastructure.

Hands-on Project: Deploying a Node.js App on AWS ECS Fargate and ECR πŸ› οΈ

Step 1: Create EC2 Instance and Install AWS CLI and Docker πŸ–₯️

Create an EC2 instance and install the AWS Command Line Interface (CLI) and Docker. Use the following script and provide it via User Data to install at boot:

#!/bin/bash
echo "AWS CLI Installation"
echo "------------------------------------------------------------------"
sudo apt install -y unzip
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
echo "Docker installation"
echo "------------------------------------------------------------------"
sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker ubuntu
sudo reboot

Step 2: Clone the GitHub Repository πŸ“‚

Obtain the Node.js application from the GitHub repository provided. Clone it to your AWS EC2 instance where we will be configuring AWS ECR.

git clone https://github.com/samsorrahman/node-todo-cicd.git

Step 3: Configure AWS ECR 🐳

Navigate to the AWS Elastic Container Registry (ECR) and create a repository. Choose the repository type and name it accordingly. Select the necessary operating systems and versions that align with the Fargate setup.

Step 4: Set Up IAM πŸ”‘

Create an IAM user in the AWS Management Console and attach the required policies for our project. Make sure the user has permissions for ECS, ECR, and other related services.

Step 5: Configure AWS CLI πŸ”§

Install the AWS Command Line Interface (CLI) on the AWS EC2 instance where the Node.js app resides. Connect the EC2 instance to the AWS Management Console using the AWS CLI.

aws configure

Step 6: Push the Image to ECR πŸ“¦

Navigate to the ECR repository created earlier and select β€œView push commands”. Use the following steps to authenticate and push an image to your repository.

  1. Retrieve an authentication token and authenticate your Docker client to your registry:

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

  2. Build your Docker image:

     docker build -t estebanmorenoit/node-todo-cicd .
    

  3. Tag your image:

     docker tag estebanmorenoit/node-todo-cicd:latest public.ecr.aws/d2v6u5n5/estebanmorenoit/node-todo-cicd:latest
    
  4. Push the image to your AWS repository:

     docker push public.ecr.aws/d2v6u5n5/estebanmorenoit/node-todo-cicd:latest
    

Step 7: Configure AWS ECS πŸ–₯️

Move on to the AWS Elastic Container Service (ECS) repository in the AWS console. Create a cluster with a relevant name, and choose the Virtual Private Cloud (VPC) and subnet where you want your application to be available. Select AWS Fargate as the launch type for the cluster.

Step 8: Create Task Definition πŸ“‹

Create a task definition for your cluster. Provide the task name, container image details, and the ports for the application to run on. Make sure you have exposed a port in your Dockerfile.

Step 9: Review and Create a Task βœ…

Review all the details and create the task definition. Once created, the task will be ready for deployment.

Step 10: Deploy and Run the Task πŸš€

Click on the β€œDeploy” button, and then choose β€œRun task” to deploy the task on the cluster. Select the cluster, and set the launch type to Fargate. Confirm the deployment.

Verify the task is up and running. πŸŽ‰

Step 11: Open the Port in the Security Group πŸ”“

Ensure port 8000 is open in the Security Group used in our task.

Step 12: Project Live Execution 🌐

Finally, navigate to the task that was created, and take note of the public IP. Your Node.js app is now live and accessible! 🌟


πŸŽ‰ Congratulations on completing Day 85 of the #90DaysOfDevOps challenge! πŸŽ‰

In this project, we successfully deployed a Node.js app on AWS ECS Fargate and ECR, leveraging the power of AWS cloud services. This project showcased how to efficiently manage containerized applications with AWS services, ensuring high availability and scalability while reducing operational overhead.

Stay tuned for tomorrow’s challenge, where we’ll dive into another exciting project! 🎯


I hope you learned something from this blog. If you did, don’t forget to follow and click the clap πŸ‘ button below to show your support πŸ˜„. Subscribe to my blogs so that you won’t miss any future posts. πŸ“¬

If you have any questions or feedback, feel free to leave a comment below. Thanks for reading and have an amazing day ahead! 🌞

πŸ“Œ Connect with me on LinkedIn

πŸ“Œ Check out my work on GitHub

0
Subscribe to my newsletter

Read articles from Nilkanth Mistry directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nilkanth Mistry
Nilkanth Mistry

Embark on a 90-day DevOps journey with me as we tackle challenges, unravel complexities, and conquer the world of seamless software delivery. Join my Hashnode blog series where we'll explore hands-on DevOps scenarios, troubleshooting real-world issues, and mastering the art of efficient deployment. Let's embrace the challenges and elevate our DevOps expertise together! #DevOpsChallenges #HandsOnLearning #ContinuousImprovement