Get started with Lifecycle Hooks in AWS Auto Scaling Groups

Lifecycle hooks allow you to control what happens when an instance launches or terminates within an Auto Scaling Group (ASG). These hooks provide an opportunity to pause the instance as it moves between any of the following lifecycle states:

  1. Pending: The instance is waiting to be launched.

  2. Pending:Wait: An optional pause state where an instance will wait until you tell the ASG to continue.

  3. Pending:Proceed: The ASG continues with the launch.

  4. Terminating: The instance is about to be terminated.

  5. Terminating:Wait: An optional pause state where an instance will wait until you tell the ASG to continue.

  6. Terminating:Proceed: The ASG continues with the termination.

Use Cases for ASG Lifecycle Hooks:

  1. Installing or updating software: For example, after launching an instance but before it is put into service, you might want to download and install updates.

  2. Data Management: Before terminating an instance, you might want to archive its data.

  3. Application Warm-Up: Before the instance starts serving traffic, it might need to warm up caches or perform other preparatory tasks.

  4. Notifications: Send notifications about instance launches or terminations.

  5. Logging: Track and record specific steps of the lifecycle process.

Workshop: Timeout Lifecycle Hook for EC2 Launch

Lifecycle hooks allow actions to be triggered when EC2 instances launch or terminate within an ASG. With these hooks, you can capture specific events like autoscaling:EC2_INSTANCE_LAUNCHING and autoscaling:EC2_INSTANCE_TERMINATING to execute custom actions.

1. Set Up: You need an active AWS account and the AWS CLI installed and configured.

2. Create an EC2 Launch Template: Replace the ImageId value with the appropriate AMI ID for your region.

aws ec2 create-launch-template --launch-template-name my-launch-template --version-description initial --launch-template-data '{"ImageId":"ami-0f34c5ae932e6f0e4","InstanceType":"t2.micro"}'

3. Create an Auto Scaling Group using the Launch Template: Replace the availability zones with the ones suitable for your region. Adjust min, max, and desired capacity as per your requirements.

aws autoscaling create-auto-scaling-group --auto-scaling-group-name my-asg --launch-template LaunchTemplateName=my-launch-template,Version=1 --min-size 1 --max-size 3 --desired-capacity 2 --availability-zones us-east-1a us-east-1b

4. Add a Lifecycle Hook: Use the AWS CLI to create a lifecycle hook.

aws autoscaling put-lifecycle-hook --auto-scaling-group-name my-asg --lifecycle-hook-name my-lifecycle-hook --lifecycle-transition autoscaling:EC2_INSTANCE_LAUNCHING --default-result CONTINUE --heartbeat-timeout 300

Here, my-asg is the name of your ASG and my-lifecycle-hook is the name of the lifecycle hook. This hook will pause instances for up to 300 seconds (5 minutes) when they're launching.

5. Trigger a Scaling Activity:

To test the lifecycle hook, we need to initiate a scaling activity that will cause an instance to launch. There are multiple ways to do this, but for simplicity, we'll manually adjust the desired capacity of our ASG.

aws autoscaling set-desired-capacity --auto-scaling-group-name my-asg --desired-capacity 2

6. Verify the Instance State:
After adjusting the desired capacity, an instance will start launching. Given our lifecycle hook, it should pause in the Pending:Wait state.

aws autoscaling describe-auto-scaling-instances

7. Perform the Desired Actions:

While the instance is in the Pending:Wait state, you can perform actions such as:

  • Connect to the instance to perform manual configurations.

  • Run scripts to set up software or configurations.

  • Execute AWS Lambda functions for automated setup.

For simplicity, let's assume you just want to verify that the instance is running and healthy.

8. Clean Up:

Remove the lifecycle hook:

aws autoscaling delete-lifecycle-hook --auto-scaling-group-name my-asg --lifecycle-hook-name my-lifecycle-hook

Finally, delete or update the ASG as needed.

Note: For a more dynamic setup, you can integrate lifecycle hooks with AWS Lambda functions, AWS Step Functions, or even SNS notifications to automate actions when instances launch or terminate.

References:

  1. Amazon EC2 Auto Scaling instance lifecycle

  2. Amazon EC2 Auto Scaling lifecycle hooks