Deploy NodeJS REST API on ECS Fargate using AWS CodePipeline
Tags: codepipeline, codebuild, ecs, aws
Published: 2024-01-04 09:56:37


_In this guide, we'll walk through Deploy NodeJS REST API on ECS Fargate using AWS CodePipeline._

=> **Create nodejs app using express framework**

- Create folder and run npm init.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gs4g0y2wumv8ls760gi.png)

=> **Will be using Express framework to deploy the Node.js-based REST API.**

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

=> **Create an index.js file and paste the code into the current directory**

```
'use strict'

const express = require('express');
const client = require('prom-client');
// Create a Registry to register the metrics
const register = new client.Registry();
client.collectDefaultMetrics({ register });
const PORT = 8080;
const HOST = '0.0.0.0';

const app = express();

// Define the route for the root path ("/")
app.get('/', (req, res) => {
    const sentence = 'Welcome to the Automation world';

    // Send the sentence as the response
    res.json({ sentence });
});

app.get('/metrics', async(req, res) => {
    res.setHeader('Content-Type', register.contentType);
    res.send(await register.metrics());
});

app.get('/ping', (req, res) => {
    res.status(200).json({ message: "pong" })
});
app.get('/error', (req, res) => {
    // Simulating an internal server error (500)
    const error = new Error('Internal Server Error');
    res.status(500).json({ error: error.message });
});
app.listen(PORT, HOST);
console.log('running on http://${HOST}:${PORT}')
```

=> **Add Dockerfile to build the nodejs app**

```
FROM node:16.18

WORKDIR /usr/src/app

COPY . .

RUN npm ci --only=production

EXPOSE 8080

USER node

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

=> **Run the 'docker build' command to create the Docker image.**

```
docker build -t nodeapp:v1 .
```

========================================================

**AWS CodeBuild Setup**

1. Create Repository in AWS ECR
2. Setup Source Provider
3. Create service role

* Creating ECR repository with name nodeapp
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mcwfv6o8p5j15s6rtpjc.png)

* Configure the Source Provider and authenticate using the GitHub provider.(Authenticate by generating a personal access token in GitHub. Navigate to GitHub's Developer Settings to create the token)

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


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

* Grant ECR permissions to the CodeDeploy service role.

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

* CodeBuild has successfully pushed the image to ECR
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0m0gp5yoz7iz5zy47tnh.png)


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

========================================================

**AWS Code deploy Setup**

* Create ECS Cluster

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

* Create Task definition in AWS ECS

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

* Create Service in AWS ECS

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

* Retrieve the IP address from the task definition and access it via the browser by pasting the IP

```
http://13.235.73.88:8080/
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a7qw7yw3cveabgsq96z3.png)

```
http://13.235.73.88:8080/ping
```


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

========================================================


**AWS Code Pipeline Setup**
* Enter the pipeline name in step 1 and in other steps  fill up the source,build and deploy stage.

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



* AWS Code pipeline status

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


If you prefer a video tutorial to help guide you through the process to setup AWS CodePipeline with ECS

{% youtube -Vb45v637aM %}

Happy by Deploying NodeJS Rest API in AWS ECS !