DEV Community

Nathan (Nursultan) Bekenov
Nathan (Nursultan) Bekenov

Posted on

Deploying a Node.js application in ECS with CodeCatalyst and Terraform

In this step-by-step guide, I'll walk you through the entire process of deploying a Node.js application in ECS Fargate with CodeCatalyst as CI/CD tool and Terraform as IaC. From setting up your environment to configuring your containers and services, we'll cover everything you need to know to get your application up and running in no time. So whether you're new to cloud deployment or just looking for a more streamlined process, this guide is for you. Let's get started!

Prerequisites
You will need the following :

  • NPM to build and run the app locally
  • Docker to build and test your image
  • Terraform to create infrastructure in AWS
  • AWS account + AWS Builder ID (for CodeCatalyst)

Note:
If you interested only in part of Creating CI/CD pipeline in CodeCatalyst please go to step # 4

Step 0 [Optional] Build and run app locally.

Feel free to skip if you already have existing node.js app.
Create the following files:

  • src/app.js main app file in the src folder
  • package.json that includes dependencies
// Import the express module const express = require('express'); // Create an instance of express const app = express(); // Define a port const port = 3000; // Define a route app.get('/', (req, res) => { res.send('Hello, World!'); }); // Start the server app.listen(port, () => { console.log(`Server is running at http://localhost:${port}`); }); 
Enter fullscreen mode Exit fullscreen mode

My example code

Now you can build and run your app locally

npm install npm run build npm test npm start 
Enter fullscreen mode Exit fullscreen mode

Step 1 - Containerize the app

Let's create Dockerfile

# Use an official Node.js runtime as a parent image FROM node:20-alpine # set the current mode ENV NODE_ENV=production # Set the working directory to /app WORKDIR /usr/src/app # Copy the application code to the container COPY --chown=node:node . . # Install the dependencies RUN npm ci --only=production # Expose the port that your application listens on EXPOSE 3000 USER node # Start the application CMD ["node", "src/app.js"] 
Enter fullscreen mode Exit fullscreen mode

Now you can run your app in Docker

docker build -t test:latest . docker run -p 3000:3000 test:latest 
Enter fullscreen mode Exit fullscreen mode

Step 3 - Create Infra

We are going to implement the following architecture using Terraform and open source modules.

Image description

First you will need to bootstrap your account:

  • create S3 and DynamoDB table for Terraform State file
  • create ECR
  • create IAM roles and policies for CodeCatalyst

More details in the readme

Then IaC for app

Step 4 - create Pipeline

  1. create AWS Builder ID
  2. Log into CodeCatalyst and create new space and project
  3. Connect AWS accounts
  4. Create environments

Top comments (0)