Deploy Node.js app on Amazon ECS and Fargate

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.

Prerequisites:

  1. 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.
  2. 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.”
  3. 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
Deploy Node.js app on Amazon ECS and Fargate 1

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
Deploy Node.js app on Amazon ECS and Fargate 2
  • 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
Deploy Node.js app on Amazon ECS and Fargate 3

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
Deploy Node.js app on Amazon ECS and Fargate 4

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.”

Deploy Node.js app on Amazon ECS and Fargate 5
Deploy Node.js app on Amazon ECS and Fargate 6

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.

Deploy Node.js app on Amazon ECS and Fargate 7

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
Deploy Node.js app on Amazon ECS and Fargate 8

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 .
Deploy Node.js app on Amazon ECS and Fargate 9

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
Deploy Node.js app on Amazon ECS and Fargate 10
Deploy Node.js app on Amazon ECS and Fargate 11

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.

Deploy Node.js app on Amazon ECS and Fargate 12

Step#8:Create Task Definitions

Deploy Node.js app on Amazon ECS and Fargate 13
Deploy Node.js app on Amazon ECS and Fargate 14
Deploy Node.js app on Amazon ECS and Fargate 15

Step#9:Run New Tasks

Deploy Node.js app on Amazon ECS and Fargate 16
Deploy Node.js app on Amazon ECS and Fargate 17
Deploy Node.js app on Amazon ECS and Fargate 18
Deploy Node.js app on Amazon ECS and Fargate 19

Step#10:Create Service in AWS Fargate

Deploy Node.js app on Amazon ECS and Fargate 20
Deploy Node.js app on Amazon ECS and Fargate 21
Deploy Node.js app on Amazon ECS and Fargate 22
Deploy Node.js app on Amazon ECS and Fargate 23

If everything works out, verify the browser should return the same response as from our local.

Step#11:Access Node.js APP on browser

Deploy Node.js app on Amazon ECS and Fargate 24

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:

AWS Create EC2 Instance from Snapshot

Top 50 Terraform Cloud Interview Questions and Answers

Akash Bhujbal

Hey, I am Akash Bhujbal, I am an aspiring DevOps and Cloud enthusiast who is eager to embark on a journey into the world of DevOps and Cloud. With a strong passion for technology and a keen interest in DevOps and Cloud based solutions, I am driven to learn and contribute to the ever-evolving field of DevOps and Cloud.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap