Deploying a Node.js App on AWS ECS Fargate and AWS ECR

Deploying a Node.js App on AWS ECS Fargate and AWS ECR

Project Description

The project involves deploying a Node JS app on AWS ECS Fargate and AWS ECR. The project aims to demonstrate the process of deploying a Node.js application using AWS ECS Fargate and AWS ECR, showcasing a serverless infrastructure approach. This involves creating a Docker container for the Node.js app, storing the container image in AWS ECR (Elastic Container Registry), and running the containerized application on AWS ECS (Elastic Container Service) with Fargate, which allows for running containers without having to manage servers or clusters.

Step 1: Launch an EC2 Instance

  1. Create a new EC2 instance with user data that includes commands to install AWS CLI and Docker.
#!/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

Clone the GitHub repository containing your Node.js application to your AWS EC2 instance where you will configure AWS ECR.

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

Step 3: Configure AWS ECR

To create an ECR Repository, open the AWS Management Console and navigate to Amazon ECR. Click "Create repository". Name your repository and configure settings as needed. Then click "create creation".

Step 4: Create an IAM role and attach the required policies

Step 5: Configure AWS CLI

Install AWS CLI on the instance. After installation, run the aws configure command to set up your credentials.

aws configure

Step 6: Push the Image to ECR

Navigate to the ECR repository created earlier and select "View push commands".

To push an image to the ronke/node-todo-app ECR repository, follow these steps:

  1. Retrieve an authentication token and authenticate your Docker client to your registry. Use the AWS CLI:
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/e2b3s2m7

  1. Build your Docker image using the following command.
docker build -t ronke/node-todo-app .

  1. After the build completes, tag your image so you can push the image to this repository:
docker tag ronke/node-todo-app:latest public.ecr.aws/e2b3s2m7/ronke/node-todo-app:latest

  1. Run the following command to push this image to your newly created AWS repository:
docker push public.ecr.aws/e2b3s2m7/ronke/node-todo-app:latest

Step 7: Configure AWS ECS

From the AWS Management Console, navigate to the Elastic Container Service (ECS). Click on the "Create repository". Provide a name for your repository and any additional configuration. Then click on the "Create repository" to create the repository. Select AWS Fargate as the launch type.

Step 8: Create Task Definition

Define task definitions by specifying your container images, CPU, memory, and the container ports for the application to run on.

Step 9: Review and Create

Review the task definition configuration to ensure everything is set up correctly and then click on the "Create" button to create the task definition. Once the task definition is created, it can be used to run tasks within your ECS cluster.

Step 10: Deploy Task:

Click on "Deploy", and then click on "Run task" to deploy your application as ECS task.

Step 11: Verify the task is running

Step 12: Open the Port in the Security Group

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

Step 13: Navigate to the created task public IP address

Thank you for reading.