{"id":35318,"date":"2016-06-08T16:03:54","date_gmt":"2016-06-08T10:33:54","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=35318"},"modified":"2016-06-09T09:41:07","modified_gmt":"2016-06-09T04:11:07","slug":"aws-codedeploy-automatic-rollback-using-aws-lambda","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/aws-codedeploy-automatic-rollback-using-aws-lambda\/","title":{"rendered":"AWS CodeDeploy Automatic Rollback using AWS Lambda"},"content":{"rendered":"<p><a title=\"Introduction To AWS LAMBDA\" href=\"http:\/\/www.tothenew.com\/blog\/introduction-to-aws-lambda\/\">AWS Lambda<\/a> 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.<\/p>\n<p><strong><span style=\"font-weight: 400;\"><strong>AWS CodeDeploy<\/strong> is a service that <a title=\"AWS DevOps\" href=\"http:\/\/www.tothenew.com\/devops-aws\">automates code deployments to Amazon EC2<\/a> 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.<\/span><\/strong><\/p>\n<p>However, one major drawback with AWS CodeDeploy is that it does not support the concept of automatic rollback in case of deployment failure.<\/p>\n<p>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.<\/p>\n<p>The basic logic to implement the above-mentioned scenario is to first <a href=\"http:\/\/www.tothenew.com\/blog\/aws-code-deploy-a-sample-walkthrough\/\">set up an AWS CodeDeploy application<\/a> 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\u00a0deployment and triggers the AWS CodeDeploy accordingly.<\/p>\n<div id=\"attachment_35320\" style=\"width: 581px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-35320\" decoding=\"async\" loading=\"lazy\" class=\" wp-image-35320\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/lambda-codedeploy2.png\" alt=\"AWS Code Deploy-Lambda integration\" width=\"571\" height=\"356\" \/><p id=\"caption-attachment-35320\" class=\"wp-caption-text\">AWS Code Deploy-Lambda integration<\/p><\/div>\n<p><strong>\u00a0<\/strong><\/p>\n<h2>Setting up AWS CodeDeploy Application &amp; AWS Lambda<\/h2>\n<h4>Follow the following steps in order to set up the AWS CodeDeploy application:<\/h4>\n<p>1. Sign in to the AWS Console. Go to the services and click on \u201c<strong>CodeDeploy\u201d.<\/strong><\/p>\n<p>2. Click on &#8220;<strong>Create new application<\/strong>&#8220;. Enter a suitable\u00a0<strong>Application Name<\/strong> and <strong>Application Group Name:<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35353\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/Screenshot-from-2016-06-07-092301.png\" alt=\"Screenshot from 2016-06-07 09:23:01\" width=\"880\" height=\"180\" \/><\/p>\n<p>3. Add existing EC2 instances using key and value:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35322\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/Screenshot-from-2016-06-07-023807.png\" alt=\"Screenshot from 2016-06-07 02:38:07\" width=\"1113\" height=\"251\" \/><\/p>\n<p>4. Choose a deployment configuration:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35323\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/Screenshot-from-2016-06-07-024125.png\" alt=\"Screenshot from 2016-06-07 02:41:25\" width=\"978\" height=\"271\" \/><\/p>\n<p>5. Now, create a trigger. Click on &#8220;<strong>Create Trigger<\/strong>&#8220;. Enter an appropriate <strong>Trigger Name<\/strong>. In &#8220;<strong>Events<\/strong>&#8221; field, select &#8220;<strong>Deployment<\/strong> <strong>fails<\/strong>&#8220;. This will ensure that the trigger would\u00a0be invoked only in case of &#8220;Deployment fail&#8221; event.<\/p>\n<p>6. Select an\u00a0<strong>Amazon SNS topic <\/strong>from the available list of configured SNS \u00a0topics. Click on &#8220;<strong>Create Trigger<\/strong>&#8220;:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35324\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/Screenshot-from-2016-06-07-024928.png\" alt=\"Screenshot from 2016-06-07 02:49:28\" width=\"764\" height=\"670\" \/><\/p>\n<p>7. Select an IAM role in &#8220;<span class=\"ng-binding\"><strong>Service Role ARN<\/strong>&#8221; field, with appropriate policies attached which are needed to run AWS CodeDeploy:<\/span><\/p>\n<p><span class=\"ng-binding\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35325\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/Screenshot-from-2016-06-07-025923.png\" alt=\"Screenshot from 2016-06-07 02:59:23\" width=\"944\" height=\"183\" \/><\/span><\/p>\n<p><span class=\"ng-binding\">8. Click on &#8220;<strong>Create application<\/strong>&#8220;. This will successfully create your AWS CodeDeploy Application.<\/span><\/p>\n<p>9. Now configure AWS Lambda. In AWS Console , go to services and click on \u201clambda<strong>\u201d.<\/strong><\/p>\n<p>10. Click on &#8220;<strong>Create Lambda Function<\/strong>&#8220;.<\/p>\n<p>11. Select <strong>SNS-message<\/strong> blueprint.<\/p>\n<p>12. Now configure event sources. Select <strong>Event Source Type<\/strong> as &#8220;<strong>SNS<\/strong>&#8221; and an appropriate <strong>SNS topic<\/strong>(SNS topic should be same as the one configured in AWS CodeDeploy application). Click <strong>Next<\/strong>.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35326\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/Screenshot-from-2016-06-07-032235.png\" alt=\"Screenshot from 2016-06-07 03:22:35\" width=\"1019\" height=\"448\" \/><\/p>\n<p>13. Now Configure the function. Give any\u00a0<strong>Name<\/strong> and <strong>Description<\/strong>. In <strong>Runtime<\/strong> Field, select &#8220;<strong>Python<\/strong> <strong>2.7<\/strong>&#8220;.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35327\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/Screenshot-from-2016-06-07-032947.png\" alt=\"Screenshot from 2016-06-07 03:29:47\" width=\"894\" height=\"266\" \/><\/p>\n<p>14. Write the following python script in the code section:<\/p>\n<p>[sourcecode language=&#8221;python&#8221;]import boto3<br \/>\ndef lambda_handler(event,context):<br \/>\n   c=boto3.client(&#8216;codedeploy&#8217;)<br \/>\n   dep_ids=c.list_deployments(applicationName=&quot;lambda_demo&quot;, deploymentGroupName=&quot;demo&quot;, includeOnlyStatuses=[&quot;Succeeded&quot;])<br \/>\n   did=dep_ids[&#8216;deployments&#8217;]<br \/>\n   final_id=did[0]<br \/>\n   b=c.get_deployment(deploymentId=final_id)<br \/>\n   commit=b[&#8216;deploymentInfo&#8217;][&#8216;revision&#8217;][&#8216;gitHubLocation&#8217;][&#8216;commitId&#8217;]<br \/>\n   print commit<br \/>\n   repo=b[&#8216;deploymentInfo&#8217;][&#8216;revision&#8217;][&#8216;gitHubLocation&#8217;][&#8216;repository&#8217;]<br \/>\n   print repo<br \/>\n   c.create_deployment(applicationName=&quot;lambda_demo&quot;,deploymentGroupName=&quot;demo&quot;,revision={&#8216;revisionType&#8217;:&#8217;GitHub&#8217;, &#8216;gitHubLocation&#8217;: {&#8216;repository&#8217;: repo,&#8217;commitId&#8217;: commit}})<br \/>\n [\/sourcecode]<\/p>\n<p>15. In Lambda function handler and role, select the default handler as &#8220;<strong>lambda_function.lambda_handler<\/strong>&#8220;. In Role field, select &#8220;<strong>Basic Execution Role<\/strong>&#8220;. 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:<\/p>\n<p>[sourcecode language=&#8221;python&#8221;]{<br \/>\n&quot;Version&quot;: &quot;2012-10-17&quot;,<br \/>\n&quot;Statement&quot;: [<br \/>\n{<br \/>\n&quot;Effect&quot;: &quot;Allow&quot;,<br \/>\n&quot;Action&quot;: &quot;*&quot;,<br \/>\n&quot;Resource&quot;: &quot;*&quot;<br \/>\n}<br \/>\n]<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>16. Click <strong>allow<\/strong>.<\/p>\n<p>17. Click <strong>Next<\/strong>.<\/p>\n<p>18. In the review window, select &#8220;<strong>Enable event source<\/strong>&#8220;:<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35329\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/Screenshot-from-2016-06-07-041423.png\" alt=\"Screenshot from 2016-06-07 04:14:23\" width=\"856\" height=\"177\" \/><\/p>\n<p>19. Click on <strong>Create function<\/strong>. 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 &#8220;Enable event source&#8221; as mentioned in the previous step).<\/p>\n<p>20. Now go back to AWS CodeDeploy dashboard. Select <b>Deployments.<\/b><\/p>\n<p>21. Click on &#8220;<strong>Create New Deployment<\/strong>&#8220;.<\/p>\n<p>22. Enter the previously configured <strong>Application<\/strong> and <strong>Deployment<\/strong> <strong>group<\/strong> <strong>name<\/strong>. Select the <strong>Revision<\/strong> <strong>Type<\/strong> as &#8220;<strong>My application is stored in github&#8221;.\u00a0<\/strong>Enter the appropriate git <strong>repository name and<\/strong>\u00a0<strong>commit id <\/strong>along with the\u00a0<strong>Deployment Config.<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35331\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/Screenshot-from-2016-06-07-043005.png\" alt=\"Screenshot from 2016-06-07 04:30:05\" width=\"1282\" height=\"600\" \/><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>23. Click on <strong>Deploy Now.<\/strong><\/p>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":914,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":43},"categories":[1174,2348],"tags":[248,1676,1679,3454,3450,3451,3453,1345,3452,1358,1790],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/35318"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/914"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=35318"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/35318\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=35318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=35318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=35318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}