Instance Refresh and Lifecycle Hooks
Roll out a new launch template gradually with instance refresh, and use lifecycle hooks to run custom logic before instances are launched or terminated.
Instance Refresh and Lifecycle Hooks is a free AWS Solutions Architect lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AWS Solutions Architect learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Problem of Rolling Instance Updates
When you update a Launch Template (new AMI, new user data, new instance type), existing running instances in the ASG do not automatically get replaced—they keep running the old configuration. To propagate changes you must replace instances. Manually terminating and relaunching instances is risky and error-prone. Instance Refresh automates this process with configurable safety controls.
Instance Refresh: How It Works
Instance Refresh replaces ASG instances in rolling batches. At each step it terminates a batch of old instances, waits for the new instances to pass health checks, and then proceeds to the next batch. You control the pace with a MinHealthyPercentage (the minimum percentage of capacity that must remain healthy during the refresh) and an optional InstanceWarmup time.
aws autoscaling start-instance-refresh \
--auto-scaling-group-name 'MyAppASG' \
--preferences '{
"MinHealthyPercentage": 80,
"InstanceWarmup": 300,
"CheckpointPercentages": [20, 50, 100],
"CheckpointDelay": 600
}'MinHealthyPercentage and Batch Size
MinHealthyPercentage defines the floor of healthy capacity during refresh. If you set it to 90%, only 10% of the fleet is replaced at a time—slower but safer. A value of 50% replaces half the fleet at once—faster but with less redundancy. ASG automatically calculates batch sizes based on this percentage and your current desired capacity. Choose a higher percentage for production workloads to maintain availability.
Checkpoints for Canary-Style Rollouts
Checkpoints allow you to pause Instance Refresh at predefined percentage milestones. For example, you can refresh 20% of instances, pause for 10 minutes to run smoke tests, and then continue. If you detect a problem at any checkpoint you can cancel the refresh—ASG stops replacing additional instances but leaves already-replaced instances running (they are already on the new template). This is effectively a canary deployment pattern for EC2.
Cancelling and Monitoring Instance Refresh
You can cancel an in-progress Instance Refresh at any time; it stops further replacements but does not roll back completed ones. Use describe-instance-refreshes to check status, progress percentage, and any instances that failed to launch. Integrate Instance Refresh with your CI/CD pipeline to automatically trigger a refresh after pushing a new AMI to the Launch Template, completing a fully automated blue/green-style deployment.
aws autoscaling describe-instance-refreshes \
--auto-scaling-group-name 'MyAppASG'
# Cancel a running refresh
aws autoscaling cancel-instance-refresh \
--auto-scaling-group-name 'MyAppASG'Lifecycle Hooks Introduced
Lifecycle Hooks pause ASG instance transitions at key moments so you can run custom logic before an instance becomes active or before it is terminated. There are two hook points: EC2_INSTANCE_LAUNCHING (after launch, before the instance is put InService) and EC2_INSTANCE_TERMINATING (after the termination decision, before the instance is actually terminated). During a hook the instance is in a Pending:Wait or Terminating:Wait state.
Use Cases for Launch Lifecycle Hooks
The launching hook is ideal for tasks that must complete before an instance serves traffic: installing additional software, pulling configuration from Parameter Store, registering with a service discovery registry, or running integration tests. The ASG will not add the instance to the load balancer target group until the hook completes (or times out). This prevents traffic from reaching an incompletely configured instance.
aws autoscaling put-lifecycle-hook \
--auto-scaling-group-name 'MyAppASG' \
--lifecycle-hook-name 'AppInit' \
--lifecycle-transition EC2_INSTANCE_LAUNCHING \
--default-result ABANDON \
--heartbeat-timeout 300Use Cases for Termination Lifecycle Hooks
The termination hook runs before an instance is actually terminated. Use cases include: draining in-flight requests from an application-level queue, flushing cached data to a persistent store, deregistering from a service registry, or copying log files to S3. Without a termination hook, in-flight work on a terminating instance may be lost. The hook gives you up to 2 hours (heartbeat timeout) to complete graceful shutdown tasks.
aws autoscaling put-lifecycle-hook \
--auto-scaling-group-name 'MyAppASG' \
--lifecycle-hook-name 'GracefulShutdown' \
--lifecycle-transition EC2_INSTANCE_TERMINATING \
--default-result CONTINUE \
--heartbeat-timeout 120Completing a Lifecycle Hook
The custom logic running on (or triggered by) the instance must signal the hook when it is done. Use complete-lifecycle-action with either CONTINUE (proceed normally) or ABANDON (on a launching hook, terminate the instance; on a terminating hook, ignore and proceed). You can also extend the hook's timeout by sending a heartbeat every interval up to the maximum duration. If the hook times out, the DefaultResult is applied.
# From inside the instance or a Lambda triggered by EventBridge
aws autoscaling complete-lifecycle-action \
--auto-scaling-group-name 'MyAppASG' \
--lifecycle-hook-name 'AppInit' \
--lifecycle-action-result CONTINUE \
--instance-id 'i-0abc123def456'Triggering Logic with EventBridge
The cleanest way to respond to lifecycle hooks is via Amazon EventBridge. When a lifecycle hook fires, ASG emits an event to EventBridge. An EventBridge rule routes this event to a Lambda function that performs the required work and then calls complete-lifecycle-action. This architecture is fully serverless, requires no polling, and scales automatically. SQS and SNS notifications are older alternatives but EventBridge is preferred for new designs.
Instance Refresh Integrates with Lifecycle Hooks
When Instance Refresh replaces an instance, both lifecycle hooks still fire: the termination hook on the old instance and the launching hook on the new one. This means your graceful shutdown and initialisation logic runs automatically during a rolling update, not just during normal scale events. Always test your lifecycle hook logic during a staged Instance Refresh before a production rollout to verify correct behaviour.
Quick Check
Test your understanding of AWS Solutions Architect (SAA-C03) concepts from this lesson.
Lesson Recap
In this lesson you learned: Instance Refresh replaces ASG instances in rolling batches controlled by MinHealthyPercentage and optional checkpoints for canary-style validation, Lifecycle Hooks pause instance transitions at launch or termination to run custom initialisation or graceful shutdown logic, and EventBridge + Lambda is the recommended pattern for responding to lifecycle hook events without polling. Next up we explore AWS Lambda functions, runtimes, triggers, and handlers.
Frequently asked questions
Is the “Instance Refresh and Lifecycle Hooks” lesson free?
Yes — the full text of “Instance Refresh and Lifecycle Hooks” is free to read here on the web, and the AWS Solutions Architect course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AWS Solutions Architect course, upgrade to CoddyKit PRO.
What will I learn in “Instance Refresh and Lifecycle Hooks”?
Roll out a new launch template gradually with instance refresh, and use lifecycle hooks to run custom logic before instances are launched or terminated. You practise AWS Solutions Architect with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AWS Solutions Architect?
No prior experience is required. AWS Solutions Architect on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Instance Refresh and Lifecycle Hooks” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AWS Solutions Architect lesson?
Yes. Every AWS Solutions Architect lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Launch Templates and ASG Configuration
- Scaling Policies: Target Tracking and Step Scaling
- Scheduled Scaling and Predictive Scaling
- Instance Refresh and Lifecycle Hooks