π 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 EC2EC2_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!
Subscribe to my newsletter
Read articles from Tathagat Gaikwad directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
