This repository contains a Node.js REST API application deployed on AWS EC2. It includes a CI/CD pipeline for continuous integration and continuous deployment, enabling automated testing, building, and deployment processes. The project demonstrates best practices for developing and deploying scalable REST APIs with seamless integration into AWS services.
First, set up Node.js server, which will act as the backbone of our REST API. This server will handle requests, process data, and communicate with the database to ensure seamless functionality.
-
Clone the repository:
https://github.com/sasmithx/Nodejs-RESTAPI-AWS-EC2-CI-CD-Pipeline.git
This will provide a structured starting point for building and integrating your API.
Since we are using MongoDB as our database, it is essential to configure its accessibility properly. To ensure seamless connectivity from any location, update the network access settings accordingly, allowing external access while maintaining security best practices.

- Setting Up EC2 Instance and Git Repository
-
Create a new EC2 instance in your AWS account.
-
Generate or use an existing SSH key pair for accessing the instance.
-
Create a new Git repository and push your Node.js code to it.
- Connecting to EC2 Instance
-
After creating the instance and pushing code to the repository, connect to the EC2 instance via SSH using the .pem file.
ssh -i your-key.pem ubuntu@your-ec2-public-ip
- Setting Up GitHub Actions
-
Navigate to your repository settings on GitHub and select Actions.
-
Add a self-hosted runner and follow the setup instructions
- Environment Setup for GitHub Actions
-
Create a .env file with your environment variables and add them as secrets in your GitHub repository settings.
- CI/CD Workflows
- Create GitHub Actions workflows for CI/CD. Below is an example of a Node.js CI workflow.
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
name: Node.js CI/CD
on:
push:
branches: [ "main" ]
jobs:
build:
runs-on: self-hosted
strategy:
matrix:
node-version: [22.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: |
touch .env
echo "${{ secrets.MONGO_DB_URL }}" > .env
- run: pm2 restart BackendAPI
- Environment Setup in Ubuntu
sudo ./svc.sh install
sudo ./svc.sh start
Ensure Node.js and Nginx are installed on your Ubuntu instance.
sudo apt update
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs nginx
- Setting Up Nginx Reverse Proxy
Configure Nginx to act as a reverse proxy for your Node.js application.
sudo nano /etc/nginx/sites-available/default
Add the following configuration,
location /api {
rewrite ^\/api\/(.*)$ /api/$1 break;
proxy_pass http://localhost:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Restart Nginx for changes to take effect,
sudo systemctl restart nginx
- Setting Up PM2
PM2 is a process manager for Node.js applications. Install and configure it to keep your application running in the background.
sudo npm i -g pm2
pm2 start server.js --name=apiServer


- Now changes are directly applying in to the CI/CD pipeline for every new push and pull in to the main branch.

Upon successful completion of the CI process, the CD (Continuous Deployment) phase is initiated.
Distributed under the MIT License. See LICENSE for more information.