πŸš€ Day 20 of #30DaysOfDevOps - Simple CI/CD Pipeline to AWS EC2 using SSH

Series: 30 Days DevOps Interview Preparation
Author: Tathagat Gaikwad

🌟 Introduction

Deployments are at the heart of DevOps. Traditionally, teams used to copy code manually to servers and restart services β€” a process that was slow and error-prone.

Today, we’ll implement a simple Continuous Integration and Continuous Deployment (CI/CD) pipeline that pushes code changes from GitHub β†’ EC2 instance using SSH.

This method is beginner-friendly and helps you understand the basics of CI/CD before moving on to advanced setups with tools like Jenkins, GitHub Actions, or AWS CodePipeline.


🧠 Theory: What is CI/CD?

  • Continuous Integration (CI) β†’ Automatically build and test code when developers push changes.

  • Continuous Deployment (CD) β†’ Automatically deploy code to servers after CI succeeds.

Why CI/CD with EC2 + SSH?

  • βœ… Simple setup for beginners.

  • βœ… Works well for small projects.

  • βœ… No extra tools required (just SSH access).

  • βœ… Great way to practice before moving to Jenkins, Docker, Kubernetes, etc.


⚑ Practical: CI/CD Pipeline to EC2 using SSH

We’ll set up a pipeline where pushing code to GitHub automatically deploys it to an AWS EC2 instance using SSH.

πŸ”Ή Step 1: Launch an EC2 Instance

  • Launch an EC2 instance (Ubuntu recommended).

  • Attach a Security Group allowing SSH (port 22) and your app port (e.g., 3000, 80).

  • Download your .pem key.

πŸ”Ή Step 2: Configure the EC2 Instance

SSH into the instance:

ssh -i mykey.pem ubuntu@<EC2_PUBLIC_IP>

Install dependencies (example: Node.js app):

sudo apt update -y
sudo apt install git -y
sudo apt install nodejs npm -y

Clone the repo once:

git clone https://github.com/<your-username>/<your-repo>.git
cd <your-repo>
npm install

πŸ”Ή Step 3: Generate SSH Key for GitHub

On your local machine (or CI server):

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Copy the public key to GitHub β†’ Repo Settings β†’ Deploy Keys β†’ Add Key.


πŸ”Ή Step 4: Setup Deployment Script on EC2

Create a script deploy.sh inside your EC2:

#!/bin/bash
cd /home/ubuntu/<your-repo>
git pull origin main
npm install
pm2 restart app || pm2 start app.js --name app
echo "Deployment successful!"

Make it executable:

chmod +x deploy.sh

πŸ”Ή Step 5: Automate Deployment from GitHub Actions

Inside your repo β†’ .github/workflows/deploy.yml:

name: CI/CD to EC2 via SSH

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Deploy to EC2
        uses: appleboy/ssh-action@v0.1.10
        with:
          host: ${{ secrets.EC2_HOST }}
          username: ubuntu
          key: ${{ secrets.EC2_SSH_KEY }}
          script: |
            cd /home/ubuntu/<your-repo>
            git pull origin main
            npm install
            pm2 restart app || pm2 start app.js --name app

πŸ”Ή Step 6: Store GitHub Secrets

Go to GitHub repo β†’ Settings β†’ Secrets and variables:

  • EC2_HOST β†’ Public IP of EC2

  • EC2_SSH_KEY β†’ Contents of your .pem file


βœ… Now Test the Pipeline

  • Push code changes to main branch.

  • GitHub Actions will connect to your EC2 via SSH.

  • Your app will update automatically! πŸŽ‰


πŸ” Best Practices

  • Use PM2 (or systemd) to keep the app running.

  • Don’t allow 0.0.0.0/0 for SSH β†’ restrict to your IP.

  • For production β†’ move to AWS CodeDeploy, Jenkins, or GitHub Actions with Docker.


🎯 Interview Questions & Answers

Q1: Why use SSH for CI/CD to EC2?
πŸ‘‰ Simple and direct for beginners, no need for extra AWS services.

Q2: What are the limitations of this method?
πŸ‘‰ Not scalable, manual key management, not ideal for microservices.

Q3: How would you improve this setup?
πŸ‘‰ Use CodeDeploy, Ansible, or Docker with ECS/Kubernetes for production-grade CI/CD.


🏁 Conclusion

In this blog, we built a basic CI/CD pipeline that deploys code from GitHub β†’ EC2 via SSH.

This is the foundation step in your DevOps journey. Once you master this, you can scale to more advanced CI/CD tools like:

  • Jenkins Pipelines

  • AWS CodePipeline

  • GitHub Actions with Docker/Kubernetes

πŸš€ Next time, we’ll enhance this pipeline with automation + monitoring.

πŸ‘‰ What’s your current deployment setup β€” manual, SSH, or advanced CI/CD? Drop your thoughts in the comments!

0
Subscribe to my newsletter

Read articles from Tathagat Gaikwad directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tathagat Gaikwad
Tathagat Gaikwad