Published on

Deploying a Node.js Application to AWS using GitHub Actions

Authors

Deploying a Node.js Application to AWS using GitHub Actions

This tutorial will guide you through the process of deploying a Node.js application to AWS EC2 using GitHub Actions.

Prerequisites

  • Node.js application ready for deployment
  • AWS account and AWS EC2 instance set up
  • GitHub repository with your Node.js application

Step 1: Setting up GitHub Actions Workflow

Create a .github/workflows/deploy.yml file in your repository with the following content:

name: Deploy Node.js App to AWS EC2

on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v1
        with:
          node-version: '14'

      - name: Install Dependencies
        run: npm install

      - name: Build
        run: npm run build

      - name: Deploy to AWS EC2
        run: |
          scp -r * ec2-user@<EC2-IP-ADDRESS>:/path/to/your/app
          ssh ec2-user@<EC2-IP-ADDRESS> "pm2 restart all"
        env:
          KEY: ${{ secrets.SSH_PRIVATE_KEY }}

Replace <EC2-IP-ADDRESS> with your actual EC2 instance's IP address.

Step 2: Configuring AWS and GitHub

  • Configure your AWS EC2 instance to allow SSH access.
  • Add your private SSH key to GitHub secrets as SSH_PRIVATE_KEY.

Step 3: Push Changes and Auto-Deploy

  • Make changes to your application and push to the main branch.
  • GitHub Actions will automatically deploy your application to AWS EC2.

Conclusion

You have successfully set up a CI/CD pipeline using GitHub Actions to deploy your Node.js application to AWS EC2.

This MDX content outlines the steps and provides a basic workflow file for the deployment process. You can expand on each section with more details as needed for your specific application and setup. Remember to replace placeholders with your actual data and credentials (securely handled, of course). This tutorial assumes familiarity with GitHub Actions, AWS EC2, and SSH key management.

Source: (1) Deploy your (Node.JS) app to AWS EC2 with GitHub Actions. https://blog.devops.dev/deploy-your-node-js-app-to-aws-ec2-with-github-actions-edec5ef1e694. (2) Deploy your Node App to EC2 with Github Actions - Medium. https://medium.com/@ikbenezer/deploy-your-node-app-to-ec2-with-github-actions-364df98d9918. (3) Deploy your Node App to EC2 with Github Actions. https://dev.to/stretch0/deploy-your-node-app-to-ec2-with-github-actions-h9a. (4) Continuous Deployment with GitHub Actions, DockerHub, and AWS EC2 — A .... https://blog.devops.dev/continuous-deployment-with-github-actions-dockerhub-and-aws-ec2-a-hands-on-tutorial-b01656a27963.