Skip to main content

Command Palette

Search for a command to run...

Building a CI/CD Pipeline on AWS Using GitHub Actions

Published
4 min read
Building a CI/CD Pipeline on AWS Using GitHub Actions

Introduction

In this project, I built a CI/CD pipeline that automatically deploys a simple web application to an AWS EC2 instance using GitHub Actions. The goal of this project was to gain hands-on DevOps experience by connecting Git, Docker, AWS, and CI/CD automation into a real-world workflow.

This project is suitable for beginners and is a strong portfolio project for anyone aspiring to become a DevOps or Cloud Engineer.

Project Overview

What this project does:

  • Hosts a simple HTML web application

  • Dockerizes the application using NGINX

  • Pushes the code to GitHub

  • Uses GitHub Actions to automatically deploy the app to AWS EC2 whenever code is pushed

Tools & Technologies Used:

  • Git & GitHub

  • Docker

  • GitHub Actions

  • AWS EC2

  • Ubuntu Linux

  • SSH (key-based authentication)

Architecture Flow

  1. Developer pushes code to GitHub

  2. GitHub Actions workflow is triggered

  3. Workflow connects securely to EC2 via SSH

  4. EC2 pulls latest code

  5. Docker image is built

  6. Container is restarted with the new version

  7. Application becomes live automatically

Step 1: Create the Web Application

We start with a very simple web application.

Create the project directory

mkdir aws-cicd-demo
cd aws-cicd-demo

Create index.html

vim index.html
<!DOCTYPE html>
<html>
<head>
  <title>AWS CI/CD Demo</title>
</head>
<body>
  <h1>App is live 🚀</h1>
  <p>Deployed automatically using GitHub Actions</p>
</body>
</html>

This page will later confirm that deployment was successful.

Step 2: Dockerize the Application

Next, we containerize the app using Docker and NGINX.

Create Dockerfile

vim Dockerfile
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html
EXPOSE 80

Build and test locally

docker build -t aws-cicd-demo .
docker run -d -P aws-cicd-demo
docker ps

Access the mapped port in your browser to confirm it works.

Step 3: Push Code to GitHub

Initialize Git

git init
git branch -M main
git add .
git commit -m "Initial commit: Dockerized web app"

Add GitHub remote and push

git remote add origin https://github.com/<your-username>/aws-cicd-demo.git
git remote -v
git push -u origin main

Step 4: Set Up AWS EC2

Launch EC2 Instance

  • Instance type: t2.micro (Free Tier)

  • OS: Ubuntu 24.04

  • Create or select a key pair

Configure Security Group

Inbound rules:

  • SSH (22) → My IP

  • HTTP (80) → 0.0.0.0/0

This allows secure SSH access and public web access.

Step 5: Connect to EC2 ( Using Instance Connect )

Install Docker on EC2

sudo apt update
sudo apt install -y docker.io git

Step 6: Prepare SSH for GitHub Actions

GitHub Actions needs secure access to EC2.

Generate SSH key

ssh-keygen -t rsa -b 4096 -f github-actions-key

Copy public key to EC2

cat github-actions-key.pub

Add it to:

~/.ssh/authorized_keys

Add secrets to GitHub

In GitHub → Settings → Secrets → Actions:

  • EC2_HOST → EC2 Public IP

  • EC2_USER → ubuntu

  • EC2_SSH_KEY → contents of github-actions-key

Step 7: Create GitHub Actions Workflow

Create workflow file

mkdir -p .github/workflows
nano .github/workflows/deploy.yml
name: CI/CD Deploy to EC2

on:
  push:
    branches: 
      - main 

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Configure SSH
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.EC2_SSH_KEY }}" >> /~/.ssh/known_hosts

      - name: Deploy to EC2
        run: |
          ssh ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF'
            cd aws-cicd-demo || git clone https://github.com/<username of github account>/aws -cicd-demo.git
            cd aws-cicd-demo
            git pull origin main
            docker stop web || true
            docker rm web || true
            docker build -t aws-cicd-demo .
            docker run -d -p 80:80 --name web aws-cicd-demo
          EOF

Step 8: Push and Trigger Deployment

git add .github/workflows/deploy.yml
git commit -m "Add CI/CD pipeline"
git push origin main

GitHub Actions automatically triggers the deployment.

Step 9: Verify Deployment

Open your browser and visit:

http://<EC2_PUBLIC_IP>

If you see:

App is live 🚀

Then the CI/CD pipeline worked successfully.

Challenges Faced & Lessons Learned

  • GitHub PAT permissions for workflows

  • SSH key-based authentication

  • EC2 networking and security groups

  • Docker port conflicts

  • Debugging GitHub Actions logs

These challenges provided real DevOps troubleshooting experience.

This project demonstrates:

  • CI/CD automation

  • Cloud deployment

  • Linux administration

  • Docker usage

  • Secure infrastructure access

Conclusion

This project transformed basic concepts into a real production-grade DevOps workflow. It is an excellent starting point for learning advanced topics such as Kubernetes, Terraform, monitoring, and scalable cloud architectures.

If you are a beginner in DevOps, this project gives you confidence, experience, and a strong portfolio asset.

Happy deploying 🚀