Sub topic 16
Lifecycle Hooks and Instance Refresh for Safe Changes
This chapter assumes you can create a launch template and an Auto Scaling group (ASG) attached to a target group, as covered in earlier chapters. It shows how to pause an instance during launch or termination, and how to roll out a new launch template version without downtime.
Why changes need guardrails
An ASG launches and terminates instances on its own. Two risks follow:
- A new instance can be marked in service before it has finished installing software or pulling configuration.
- A terminating instance can be removed before it has uploaded logs or finished its work.
Lifecycle hooks address both risks. Instance refresh addresses a third one: replacing every instance after you change the launch template.
Lifecycle hooks
A lifecycle hook is a pause point in an instance's life. The instance moves into a wait state until your code sends a completion signal or the timeout expires.
| Transition | Hook state | Typical use |
|---|---|---|
| Launch | `Pending:Wait` | Install software, register with a configuration service |
| Terminate | `Terminating:Wait` | Drain work, upload logs, deregister from a service |
Create a launch hook that waits up to 10 minutes. If the timeout expires, DefaultResult decides what happens. ABANDON terminates the new instance. CONTINUE puts it in service anyway.
aws autoscaling put-lifecycle-hook \
--auto-scaling-group-name web-asg \
--lifecycle-hook-name wait-for-bootstrap \
--lifecycle-transition autoscaling:EC2_INSTANCE_LAUNCHING \
--heartbeat-timeout 600 \
--default-result ABANDONWhen the instance finishes its setup, it signals completion. Run this from the instance, or from any process that knows the instance ID:
aws autoscaling complete-lifecycle-action \
--auto-scaling-group-name web-asg \
--lifecycle-hook-name wait-for-bootstrap \
--lifecycle-action-result CONTINUE \
--instance-id i-0123456789abcdef0If setup fails, send ABANDON instead so the ASG replaces the instance. For longer jobs, call record-lifecycle-action-heartbeat to extend the wait. Total time in a wait state is capped at 48 hours.
A hook can notify an Amazon EventBridge rule, an SQS queue or an SNS topic, which then starts your automation.
Instance refresh
After you publish a new launch template version, existing instances keep running the old one. Instance refresh replaces them in batches.
aws autoscaling start-instance-refresh \
--auto-scaling-group-name web-asg \
--preferences '{"MinHealthyPercentage": 90, "InstanceWarmup": 300}'The settings control the pace:
MinHealthyPercentageis the share of desired capacity that must stay healthy. At 90 percent, replacement proceeds in small batches.InstanceWarmupis the time the ASG waits after an instance becomes healthy before it counts as ready.
Check progress, and cancel if the new version misbehaves:
aws autoscaling describe-instance-refreshes --auto-scaling-group-name web-asg
aws autoscaling cancel-instance-refresh --auto-scaling-group-name web-asgCommon errors
- Hook never completes. Nothing sends
complete-lifecycle-action, so the instance waits for the full timeout. Test the signal path before relying on it. - Refresh stalls. New instances fail health checks, so the ASG cannot meet
MinHealthyPercentage. Look at target health first. - Dropped requests at scale-in. A terminate hook does not replace deregistration delay. Both run, so size your timeouts together.
Exercise
A launch hook has a 600-second heartbeat timeout and DefaultResult set to ABANDON. The bootstrap script crashes and never calls complete-lifecycle-action. What happens to the instance, and what would change if DefaultResult were CONTINUE?
Show answer
After 600 seconds the ASG terminates the instance and launches a replacement, because ABANDON ends the launch. With CONTINUE, the instance would enter service despite the failed bootstrap and could receive traffic while broken, so ABANDON is the safer choice here.