AWS Auto Scaling Group – Working with Lifecycle Hooks

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 applications like Java and .Net. 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 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.

1.       Go to EC2 console and create a lunch configuration.

    a.       Choose the Ubuntu Server 16.04 AMI.

    b.      Select t2.micro as the instance type.

    c.       On Configure details page, expand the Advanced Details section and enter following in the user data


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.

        I.   Updating the repositories and upgrading all packages

        II.   Installing Java

        III.   Sending a signal to Auto Scaling that we are done with the installation of applications so traffic can now be served to this instance.

        IV.   If any of the above fail, we can send a ABANDON signal to AutoScaling to terminate the instance as the applications did not install properly and instance cannot serve traffic.

    d.      Keep rest of settings default and finalize the creation of Launch Configuration.

2.       Create an Auto Scaling Group from the newly created Launch Configuration. Keep the Group Size to be zero at this moment. Make sure you give 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.

3.       Select you Auto Scaling Group. Go to 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.

4.       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 are executed the instance will then be declared as ready to serve traffic.

5.       Now you can ssh into the newly launched and execute java –version to check that your application has bene 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 depended on many other building blocks lifecycle hooks are the best way of making sure your instance is fully ready before it starts receiving traffic.

Muhammad Ali
Bluestack IT Solutions