Deploy Node.js on AWS EC2 with CI/CD | Complete GitHub Actions Automation Tutorial

Deploy Node.js on AWS EC2 with CI/CD | Complete GitHub Actions Automation Tutorial

Understanding CI/CD with Node.js Deployment on AWS

Introduction to CI/CD

  • Tushar introduces the video, focusing on how Continuous Integration and Continuous Deployment (CI/CD) works.
  • The video aims to demonstrate deploying a Node.js application using CI/CD practices with AWS EC2 instances and GitHub Actions.

Setting Up the Environment

  • Tushar logs into the AWS console, referencing familiarity with the dashboard from previous videos.
  • He navigates to GitHub to access a repository named "DevOps" and instructs viewers to download a code zip file for the backend application.

Initial Application Setup

  • A quick overview of the default app structure is provided, highlighting an Express app that requires running npm install due to initial errors.
  • After installation, he runs the application locally on port 8000, confirming its functionality through Google Chrome.

Configuring AWS EC2 Instance

  • Tushar prepares a configuration file for deployment and explains TypeScript conversion requirements during build processes.
  • He initiates creating an EC2 instance by selecting Ubuntu as the operating system for better familiarity compared to Amazon Linux.

Security Group Configuration

  • During instance setup, he generates a new key pair for SSH access and creates a security group allowing HTTP traffic from anywhere.
  • After launching the instance, he demonstrates connecting via SSH using terminal commands while ensuring proper permissions are set.

Installing Necessary Packages

  • Once connected to the EC2 instance, Tushar updates package lists and installs necessary software packages including Node.js.
  • He emphasizes installing specific versions of Node.js globally using npm commands tailored for production environments.

Deploying Application Code

  • With Node.js installed, he proceeds to install Nginx as a web server due to its efficiency over Apache.
  • Tushar checks if Nginx is working correctly by accessing its default page through public DNS of the EC2 instance.

Setting Up Application Directory

  • He creates directories in which his Express app will reside and initializes Git within this directory before pushing code from local development.

Creating GitHub Repository

  • Tushar walks through creating a new repository on GitHub specifically for CI/CD purposes and pushes local changes there.

Configuring Nginx as Reverse Proxy

  • Instructions are given on configuring Nginx as a reverse proxy so that it can forward requests appropriately from port 80 to where Node.js runs (port 8000).

Finalizing Deployment Steps

  • After setting up configurations in Nginx, he restarts services ensuring all changes take effect before testing again in browser.

Implementing PM2 for Process Management

  • To ensure reliability in production environments, Tushar installs PM2 which helps manage application processes effectively even after reboots or crashes.

Setting Up Secrets in GitHub Actions

  • Finally , he discusses adding secrets like EC2 host IP , username ,and SSH key into GitHub settings necessary for automating deployments via actions .

This structured approach provides clarity on each step involved in deploying a Node.js application using CI/CD principles while leveraging AWS services effectively.

Setting Up the Build Process

Initial Steps for Code Checkout

  • The process begins with checking out the code from the repository to ensure that the latest version is being used.
  • It is essential to set up Node.js and cache npm to avoid repeated installations, streamlining the build process.
  • Dependencies must be installed using a clean installation approach alongside CI (Continuous Integration).

Creating Deployment Packages

  • A deployment package is created without node modules, ensuring a clean environment for deployment.
  • The system allocates a temporary machine on Ubuntu OS where all actions will be performed during the build.

Managing Deployment Files

Organizing Source Code

  • All source code and packages are organized into a designated directory for deployment.
  • Temporary storage provided by Git allows for easy management of deployment files, which are automatically deleted after one day unless retention settings are adjusted.

Uploading Build Artifacts

  • After successful builds, artifacts are uploaded to facilitate future deployments. This ensures that only necessary files are retained.

Executing Deployment Jobs

Dependency Management

  • The deployment job depends on successful completion of the build job; it cannot proceed until this prerequisite is met.
  • Downloading built packages from artifacts is crucial before deploying them onto EC2 instances.

SSH Configuration

  • Proper SSH configuration requires verifying secrets in GitHub Actions to ensure secure connections to EC2 hosts.

Backup Procedures During Deployment

Creating Backups

  • A backup of the current version should be made before deploying new changes, allowing for rollback if issues arise.
  • Only five recent backups should be kept in order to manage storage effectively while still providing recovery options.

Finalizing Deployments and Rollbacks

Verifying Successful Deployments

  • Post-deployment verification checks whether applications run correctly on EC2 instances using public IP addresses or domain names.

Rollback Mechanism

  • If any issues occur post-deployment, there’s an established mechanism to roll back to the last stable version quickly.

Automating CI/CD Workflows

Triggering Builds Automatically

  • Pushing code triggers automatic builds and deployments without manual intervention, enhancing efficiency in development workflows.

Monitoring Build Statuses

  • Continuous monitoring of logs helps identify any potential issues during builds or deployments, ensuring smooth operations.

Implementing Changes and Testing

Making Code Adjustments

  • Developers can make adjustments such as adding new routes or features directly within their applications before pushing updates live.

Testing New Features Locally

  • Local testing of new features ensures they function correctly before being deployed into production environments.

Multi Branch Support in Deployments

Handling Multiple Branches

  • Developers can trigger deployments across multiple branches based on specific requirements or conditions set within their CI/CD configurations.
Video description

Deploy your Node.js application on AWS EC2 automatically using a complete CI/CD pipeline powered by GitHub Actions. This tutorial covers the entire process from configuring your AWS EC2 server, setting up SSH and workflow secrets, writing a GitHub Actions pipeline, and deploying code without manual intervention. Perfect for developers who want production-grade automated deployments and real DevOps experience. REPO Link 👇:- https://github.com/TusharVashishth/Devops/tree/main/nodejs-backend You can Follow me on 👇 *Twitter :-*. https://x.com/tushardotcom29 *Linkedin :-* https://www.linkedin.com/in/tushar-vashishth-b15998172/ *Instagram :* https://www.instagram.com/tushar.vashishth #cicd #githubactions #aws #awsec2 #nodejs #devops #cloudcomputing #automation #deployment #softwareengineering #backenddeveloper #programming #techtutorial