{"id":27465,"date":"2015-10-31T12:23:22","date_gmt":"2015-10-31T06:53:22","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=27465"},"modified":"2015-11-01T10:45:28","modified_gmt":"2015-11-01T05:15:28","slug":"aws-auto-scaling-lifecycle-hooks-2","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/aws-auto-scaling-lifecycle-hooks-2\/","title":{"rendered":"AWS Auto-scaling Lifecycle Hooks"},"content":{"rendered":"<p>The lifecycle hooks are the great feature of auto scaling, it helps to control instance launch and termination state within auto-scaling group. I got to know about this hidden feature when I was looking\u00a0for a solution where I had to perform some automated tasks on the instance before adding\/removing it from auto-scaling group. I was struggling with auto-scaling setup as the time required for newly\u00a0launched instance to display &#8220;in:service&#8221; status was 5 minutes and the auto-scaling health check value was 3 minutes, due to which the instance launched under auto-scaling gets terminated before the completion of automated task. It leads to an infinitive launch and termination loop. While increasing the\u00a0health check interval of\u00a0auto-scaling group may help to get rid off this issue but during the same, if the running instance goes unhealthy, then the application performance will be compromised. Before getting a deep dive into it, let us first understand how auto-scaling works.<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28888\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/Screenshot-from-2015-10-25-141720.png\" alt=\"Screenshot from 2015-10-25 14:17:20\" width=\"940\" height=\"603\" \/><\/p>\n<p><strong>\u00a0<\/strong>1.\u00a0When a scale up event is triggered, a new instance request is initiated by auto-scaling group. It will take some time to launch\u00a0an instance with a predefined launch configuration. This state is known as<b>\u00a0&#8220;<\/b>pending&#8221;.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28898\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/61.png\" alt=\"6\" width=\"1093\" height=\"140\" \/><\/p>\n<p>2. Once the instance is available, the lifecycle hooks are invoked and the instance state will be changed to &#8220;Pending:Wait&#8221;. In this state, we can perform pre-production tasks.\u00a0The instance will not be passed to ELB until lifecycle hook&#8217;s timeout occurs.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28899\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/71.png\" alt=\"7\" width=\"1098\" height=\"132\" \/><\/p>\n<p>3. After a predefined timeout of lifecycle hook, the instance will be attached to ELB and will be ready to serve production traffic. This state is known as &#8220;InService&#8221;.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28900\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/101.png\" alt=\"10\" width=\"1092\" height=\"127\" \/><\/p>\n<p>4. When a scale down event is triggered, an instance\u00a0will be chosen to be removed from auto-scaling group. The lifecycle hook will be invoked and the instance will remain in the &#8220;Terminating:Wait&#8221; state\u00a0until hook timeouts.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28901\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/121.png\" alt=\"12\" width=\"1091\" height=\"119\" \/><\/p>\n<p>5. The instance will be terminated after completion of timeout interval. This state is known as &#8220;termination:proceed&#8221;.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28902\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/13.png\" alt=\"13\" width=\"1092\" height=\"121\" \/><\/p>\n<p>I previously mentioned it as a <b>hidden feature<\/b>\u00a0because there is no option in the console to set hooks in auto-scaling configuration. We can add hooks only from CLI or API. Here, I am assuming that AWS CLI is configured on your system with full permission on auto-scaling resources.<\/p>\n<p><strong><\/p>\n<h3>Configuration Steps:<\/h3>\n<p><\/strong><\/p>\n<p><strong>1. Create SNS Topic:<\/strong> \u00a0We need an SNS topic to receive all event notification triggered by the hook. When a newly launched instance enters into &#8220;launch:wait&#8221; state, a notification is triggered to subscribed Email ID.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28903\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/31.png\" alt=\"3\" width=\"925\" height=\"221\" \/><\/p>\n<p><strong>2. Create IAM Role:<\/strong> The auto-scaling resources need permission to send notification alert via SNS. We have to create a service role with &#8220;AutoScaling Notification Access&#8221; policy.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28904\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/22.png\" alt=\"2\" width=\"1038\" height=\"439\" \/><\/p>\n<p><strong>3.\u00a0Create a lifecycle hook:<\/strong> Here, we need ARN of SNS and IAM role which we created in previous steps. If the above steps are executed as described\u00a0then we are good to go. In order to\u00a0create lifecycle hook in auto-scaling group, you can run command given below. This will invoke newly launched instance with 60 seconds\u00a0wait interval.<\/p>\n<p>[js]aws autoscaling put-lifecycle-hook &#8211;lifecycle-hook-name  &#8211;auto-scaling-group-name  &#8211;lifecycle-transition autoscaling:EC2_INSTANCE_LAUNCHING &#8211;notification-target-arn  &#8211;role-arn  &#8211;heartbeat-timeout 300[\/js]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28906\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/14.png\" alt=\"1\" width=\"1320\" height=\"100\" \/><\/p>\n<p>If no error message is reported by above command, that means, the lifecycle hook is implemented in auto-scaling but, to be on the safer side, don&#8217;t forget to verify the details of lifecycle hook with the following command:<\/p>\n<p>[js]<br \/>\naws autoscaling describe-lifecycle-hooks &#8211;auto-scaling-group-name test_asg &#8211;lifecycle-hook-names asg_hook &#8211;out json<br \/>\n[\/js]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28905\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/51.png\" alt=\"5\" width=\"1317\" height=\"292\" \/><\/p>\n<p>During scale-up action, when an instance enters in &#8220;launch:wait&#8221; state, a notification alert is pushed to SNS topic. As mentioned in below snap this notification message contains detailed information of instance including &#8220;LifecycleAction Token&#8221;. This token ID will be used to modify instance hook attributes.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28909\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/91.png\" alt=\"9\" width=\"754\" height=\"180\" \/><\/p>\n<p>If we want to extend default timeout value, it can be achieved with the help of below command. We can force instance to stay in the wait state for next 300 seconds.\u00a0Please make sure the token ID must be same as we received in the previous step.<\/p>\n<p>[js]<br \/>\naws autoscaling record-lifecycle-action-heartbeat &#8211;lifecycle-action-token &lt;Action Token ID&gt; &#8211;auto-scaling-group-name test_asg &#8211;lifecycle-hook-name asg_hook<br \/>\n[\/js]<\/p>\n<p>If the automated job finished before the default timeout value, then we can also force an instance to leave its waiting state using following command:<\/p>\n<p>[js]<br \/>\naws autoscaling complete-lifecycle-action &#8211;lifecycle-action-token &lt;Action Token ID&gt; &#8211;lifecycle-hook-name asg_hook &#8211;autio-scaling-group-name &lt;test_asg&gt; &#8211;lifecycle-action-result CONTINUE<br \/>\n[\/js]<\/p>\n<p>&nbsp;<\/p>\n<p>I hope this blog will help you to setup and run instances with lifecycle hooks. If you come across any\u00a0error during the setup, I would suggest to crosscheck your ARN and LifecycleActionToken.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The lifecycle hooks are the great feature of auto scaling, it helps to control instance launch and termination state within auto-scaling group. I got to know about this hidden feature when I was looking\u00a0for a solution where I had to perform some automated tasks on the instance before adding\/removing it from auto-scaling group. I was [&hellip;]<\/p>\n","protected":false},"author":165,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":66},"categories":[1174,1],"tags":[1217,2687,248,1746,1462,2686],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/27465"}],"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\/165"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=27465"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/27465\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=27465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=27465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=27465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}