Last updated on June 5, 2023
Amazon EC2 Auto Scaling allows you to scale-out or scale-in your EC2 instances automatically based on policies you configured in response to changing demand on your application. Auto Scaling Groups lets you set a minimum and maximum number of EC2 instances to automatically resize your cluster based on a defined schedule or instance metrics such as CPU utilization, network traffic, etc.
This is an excellent way to cut costs and make sure you have the correct number of instances since traffic on your application fluctuates throughout the day.
Auto Scaling Lifecycle Hooks
As your Auto Scaling group scale-out or scale-in your EC2 instances, you may want to perform custom actions before they start accepting traffic or before they get terminated. Auto Scaling Lifecycle Hooks allow you to perform custom actions during these stages.
For example, during the scale-out event of your ASG, you want to make sure that new EC2 instances download the latest code base from the repository and that your EC2 user data has completed before it starts accepting traffic. This way, the new instances will be fully ready and will quickly pass the load balancer health check when they are added as targets. Another example is this – during the scale-in event of you ASG, suppose your instances upload data logs to S3 every minute. You may want to pause the instance termination for a certain amount of time to allow the EC2 to upload all data logs before it gets completely terminated.
Lifecycle Hooks give you greater control of your EC2 during the launch and terminate events. The following diagram shows the transitions between the EC2 instance states with lifecycle hooks.
1. The Auto Scaling group responds to a scale-out event and provisions a new EC2 instance.
2. The lifecycle hook puts the new instance on Pending:Wait state. The instance stays in this paused state until you continue with the “CompleteLifecycleAction” operation or the default wait time of 3600 seconds is finished. For example, you can create a script that runs during the creation of the instance to download and install the needed packages for your application. Then the script can call the “CompleteLifecycleAction” operation to move the instance to the InService state. Or you can just wait for your configured timeout and the instance will be moved to the InService state automatically.
3. The instance is put to InService state. If you configured a load balancer for this Auto Scaling group, the instance will be added as targets and the load balancer will begin the health check. After passing the health checks, the instance will receive traffic.
4. The Auto Scaling group responds to a scale-in event and begins terminating an instance.
5. The instance is taken out of the load balancer target. The lifecycle hook puts the instance on Terminating:Wait state. For example, you can set a timeout of 2 minutes on this section to allow your instance to upload any data files inside it to S3. After the timeout, the instance is moved to the next state.
6. Auto scaling group completes the termination of the instance.
During the paused state (either launch or terminate), you can do more than just run custom scripts or wait for timeouts. CloudWatch Events receives the scaling action and you can define a CloudWatch Events Target to invoke a Lambda function that can perform a pre-configured task. You can also configure a notification target for the lifecycle hook so that you will receive a message when the scaling event occurs.
Configure Lifecycle Hooks on your Auto Scaling Groups
The following steps will show you how to configure lifecycle hooks for your Auto Scaling group.
1. On the Amazon EC2 Console, under Auto Scaling, choose Auto Scaling Group.
2. Select your Auto Scaling group.
3. Click the Lifecycle hooks tab then click the Create Lifecycle Hook button.
4. In the Create Lifecycle Hook box, do the following:
-
- Lifecycle Hook Name – then name for this lifecycle hook
- Lifecycle Transition – choose whether this lifecycle hook is for “Instance Launch” or “Instance Terminate” event. If you need a lifecycle hook for both events, you need to add another lifecycle hook.
- Heartbeat timeout – the amount of time (in seconds) for the instance to remain in the wait state. The range is between 30 seconds to 7200 seconds.
- Default Result – the action the Auto Scaling group takes when the lifecycle hook timeout elapses or if an unexpected error occurs.
-
-
- If you choose CONTINUE and the instance is launching, the Auto Scaling group assumes that the actions are successful and proceeds to put the instance to InService state. If you choose CONTINUE and the instance is terminating, the Auto Scaling group will proceed with other lifecycle hooks before termination.
- Choosing ABANDON on either state will terminate the instance immediately.
-
-
- Notification Metadata – additional information to include in messages to the notification target.
5. Click Create to apply the lifecycle hook for this Auto Scaling group.
In the second part of this article, we will talk about Configuring Notifications for Amazon EC2 Auto Scaling Lifecycle Hooks.
Sources:
https://docs.aws.amazon.com/autoscaling/ec2/userguide/lifecycle-hooks.html
https://docs.aws.amazon.com/cli/latest/reference/autoscaling/put-lifecycle-hook.html
https://docs.aws.amazon.com/cli/latest/reference/autoscaling/record-lifecycle-action-heartbeat.html
https://docs.aws.amazon.com/autoscaling/ec2/userguide/what-is-amazon-ec2-auto-scaling.html
https://aws.amazon.com/blogs/infrastructure-and-automation/run-code-before-terminating-an-ec2-auto-scaling-instance/