DEV Community

Shankar Thejaswi
Shankar Thejaswi

Posted on

Automating AWS Deployments with GitHub and CodePipeline using Jenkins

Introduction

Continuous Integration and Continuous Deployment (CI/CD) is a fundamental practice in DevOps that automates the deployment process, reducing manual work and improving efficiency. AWS CodePipeline is a managed CI/CD service that automates code changes to application deployment. Combined with GitHub and Jenkins, you can create a robust deployment workflow to AWS services such as S3, ECS, Lambda, or EC2.

In this blog, we'll walk through setting up a CI/CD pipeline using GitHub, Jenkins, and AWS CodePipeline to deploy an application to AWS.


Step 1: Setting Up AWS CodePipeline

** Create an S3 Bucket for Artifacts**

AWS CodePipeline requires an S3 bucket to store artifacts during deployment.

  • Go to the AWS Console → S3 → Create a new bucket.
  • Ensure you disable Block all public access (artifacts do not need to be public).
  • Note the bucket name for later use.

** Create an IAM Role for CodePipeline**

AWS CodePipeline needs permissions to access GitHub, deploy applications, and interact with AWS services.

  1. Navigate to IAM → Roles → Create Role.
  2. Select AWS Service → Choose CodePipeline.
  3. Attach the following policies:
    • AWSCodePipelineFullAccess
    • AmazonS3FullAccess
    • AWSCodeBuildAdminAccess
  4. Name the role and create it.

** Create a CodePipeline**

  1. Navigate to AWS CodePipeline → Create a new pipeline.
  2. Choose GitHub as the source provider and connect your repository.
  3. Add Jenkins as the build provider.
  4. Deploy using AWS CodeDeploy (for EC2/ECS) or another AWS service as needed.
  5. Click Create Pipeline.

Step 2: Configuring GitHub and Jenkins for AWS Deployment

** Install and Configure Jenkins**

  1. Install Jenkins on an AWS EC2 instance or a local server.
  2. Install necessary plugins:
    • AWS CodePipeline Plugin
    • Git Plugin
    • Pipeline Plugin
  3. Set up credentials for AWS within Jenkins.
  4. Create a new Jenkins Pipeline Job that fetches code from GitHub and triggers deployments.

** Add AWS Credentials to Jenkins**

  1. In Jenkins, go to Manage Jenkins → Manage Credentials.
  2. Add AWS credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) under Global Credentials.

** Create a Jenkinsfile**

Create a Jenkinsfile in your repository to define the build and deployment stages:

pipeline {
    agent any
    environment {
        AWS_ACCESS_KEY_ID = credentials('AWS_ACCESS_KEY_ID')
        AWS_SECRET_ACCESS_KEY = credentials('AWS_SECRET_ACCESS_KEY')
    }
    stages {
        stage('Checkout Code') {
            steps {
                git branch: 'main', url: 'https://github.com/your-repo.git'
            }
        }
        stage('Build') {
            steps {
                sh 'echo Building application...'
            }
        }
        stage('Deploy to AWS') {
            steps {
                sh 'aws s3 sync . s3://my-bucket-name --delete'
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Replace my-bucket-name with your actual S3 bucket name.


Step 3: Deploying an Application to AWS

Depending on your target AWS service, modify the Jenkinsfile to deploy accordingly:

** Deploy to AWS ECS (Dockerized Applications)**

        stage('Deploy to ECS') {
            steps {
                sh '''
                aws ecr get-login-password | docker login --username AWS --password-stdin <AWS_ACCOUNT_ID>.dkr.ecr.<AWS_REGION>.amazonaws.com
                docker build -t my-app .
                docker tag my-app:latest <AWS_ACCOUNT_ID>.dkr.ecr.<AWS_REGION>.amazonaws.com/my-app:latest
                docker push <AWS_ACCOUNT_ID>.dkr.ecr.<AWS_REGION>.amazonaws.com/my-app:latest
                aws ecs update-service --cluster my-cluster --service my-service --force-new-deployment
                '''
            }
        }
Enter fullscreen mode Exit fullscreen mode

Step 4: Automating Rollbacks and Monitoring

** Enable Rollbacks in CodeDeploy**

  • Use CodeDeploy Deployment Configs (CodeDeployDefault.OneAtATime) to control rollout speed.
  • Enable CloudWatch Alarms to trigger rollbacks on failure.

** Set Up Monitoring with CloudWatch**

  • Use AWS CloudWatch Logs for debugging.
  • Set up Amazon SNS Notifications for deployment alerts.

** Debugging and Troubleshooting**

  • Check Jenkins build logs for errors.
  • Use AWS CodePipeline Execution History to track failures.
  • Review CloudWatch Logs for detailed error messages.

Conclusion

By integrating Jenkins, GitHub, and AWS CodePipeline, you can automate your deployment process, ensuring faster and more reliable application updates. This setup reduces manual intervention, improves development efficiency, and enhances security.


Top comments (0)