Lesson 3 of 3 Amazon EC2 Auto Scaling

Preparing Instances with Lifecycle Hooks and Warm Pools

This lesson explains how instances pass through states that can be paused or prepared in advance, so they are ready before they take traffic and finish work before they go.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how lifecycle hooks run custom actions.
  • Explain how warm pools, instance warm-up and custom AMIs speed up scale-out.
  • Explain health check grace periods, scale-in protection and standby.

Lifecycle hooks

Lifecycle hooks pause instances as they launch or terminate so that custom actions can run. A launch hook holds new instances in the Pending:Wait state, so scripts can install software, register the instance with external systems or warm caches before it enters service and receives traffic.

A termination hook holds instances in the Terminating:Wait state, so they can drain connections, finish jobs or copy logs before they disappear. A hook waits until it is completed, by the instance itself or by automation triggered through EventBridge, SNS or SQS notifications, or until its timeout expires, which is one hour by default.

The following diagram shows where each hook pauses an instance.

Auto Scaling lifecycle hooks. A launch hook holds a new instance in Pending:Wait while scripts install software, register it or warm caches before it enters service. A termination hook holds an instance in Terminating:Wait so it can drain connections, finish jobs or copy logs. Each hook waits until it is completed or its timeout expires, one hour by default.
An instance in Pending:Wait receives no traffic, so it enters service only when it is ready.
Scenario questions

Lifecycle hooks

Exam skills 3.2.2 · 2.2.1

Choose the lifecycle feature for each requirement.

Log in to try this activity Interactive activities are free with an account.

Speeding up scale-out

Scaling out helps only once new instances are ready to serve, and applications that take minutes to start can fall behind a sudden rise in load. Three features shorten or manage that delay. Warm pools keep instances that have already completed their launch work, stopped, hibernated or running, beside the group; scale-out takes one of them instead of launching from scratch, so it completes in seconds.

Default instance warm-up tells the group how long a new instance needs before its metrics should count. Without it, booting instances report low or no load, and the policy may launch even more instances than needed. Custom AMIs with the software already installed reduce the work done at boot, so every new instance starts faster. The following table summarises the features.

FeatureEffect
Warm poolsKeep pre-initialised instances stopped, hibernated or running, so scale-out takes seconds instead of minutes
Default instance warm-upStops new instances' metrics counting until they are ready, so booting instances do not cause over-scaling
Custom AMIsBake software into the image so less work happens at boot

The following diagram compares scale-out with and without a warm pool.

Scale-out with and without a warm pool. Without one, each new instance boots and runs its launch work, which can take minutes. With a warm pool, instances that already completed their launch work wait stopped, hibernated or running, and scale-out moves one into service in seconds.
Stopped pool instances are not charged for compute, only for their storage, which makes stopped the usual choice.

Health checks and grace periods

Several settings control how Auto Scaling treats new and existing instances. The health check grace period stops new instances from being marked unhealthy before they have finished starting; if it is too short, instances are terminated while still booting and the group cycles endlessly.

Scale-in protection stops chosen instances from being terminated during scale-in, for example while they process a long job. Instances can also be moved into standby, which removes them from service for troubleshooting or updates without the group replacing them, and returned to service afterwards.

Troubleshooting scenario

Endless replacement

Exam skills 2.2.3 · 3.2.2

Resolve Auto Scaling behaviour caused by start-up time.

Log in to try this activity Interactive activities are free with an account.
Worked example

Slow-starting instances and lost logs

A Java application takes eight minutes to start. During spikes, users see errors before new instances are ready, and logs are lost when instances scale in.

ProblemFix
Instances take eight minutes to be readyA warm pool of stopped, pre-initialised instances
Booting instances skew metrics and cause over-scalingDefault instance warm-up of 480 seconds
Instances marked unhealthy while startingA health check grace period above the start-up time
Logs lost on terminationA termination lifecycle hook that copies logs to S3 before completing

Scale-out now takes seconds, and every terminated instance hands over its logs first.

Check yourself

What does a warm pool do for an Auto Scaling group?

Show answer

It keeps pre-initialised instances, stopped or running, ready to join the group, so scale-out skips the slow start-up of applications that take a long time to initialise.

Exam Tips

Remember for the exam

  • To run a script before an instance terminates, use a lifecycle hook.
  • When long boot times delay scaling, use warm pools.
  • If the group adds too many instances while new ones boot, configure instance warm-up.
  • If new instances are terminated before they finish starting, increase the health check grace period.
  • To troubleshoot an instance without it being replaced, put it into standby.