DEV Community

Cover image for Deploy NodeJS REST API on ECS Fargate using AWS CodePipeline
Ravindra Singh for AWS Community Builders

Posted on

9 1

Deploy NodeJS REST API on ECS Fargate using AWS CodePipeline

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

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

Image description

=> 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}')


Enter fullscreen mode Exit fullscreen mode

=> 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"]


Enter fullscreen mode Exit fullscreen mode

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



docker build -t nodeapp:v1 .


Enter fullscreen mode Exit fullscreen mode

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

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

  • 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

Image description

  • Grant ECR permissions to the CodeDeploy service role.

Image description

  • CodeBuild has successfully pushed the image to ECR Image description

Image description

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

AWS Code deploy Setup

  • Create ECS Cluster

Image description

  • Create Task definition in AWS ECS

Image description

  • Create Service in AWS ECS

Image description

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


http://13.235.73.88:8080/


Enter fullscreen mode Exit fullscreen mode

Image description



http://13.235.73.88:8080/ping


Enter fullscreen mode Exit fullscreen mode

Image description

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

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

  • AWS Code pipeline status

Image description

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

Happy by Deploying NodeJS Rest API in AWS ECS !

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Best Practices for Running  Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK cover image

Best Practices for Running Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK

This post discusses the process of migrating a growing WordPress eShop business to AWS using AWS CDK for an easily scalable, high availability architecture. The detailed structure encompasses several pillars: Compute, Storage, Database, Cache, CDN, DNS, Security, and Backup.

Read full post

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay