Integration of AWS CodeDeploy with Jenkins

30 / Aug / 2016 by Parag Gupta 0 comments

We have been working on a scenario where we want to automate testing, build, deploy and revert in one Jenkins job. Currently, we are using separate Jenkins jobs for testing, deploying and reverting the code changes. We would be automating these tasks using AWS CodeDeploy with Jenkins.

Jenkins can provide us the functionality to run the test cases whenever there is a change in the application code and AWS CodeDeploy can automate the deployment process on the servers. Integration of Jenkins and AWS CodeDeploy can automate the whole process .

AWS CodeDeploy still doesn’t provide us the functionality to rollback if there is a deployment failure and rolling back to a successful revision is a painful task. But using a script and different plugins provided by Jenkins we can also automate this process.

Use Case

Integrating AWS CodeDeploy with Jenkins for automating testing, build, deployment as well as Automatic Rollback in a case of Deployment Failure.


  • At least one AWS EC2 running instance with CodeDeploy agent configured on it
  • A GitHub account with admin access and a repository where the updated code gets pushed
  • A running Jenkins server that will act as a Continuous integration tool and can be accessible publicly over the internet
  • A configured AWS CodeDeploy application. AWS CodeDeploy service is being used as a Continuous Deployment tool
  • Necessary plugins that need to be installed in Jenkins are:
    • AWS CodeDeploy Plugin for Jenkins
    • GitHub plugin
    • Post-Build Script Plug-in


  • Whenever a change in the application code is pushed to Github than a Jenkins job will get triggered that will run test cases on the updated code.
  • If the test case fails then it will further stop the post-build actions
  • If the test cases are successful then It will go to post build actions and trigger AWS CodeDeploy
  • AWS CodeDeploy will deploy the new revision of the application on each server.
  • After the deployment process, another post build action will get triggered that will check if the deployment status is failed then the previous successful revision gets deployed.

In order to configure AWS CodeDeploy application you can refer to any of the following blogs:

In order to configure Jenkins job for our task, follow the steps mentioned below:

1. Now, we have to create a new Job:job1


2. Then select GitHub project and provide the repo URL:job2


3. In SCM select Git and provide Repository URL and Login Credentials:job4


4. In Build Triggers select Build when a change is pushed to GitHub:job6


5. In Build select Add build step  –> Execute Shell  and provide command to test the updated
application code or execute a script containing test cases:job15


6. In Post-build Actions select Add post-build action  –> Deploy an application to AWS CodeDeploy:


7. Then we need to provide the following:

  • AWS CodeDeploy Application Name
  • AWS CodeDeploy Deployment Group
  • AWS CodeDeploy Deployment Config
  • AWS Region
  • S3 Bucket : Provide the bucket name where you want the AWS CodeDeploy plugin to send the zip file.
  • S3 Prefix Provide your directory name under the S3 bucket.
  • Use Access/Secret keys Provide AWS Access Key and AWS Secret Key.



8. In Post-build Actions select Add post-build action  –> Execute a set of scripts  –> Build steps  –>Execute Shell:


9. Provide the path where the script is present on the server:


10. The script provided here will perform rollback in case of deployment failure i.e. in a case of failure it will deploy the previous successful revision. The script is written in Python and uses boto3 so Python should be installed and boto3 package needs to be available on the server where Jenkins is installed:

  • #!/bin/python
    import boto3
    import time
    client = boto3.client('codedeploy', region_name='us-east-1', aws_access_key_id='xxxxxxxxxxxxxxxxxxxx', aws_secret_access_key='xxxxxxxxxxxxxxxxxxxx')
    # The time revision will take for deployment in seconds.
    response5 = client.list_deployments(
    print response5['deployments'][0]
    last_success_Id = response5['deployments'][0]
    response3 = client.get_deployment(
    print response3['deploymentInfo']['status']
    Status = response3['deploymentInfo']['status']
    if (Status == 'Failed'): 
     response4 = client.create_deployment(
     'revisionType': response3['deploymentInfo']['revision']['revisionType'],
     's3Location': {
     'bucket': response3['deploymentInfo']['revision']['s3Location']['bucket'],
     'key': response3['deploymentInfo']['revision']['s3Location']['key'],
     'bundleType': response3['deploymentInfo']['revision']['s3Location']['bundleType'],
     'eTag': response3['deploymentInfo']['revision']['s3Location']['eTag']
    print response4['deploymentId']
  • We have to provide Access KeySecret Key, Application Name and Deployment Group in the above script.
  • The script will give the Deployment Id, Status of the current deployment revision and if in a case of deployment,  Status is Failed then previous successful revision gets deployed.

In order to setup GitHub and Webhook follow the below-mentioned steps:

  1. Go to GitHub repo then select Settings  –> Webhook and Services tab  –> Add Service  –> Jenkins (GitHub Plugin)
  2. Add the following as the Jenkins hook URL:
    •  http://JENKINS.SERVER.IP.ADDRESS:8080/github-webhook/
    • Click Add service:


Integration and Deployment have been setup and now if we commit a code and push it to our repo then Jenkins will automatically get triggered and a job will start building automatically. If the build is successful Jenkins will create a zip file of our updated application and push it to AWS S3 and further trigger AWS CodeDeploy application. If deployment is failed then it will automatically deploy the previous successful revision.

Hence, the above procedure helps us to achieve Continuous Integration and Continuous Deployment.


Leave a comment -