In this article, Deploy Node.js app on Amazon ECS and Fargate. | discover a step-by-step guide to deploying a robust Node.js web server on AWS, harnessing the capabilities of ECS and Fargate for efficient containerized deployment.
Table of Contents
Prerequisites:
- An AWS Account: Begin by setting up an AWS account. If you don’t have one yet, you can easily create one and take advantage of the AWS free tier for the first year.
- Fundamental understanding of AWS, Node.js, Docker, image, and container creation, along with familiarity with ECR (Elastic Container Registry), ECS (Elastic Container Service), and Fargate.”
- Create a EC2 instance of (Ubuntu 22.04 LTS)on AWS and Install Nodejs and Docker
Steps for implementing Node.js app on Amazon ECS and Fargate
Step#1:Create Simple Node.js App on Ubuntu 22.04 LTS
- Install Nodejs on Ubuntu 22.04 LTS
sudo apt-get install -y nodejs
sudo apt-get install npm
sudo npm install express --save
- Create a app.js file:
sudo nano app.js
- Edit the file that consists of all the files and dependencies required that describe your app and name it as a “package. jason”.
{ "name": "express_app", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { "start": "node app.js", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "express": "^4.18.2", "nodemon": "^3.0.2" } }
- Now, initialize the node project using the following command.
sudo npm init
- Install the Express library and add it to the package.json file as a dependency.
sudo npm install --save express
sudo npm install --save nodemon
- Create a app.js file that defines a web app using the Express.js framework:
// import and create an express app const express = require('express'); const app = express() // message as response msg = "Hello world! this is nodejs in a docker container.." // create an end point of the api app.get('/', (req, res) => res.send(msg)); // now run the application and start listening // on port 3000 app.listen(3000, () => { console.log("app running on port 3000..."); })
This code will start up web server by using the express
and return the “Hello world! this is nodejs in a docker container..” this is nodejs in a docker container..” when page get request with port 3000.
You can test the page by type the node app.js
at the command console.
- At this stage, we can run our application on our local system using the following command:
npm run start

Step#2:Creating a Dockerfile for Node.js app
- First, create a file name as Dockerfile.
sudo vi Dockerfile
FROM node:latest WORKDIR /app COPY package.json /app RUN npm install COPY . /app CMD ["npm", "start"] EXPOSE 3000
Step#3:Build Docker Image for Node.js app
- After creating a Dockerfile build an image using the help Dockerfile by using the following command.docker build -t docker-container-nodejs .
sudo docker build -t docker.container.nodejs .
- Confirm that the image has been created.
docker images

- Run the image
docker run -d -p 3000:3000 -v address_to_app_locally:/app docker.container.nodejs
- Edit the inbound rules of the intsance

Step#4:Verify local Node.js app on Browser
Type below http://(your_ip_address:3000)
to see if able to get response from the container Node.js, once if we get the same , “Hello world! this is nodejs in a docker container..”means the docker image/container works on the local and ready to setup image/container to AWS.
curl http://your_ip_address:3000

Step#5:Create Public Repository at AWS ECR
Up to this point, we’ve crafted a Docker image locally and created a container, exposing port 3000 as 3000 to the calling site. Now, let’s replicate the process in the AWS cloud. We’ll start by generating a Docker image within Amazon Elastic Container Registry (ECR), a dedicated image repository where we can effortlessly push and manage our images. The flexibility allows us to configure the image’s visibility, choosing between public or private accessibility.”


Step#6:Push Docker Image to AWS ECR(Public Repo)
Once create the AWS ECR Repository, click and go inside then you won’t see any image as below. Here we’ll push our docker image at here.

AWS ECR already has list of command can help to push image, click the View push commands you should see the detail of how to use AWS cli to push local image to the ECR.
First, we’ll exist below command to authenticate, once you get successful, you should see Login Succeeded.
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/j9m2z7n7

Second, we’ll build our docker image, if already use docker build image built locally then can skip this part.
docker build -t docker.container.nodejs .

Next, we’ll tag our image with ESR’s repository
docker tag docker.container.nodejs:latest public.ecr.aws/j9m2z7n7/docker.container.nodejs:latest
Lastly, we’ll push our image to the AWS ECR
docker push public.ecr.aws/j9m2z7n7/docker.container.nodejs:latest


Step#7:Create Cluster on AWS ECS
We’ll close to finish, so far we have our docker image at ECR, which is expose port as 3000, so start from here we’ll create clusters, task to run the docker image.

Step#8:Create Task Definitions



Step#9:Run New Tasks




Step#10:Create Service in AWS Fargate




If everything works out, verify the browser should return the same response as from our local.
Step#11:Access Node.js APP on browser

Conclusion:
In conclusion, this step-by-step guide empowers you to seamlessly establish a powerful Node.js web server on AWS using ECS and Fargate. By combining containerization and cloud orchestration, you’ve unlocked a scalable and efficient infrastructure to deploy and manage your Node.js applications. Take this newfound knowledge and elevate your web hosting capabilities on the AWS cloud.
Reference:-
For reference visit the official website Amazon Elastic Container Service
Related Articles: