AWS Auto Scaling Group: Working With Lifecycle Hooks
Lifecycle hooks are a tool that you can use to prevent complex Java and .Net applications from recieving traffic until you are ready.
Join the DZone community and get the full member experience.Join For Free
Purpose of Lifecycle Hooks
Usually deploying instances inside an AutoScaling group involves installing applications on them and sometimes this can take time, especially if you are deploying complex Java and .Net applications. So, during the time your applications are getting deployed and your instance is basically not yet ready for serving traffic, the auto-scaling group or ELB may see the instance state as healthy and start serving traffic to it. This is where Lifecycle hooks come into play.
Lifecycle hooks pause the state of an instance at Pending:Wait, by default, for 60 minutes. This is the duration during which you can deploy your applications on the instance. Here you can find more information on instance states inside an Auto Scaling Group.
Let us go through an example to demonstrate the concept given above.
Go to EC2 console and create a lunch configuration.
Choose the Ubuntu Server 16.04 AMI.
Select t2.micro as the instance type.
On Configure details page, expand the Advanced Details section and enter following in the user data
#!/bin/bash apt-get update && apt-get upgrade && \ apt-get install default-jdk && \ INSTANCE_ID="`wget -q -O - http://instance-data/latest/meta-data/instance-id`" && \ aws autoscaling complete-lifecycle-action --lifecycle-action-result CONTINUE --instance-id $INSTANCE_ID --lifecycle-hook-name helloworld-hook --auto-scaling-group-name helloworld-asg --region us-east-1 || \ aws autoscaling complete-lifecycle-action --lifecycle-action-result ABANDON --instance-id $INSTANCE_ID --lifecycle-hook-name helloworld-hook --auto-scaling-group-name helloworld-asg --region us-east-1
Above is the script that will be executed at the time of instance launch. In this script we are doing the following.
Updating the repositories and upgrading all packages
Sending a signal to Auto Scaling that we are done with the installation of applications so traffic can now be served to this instance.
If any of the above fail, we can send an ABANDON signal to AutoScaling to terminate the instance as the applications did not install properly and the instance cannot serve traffic. Keep the rest of the settings default and finalize the creation of Launch Configuration.
Create an Auto Scaling Group from the newly created Launch Configuration. Keep the Group Size to zero at this moment. Make sure you give the same name to your Auto Scaling Group as you have specified in the User data sections of your launch configuration. Finalize the creation of the group.
Select your Auto Scaling Group. Go to the Lifecycle tab and create a new lifecycle hook. Select the Lifecycle transition to be Instance Launch. The name you assign to your lifecycle hook should match with what you mention in the script specified in User Data.
Edit the max and min number of instance inside the group to 1 and save the settings.
Note: After starting the launching of a new instance, Auto Scaling group will hold the state of new instance until the scripts mentioned in the User Data section of the launch configuration are executed. Once scripts have executed, the instance will then be declared as ready to serve traffic.
Now you can SSH into the newly launched application and execute
java –version to check that your application has been installed.
Using Lifecycle hooks, you can easily deploy complete stack for your applications, configure them and make configurations like security certificates and more. For complex applications that are dependent on many other building blocks, Lifecycle hooks are the best way of making sure your instance is fully ready before it starts receiving traffic.
Opinions expressed by DZone contributors are their own.