DEV Community

Arunodhayam for itTrident Software Services

Posted on • Updated on

Deploy NodeJS application on AWS ECS (EC2 Launch Type)

In this post, we will build the docker image for a nodejs application and deploy it onto AWS ECS (EC2 Launch type)

Prerequisites

#1: Create a simple Node app

  • Create a directory and navigate to it
mkdir node-app
cd node-app
Enter fullscreen mode Exit fullscreen mode
  • Initialize packages
npm init --y
Enter fullscreen mode Exit fullscreen mode
npm install express
Enter fullscreen mode Exit fullscreen mode
  • Enter this block of code into an index.js file
const express = require('express');
const app = express();
app.get('/', (req,res) => {
    res.send("This is ECS deployment")
});
app.listen(8080,() => {
    console.log("Server started")
});
Enter fullscreen mode Exit fullscreen mode
  • Issue the below command to run the application locally
node index.js
Enter fullscreen mode Exit fullscreen mode

Image description

#2: Dockerize Node app

  • Next, we create a Dockerfile in the project root to build an image out of
# Use a Node runtime as a parent image
FROM node: alpine
# Set the working directory to /app
WORKDIR /app
# Copy package.json and package-lock.json to the working directory
COPY package*.json ./
# Install any needed packages specified in package.json
RUN npm install
# Copying the rest of the code to the working directory
COPY . .
# Make port 8080 for the application port
EXPOSE 8080
# Run index.js for application expose for the container
CMD [ "node", "index.js"]
Enter fullscreen mode Exit fullscreen mode

#3: Build and Push the docker image to AWS ECR

  • Navigate to ECR service in your AWS Console

Image description

  • Click on Create repository in top right corner to create the repo

Image description

  • Create either a private or public repo based on your need

Image description

  • Once the repository is created, click view push commands from inside to push the image

Image description

  • You can from the instructions, infer the commands to log in to AWS ECR, build the image, tag the build, and push the tagged image to AWS ECR.

Image description

  • Finally, your pushed image should look like this

Image description

#4: Create the ECS Cluster

  • Click on Clusters from the ECS console sidebar

Image description

  • Choose EC2 Linux + Networking as the cluster template

Image description

  • Input the instance and networking configuration

Image description

  • The cluster then is created and cloudformation by default takes care of launching the rest of the resources

Image description

#5: Create a Task Definition

Task Definitions in ECS sort of acts as a blueprint of how an application should be deployed

  • Click on Task Definitions right under Clusters from the ECS console sidebar and click on Create new Task Definition.

Image description

  • Select EC2 as the launch type

Image description

  • Assign the Name, Task role, and Network mode of the container

Image description

  • vCPU and Memory count is optional, as we have opted for the EC2 launch type. Click Add container once filled.

Image description

  • Now, time to configure the container: Container name, Image; copy the AWS ecr image URI, specify the Memory limit - 300MB and above, and set Port mapping to the given application port

Image description

  • Now your task definition is ready to be used in tandem with the Service definition

Image description

#6: Create Service

  • Navigate to the Cluster tab, scope in on the service tab, and click Create

Image description

  • Configure the service with launch type EC2, choose the Task definition we have created, and Service type as Replica.

Image description

  • Configure the VPC within which the ECS service should go. We are selecting None on Load balancing and Service discovery, as we aren't aiming for a larger scale.

Image description

  • Configure auto-scaling if need be

Image description

Do a quick review glance once all the configuration is done

Image description

  • Clicking the services tab will give you info on running containers and their configurations

Image description

  • Navigate to the Tasks tab and you'll find the task

Image description

  • Here you can view the details of the task

Image description

  • Click the drop-down next to the container name to reveal the External Link

Image description

And there we have it

Image description

The NodeJS application is successfully deployed on AWS ECS.

Top comments (0)