Deploy a NodeJS App to Amazon ECS and Fargate

This blog demonstrates deploying a simple application to Amazon ECS and Fargate. Amazon Elastic Container Service (ECS) is a fully managed container orchestration service that helps you to deploy, manage, and scale containerized applications more efficiently. This blog assumes that you have all the prerequisites in place.

Prerequisites 

  • AWS Account.
  • EC2 Instance with Docker and AWS CLI installed.

Clone the Source Code from GitHub

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

image

2. Build the Docker Image

The source code includes a Dockerfile. Build the docker images from the docker file by navigating to the source code directory and running the command below: sudo docker build -t todoapp .

image

3. Create an Amazon Elastic Container Registry

Now, login to the AWS Management Console, go to Elastic Container Registry and create a public repository that will hold the docker image created in Step 2. Click Create Repository.

image

image

4. Push the Docker Image to ECR

After creating the repository, follow the steps below to authenticate and push the docker image to the ECR repository that you just created. Get the commands from the ECR repository: Retrieve an authentication token and authenticate your Docker client to your registry. Use the AWS CLI:

image

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

image

  • Tag your image so you can push the image to this repository: sudo docker tag todoapp:latest public.ecr.aws/r5h2p7m7/todoapp:latest image

  • Run the following command to push this image to your newly created AWS repository: sudo docker push public.ecr.aws/r5h2p7m7/todo-app:latest

    image

Our image has been pushed to Amazon Elastic Container Registry image

5. Create a Cluster in the Amazon Elastic Container Service

Now we need to create the ECS Cluster where our service will reside. In the AWS Management Console, navigate to Elastic Container Service and create a Cluster select AWS Fargate (serverless) infrastructure. Provide the cluster name and select AWS Fargate, then click the Create button.

image

The cluster has been successfully created. image

6. Create a Task Definition

Under the Amazon ECS console, navigate to Task Definitions and click Create new task definition.

  • Enter the task definition name image

  • Under Infrastructure requirements, select AWS Fargate launch type, Linux/X86_64 Operating System architecture. For task size, select 1 vCPU and 2GB Memory. Select None for the task role and Create new role under the Task execution role section. image

  • Specify a name, container image, and whether the container should be marked as essential. Each task definition must have at least one essential container. Add port mappings to allow the container to access ports on the host to send or receive traffic. Leave everything as default and click Create. image

7. Deploy the Task definition and Create service

Under the Environment section, select an existing cluster that we created earlier and leave everything as default.

image

Under the Deployment configuration section, Specify what type of application you want to run provide the service name, and click Create.

image

The Service has been created and is up and running.

image