Skip to main content
📝 Cloud & DevOps (AWS)

AWS CodeDeploy: How to Set Up a CI/CD Pipeline to Deploy Applications on EC2 Using GitHub

AWS CodeDeploy: How to Set Up a CI/CD Pipeline to Deploy Applications on EC2 Using GitHub Deploying applications manually is tedious, error-prone, and...

3 min

Tiempo de lectura

537

Palabras

Mar 23, 2025

Publicado

Engr Mejba Ahmed

Escrito por

Engr Mejba Ahmed

Compartir Artículo

AWS CodeDeploy: How to Set Up a CI/CD Pipeline to Deploy Applications on EC2 Using GitHub

AWS CodeDeploy: How to Set Up a CI/CD Pipeline to Deploy Applications on EC2 Using GitHub

Deploying applications manually is tedious, error-prone, and inefficient. AWS CodeDeploy makes automating deployments straightforward, enhancing efficiency and minimizing downtime. In this guide, you'll learn step-by-step how to set up a robust Continuous Integration and Continuous Deployment (CI/CD) pipeline to deploy your applications directly from GitHub to an Amazon EC2 instance.

What You'll Need:

  • AWS Account
  • EC2 instance running Amazon Linux
  • GitHub Repository with your application
  • AWS CLI installed on your local machine

Step 1: Prepare Your EC2 Instance

Connect to your EC2 instance via SSH:

ssh -i "your-key.pem" ec2-user@your-ec2-instance-public-ip

Install and start the CodeDeploy agent:

sudo yum update
sudo yum install ruby -y
sudo yum install wget -y

cd /home/ec2-user
wget https://aws-codedeploy-region.s3.region.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto

sudo service codedeploy-agent status

Replace region with your AWS region, such as us-east-1.


Step 2: Create an IAM Role for CodeDeploy

In AWS Console:

  • Navigate to IAM → RolesCreate Role.
  • Choose AWS service, then select CodeDeploy.
  • Attach policies: select AWSCodeDeployRole.
  • Name your role (e.g., CodeDeployRole) and create it.

Step 3: Set Up the CodeDeploy Application

  • Go to AWS CodeDeploy → Applications → Create application.
  • Enter an application name.
  • Choose compute platform: EC2/On-premises.

Create a deployment group:

  • Deployment group name (e.g., Production)
  • Select your IAM Role (CodeDeployRole)
  • Select EC2 instances by tag or manually select your EC2 instance.
  • Deployment type: In-place.

Step 4: Prepare Your GitHub Repository

Create an appspec.yml file at your repository root:

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/html
hooks:
  AfterInstall:
    - location: scripts/install_dependencies.sh
      timeout: 300
      runas: root

Create necessary scripts, e.g., scripts/install_dependencies.sh:

#!/bin/bash
sudo yum update -y
sudo yum install -y httpd
sudo service httpd start
sudo chkconfig httpd on

Make sure your scripts have execute permissions:

chmod +x scripts/*.sh

Commit and push changes to GitHub:

git add .
git commit -m "Added appspec.yml and deployment scripts"
git push origin main

Step 5: Connect AWS CodeDeploy with GitHub

  • In CodeDeploy console, select your application and deployment group.
  • Click Create Deployment.
  • Select GitHub as your repository type.
  • Connect to GitHub and authorize AWS.
  • Choose your repository and commit branch (e.g., main).
  • Click Deploy.

Step 6: Automate with CI/CD Using GitHub Actions

Create .github/workflows/deploy.yml in your repo:

name: AWS Deploy

on:
  push:
    branches:
      - main

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

      - name: AWS CodeDeploy
        uses: aws-actions/aws-codedeploy@v1
        with:
          application-name: your-codedeploy-app-name
          deployment-group-name: your-deployment-group
          region: your-region
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

Set AWS credentials as GitHub secrets:

  • Go to GitHub → Settings → Secrets and variables → Actions → New repository secret
  • Add your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.

Now, every push to main triggers automatic deployments.


Step 7: Verify Your Deployment

Visit your EC2 public IP in a web browser:

http://your-ec2-instance-public-ip

You should see your application deployed successfully!


Conclusion

Congratulations! You've successfully set up an AWS CodeDeploy CI/CD pipeline integrated with GitHub to automatically deploy applications on EC2. This automation will significantly streamline your deployment process, reduce downtime, and simplify updates.

Coffee cup

¿Te gustó este artículo?

Tu apoyo me ayuda a crear más contenido técnico detallado, herramientas de código abierto y recursos gratuitos para la comunidad de desarrolladores.

Temas Relacionados

Engr Mejba Ahmed

Sobre el Autor

Engr Mejba Ahmed

Engr. Mejba Ahmed builds AI-powered applications and secure cloud systems for businesses worldwide. With 10+ years shipping production software in Laravel, Python, and AWS, he's helped companies automate workflows, reduce infrastructure costs, and scale without security headaches. He writes about practical AI integration, cloud architecture, and developer productivity.

Discussion

Comments

0

No comments yet

Be the first to share your thoughts

Leave a Comment

Your email won't be published

4  +  7  =  ?

Seguir Aprendiendo

Artículos Relacionados

Ver Todos

Comments

Leave a Comment

Comments are moderated before appearing.

Learning Resources

Expand Your Knowledge

Accelerate your growth with structured courses, verified certificates, interactive flashcards, and production-ready AI agent skills.

Sample Certificate of Completion

Sample certificate — complete any course to earn yours