AWS CodeDeploy Automatic Rollback using AWS Lambda

08 / Jun / 2016 by Shruti Lamba 1 comments

AWS Lambda is a compute service where we can upload our code to AWS Lambda and the service can run the code on our behalf using AWS infrastructure.

AWS CodeDeploy is a service that automates code deployments to Amazon EC2 instances. AWS CodeDeploy makes it easier to rapidly release new features, helps to avoid downtime during deployment, and handles the complexity of updating the applications. We can use AWS CodeDeploy to automate deployments, eliminating the need for error-prone manual operations.

However, one major drawback with AWS CodeDeploy is that it does not support the concept of automatic rollback in case of deployment failure.

This blog illustrates how we can use AWS Lambda to perform an automatic rollback of AWS CodeDeploy (using Git) in the case of deployment failure.

The basic logic to implement the above-mentioned scenario is to first set up an AWS CodeDeploy application with an appropriate Deployment group. In that application, configure a trigger which will invoke an SNS topic whenever a deployment fails. The SNS topic then further triggers a Lambda function, which in turn calls a python script. This python script simply finds the repository name and commit id of the last successful deployment and triggers the AWS CodeDeploy accordingly.

AWS Code Deploy-Lambda integration

AWS Code Deploy-Lambda integration

 

Setting up AWS CodeDeploy Application & AWS Lambda

Follow the following steps in order to set up the AWS CodeDeploy application:

1. Sign in to the AWS Console. Go to the services and click on “CodeDeploy”.

2. Click on “Create new application“. Enter a suitable Application Name and Application Group Name:

Screenshot from 2016-06-07 09:23:01

3. Add existing EC2 instances using key and value:

Screenshot from 2016-06-07 02:38:07

4. Choose a deployment configuration:

Screenshot from 2016-06-07 02:41:25

5. Now, create a trigger. Click on “Create Trigger“. Enter an appropriate Trigger Name. In “Events” field, select “Deployment fails“. This will ensure that the trigger would be invoked only in case of “Deployment fail” event.

6. Select an Amazon SNS topic from the available list of configured SNS  topics. Click on “Create Trigger“:

Screenshot from 2016-06-07 02:49:28

7. Select an IAM role in “Service Role ARN” field, with appropriate policies attached which are needed to run AWS CodeDeploy:

Screenshot from 2016-06-07 02:59:23

8. Click on “Create application“. This will successfully create your AWS CodeDeploy Application.

9. Now configure AWS Lambda. In AWS Console , go to services and click on “lambda”.

10. Click on “Create Lambda Function“.

11. Select SNS-message blueprint.

12. Now configure event sources. Select Event Source Type as “SNS” and an appropriate SNS topic(SNS topic should be same as the one configured in AWS CodeDeploy application). Click Next.

Screenshot from 2016-06-07 03:22:35

13. Now Configure the function. Give any Name and Description. In Runtime Field, select “Python 2.7“.

Screenshot from 2016-06-07 03:29:47

14. Write the following python script in the code section:

[sourcecode language=”python”]import boto3
def lambda_handler(event,context):
c=boto3.client(‘codedeploy’)
dep_ids=c.list_deployments(applicationName="lambda_demo", deploymentGroupName="demo", includeOnlyStatuses=["Succeeded"])
did=dep_ids[‘deployments’]
final_id=did[0]
b=c.get_deployment(deploymentId=final_id)
commit=b[‘deploymentInfo’][‘revision’][‘gitHubLocation’][‘commitId’]
print commit
repo=b[‘deploymentInfo’][‘revision’][‘gitHubLocation’][‘repository’]
print repo
c.create_deployment(applicationName="lambda_demo",deploymentGroupName="demo",revision={‘revisionType’:’GitHub’, ‘gitHubLocation’: {‘repository’: repo,’commitId’: commit}})
[/sourcecode]

15. In Lambda function handler and role, select the default handler as “lambda_function.lambda_handler“. In Role field, select “Basic Execution Role“. A new window will pop up which specifies the IAM role and policy name along with policy document. Click on edit policy, and write the following policy in order to allow your Lambda function to access other AWS Services:

[sourcecode language=”python”]{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "*",
"Resource": "*"
}
]
}
[/sourcecode]

16. Click allow.

17. Click Next.

18. In the review window, select “Enable event source“:Screenshot from 2016-06-07 04:14:23

19. Click on Create function. This will successfully create your Lambda function which will be invoked immediately (If you do not want your Lambda function to be invoked immediately after its creation, do not select “Enable event source” as mentioned in the previous step).

20. Now go back to AWS CodeDeploy dashboard. Select Deployments.

21. Click on “Create New Deployment“.

22. Enter the previously configured Application and Deployment group name. Select the Revision Type as “My application is stored in github”. Enter the appropriate git repository name and commit id along with the Deployment Config.Screenshot from 2016-06-07 04:30:05

 

23. Click on Deploy Now.

This will successfully trigger your AWS CodeDeploy and if in any case code deploy fails, Lambda function will be triggered, thus leading to automatic rollback of CodeDeploy.

FOUND THIS USEFUL? SHARE IT

comments (1 “AWS CodeDeploy Automatic Rollback using AWS Lambda”)

  1. Zak

    Hi there,
    Thank you for nice tutorial. I found above script is for code deploy deployment with git but i am deploying from s3 bucket. Do you have s3 version of this script please? Thank you so much.

    Sincerely,
    Zak

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *