Deploy NodeJS application on AWS ECS (EC2 Launch Type)
Tags: aws, ecs, containers, ec2launchtype
Published: 2022-10-06 12:08:57


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

## Prerequisites

- [NodeJS](https://nodejs.org/en/download/)
- [Docker](https://docs.docker.com/engine/install/)
- [AWS cli](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)
- [AWS Account](https://console.aws.amazon.com/console/home)

## #1: Create a simple Node app
- Create a directory and navigate to it

```sh
mkdir node-app
cd node-app
```
- Initialize packages

```javascript
npm init --y
```
- Install [ExpressJS](https://expressjs.com/)

```javascript
npm install express
```
- Enter this block of code into an `index.js` file

```Javascript
const express = require('express');
const app = express();
app.get('/', (req,res) => {
    res.send("This is ECS deployment")
});
app.listen(8080,() => {
    console.log("Server started")
});
```

- Issue the below command to run the application locally

```javascript
node index.js
```
- You can now view the application at http://localhost:8080

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/svyackptxryjhluyim3n.png)

## #2: Dockerize Node app

- Next, we create a `Dockerfile` in the project root to build an image out of

```Dockerfile
# 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"]
```

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

- Navigate to ECR service in your AWS Console

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u6qti7362n3d2rzz2wad.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fqzkckbc59qmsihwuw12.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/co4nqh164aeex6y9zikc.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/snygfir51iqi5x9pontg.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9e5ve9bmec1j1zyo6ipt.png)

- Finally, your pushed image should look like this

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eya6tpocuvyklm1xv44f.png)

## #4: Create the ECS Cluster

- Click on `Clusters` from the ECS console sidebar

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oyb4qlvpva9k3uiniwtb.png)

- Choose `EC2 Linux + Networking` as the cluster template

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6swcoeyi35wvqxnfpkui.png)

- Input the instance and networking configuration

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzj8u9i35yymn5hb3l1z.jpg)

- The cluster then is created and [cloudformation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) by default takes care of launching the rest of the resources

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rkaqvmez9shmt7vvyank.png)

## #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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4yyh7gjrkhw4xtr2m6jo.png)

- Select `EC2` as the launch type

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/alan7hjw97tjausy3dal.png)

- Assign the `Name`, `Task role`, and `Network mode` of the container

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/stir7jbclw9f74j4xlb1.png)

-  [vCPU and Memory](https://aws.amazon.com/premiumsupport/knowledge-center/ecs-cpu-allocation/) count is optional, as we have opted for the EC2 launch type. Click `Add container` once filled.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6qmwnhxfacr9c0dguxwk.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/knxrkvc2j64upf8n65vz.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xweq8x9lflxkeg2lwzz2.png)

## #6: Create Service

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wsjwm1cz4q62l8wr5vk3.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f0gs8s0g4wqrg7bz4mk3.jpg)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gz0565pvz2nmfcftdrhs.jpg)

- Configure auto-scaling if need be

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j8hxpadlaxfynjwfgpel.png)

Do a quick review glance once all the configuration is done

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lb6zizryikx6oida48lu.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n3o8hetruha167j20426.png)

- Navigate to the `Tasks` tab and you'll find the task

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/81qxlj40cdudt0iq0ijg.png)

- Here you can view the details of the task

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g0kmpjub14qhu5hfy0v4.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o8rf8iihdeqszn8nuyfp.png)

And there we have it

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hdqb6kv8gi7pzn8yoejk.png)

The NodeJS application is successfully deployed on AWS ECS.