DEV Community

Cover image for Deploying a Node.js Express API on Amazon ECS (Elastic Container Service)
Pavithra Sandamini
Pavithra Sandamini

Posted on

Deploying a Node.js Express API on Amazon ECS (Elastic Container Service)

Greetings from a cloud trip, fellow devs! Installing programs that are reliable and scalable is crucial in the current fast-paced web development industry. One of the best methods to do this is to orchestrate your application on the cloud using containerization. This post will explore the fascinating world of utilizing Amazon Elastic Container Service (ECS) to install a Node.js Express API. Delivering a Node.js Express API involves building a Docker container, publishing it to a container registry, and then deploying it to Amazon ECS (Elastic Container Service). The general instructions for it are located here. But first, there are a few prerequisites that you need to fulfill. These are outlined below.

Prerequisites:
AWS CLI:
Ensure that your local machine have the AWS CLI installed because it will help to mange via command line.

Docker:
Make sure you have Docker latest version installed in your local machine to build your Docker image.

Step 1:
The very first step to create the docker file. Here I created a sample docker file that matches my Node.js Express application.
Here's the code for the my docker file.

`Copy code
FROM node:14

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["node", "app.js"]`

Step 2:
After creating your docker file, build it locally. I will add the command below.

docker build

This command will create a docker image for your application. And then push your docker image into AWS ECR. I added all the steps for that in below. SO, now follow it with me.

Step 3:
To push your image into ECR we will create a repository on AWS ECR. I added the code lines in creating ECR repo using CLI.

aws ecr create-repository --repository-name your-repository-name
After creating the repo, you should build and tag your docker image using the code below.

docker build -t your-repository-name:tag .
So, now you have tagged your image. After that authenticate docker into your ECR repo. For that,

aws ecr get-login-password --region your-region | docker login --username AWS --password-stdin your-account-id.dkr.ecr.your-region.amazonaws.com
Hureeeh, now you have successfully authenticated your docker image into ECR repo. Then, push the docker image into ECR repository using command below.

docker tag your-repository-name:tag your-account-id.dkr.ecr.your-region.amazonaws.com/your-repository-name:tag
docker push your-account-id.dkr.ecr.your-region.amazonaws.com/your-repository-name:tag

Next, create a job definition in YAML or JSON that includes the RAM, CPU, Docker image, and any environment variables. Here, I've included a few JSON-formatted task definition files.

{
"family": "your-task-family",
"containerDefinitions": [
{
"name": "your-container",
"image": "your-account-id.dkr.ecr.your-region.amazonaws.com/your-repository-name:tag",
"portMappings": [
{
"containerPort": 3000,
"hostPort": 3000
}
],
"essential": true
}
]
}

Step 4:
The ECS cluster will be created as the next step.#:~:text= Launch the ECS console%20in,template%20select%20EC2%20Networking%20%2B%20Linux%).
Next, use the task specification you created to execute your ECS task.

aws ecs run-task --cluster your-cluster-name --task-definition your-task-family

Make an Application Load Balancer (ALB) and link it to your ECS service if your API needs external access.

Use the ALB URL to test your Node.js Express API when the task has completed and the service has been connected to the ALB.

Don't forget to include your actual values in placeholders like your-repository-name, your-region, your-account-id, your-task-family, your-cluster-name, and others. Furthermore, modify parameters based on the requirements of your application.
So, now you have successfully deployed your API in ECR. I hope you all learned a lot. Stay tuned for more...

Top comments (0)