Lesson 3 of 5 Amazon EC2 Auto Scaling

Managing Health Checks, Lifecycle Hooks and Warm Pools

This lesson explains how Auto Scaling replaces unhealthy instances, runs custom actions when instances launch or terminate, and reduces the time to add capacity with warm pools.

Learning outcomes

After completing this lesson, you will be able to:

  • Choose between EC2 and ELB health checks and set grace periods.
  • Run custom actions with lifecycle hooks.
  • Use warm pools.
  • Stop an endless replacement loop.

Health checks

By default Auto Scaling uses EC2 status checks: an instance is unhealthy if it is not running or fails a status check. Enabling ELB health checks on the group also marks instances unhealthy when the load balancer's target group reports them unhealthy, so an instance whose application has crashed is replaced. Custom health checks are also possible: a monitoring script can call SetInstanceHealth. The health check grace period delays health checks after launch; if it is shorter than the application's start-up time, new instances are marked unhealthy and replaced in an endless loop. Unhealthy instances are terminated and replaced; standby and suspended processes change this behaviour.

The following diagram shows what happens when the grace period is shorter than the start-up time.

Why the health check grace period must cover the start-up time. If an application needs four minutes to start but the grace period is 90 seconds, and the target group marks a target unhealthy after three failed 30-second checks, each instance is replaced before it can ever pass, in an endless loop. A grace period of 300 seconds covers the start-up.
Whenever the grace period is shorter than the start-up time, healthy instances are replaced as unhealthy.
Troubleshooting scenario

Replacement problems

Exam skills 2.1.1 · 2.2.1

Fix Auto Scaling health check problems.

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

Lifecycle hooks

A lifecycle hook pauses an instance in Pending:Wait during launch or Terminating:Wait during termination, for a heartbeat timeout of one hour by default (configurable up to two hours, and extendable with RecordLifecycleActionHeartbeat up to 48 hours in total). Hooks send a notification to EventBridge, SNS or SQS, and a Lambda function, Systems Manager Automation runbook or script performs work, such as installing software, registering with a configuration system, or copying logs off an instance before it is terminated. The worker then calls CompleteLifecycleAction with CONTINUE or ABANDON; if it does not, the default result is applied when the timeout expires.

aws autoscaling put-lifecycle-hook --auto-scaling-group-name web-asg \
  --lifecycle-hook-name drain-logs --lifecycle-transition autoscaling:EC2_INSTANCE_TERMINATING \
  --heartbeat-timeout 300 --default-result CONTINUE

# from the instance or a Lambda function when the work is done
aws autoscaling complete-lifecycle-action --auto-scaling-group-name web-asg \
  --lifecycle-hook-name drain-logs --instance-id i-0abc --lifecycle-action-result CONTINUE

1hour

Default lifecycle hook heartbeat timeout

2hours

Maximum configurable heartbeat timeout

Put in order

A termination lifecycle hook

Exam skills 2.1.1 · 3.2.2

Put the steps of a termination lifecycle hook in order.

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

Warm pools

A warm pool keeps pre-initialised instances that have already run their user data and lifecycle hooks in a Stopped, Hibernated or Running state outside the group's serving capacity. When the group scales out, it takes instances from the warm pool, which become ready much faster than new launches. Stopped instances incur only EBS storage cost. Warm pools suit applications with long initialisation, such as loading large datasets or compiling assets. Instances can be returned to the warm pool on scale-in instead of being terminated (instance reuse policy).

Worked example

Endless replacement loop

After a new release, an Auto Scaling group continuously launches and terminates instances. The activity history says an instance was taken out of service in response to an ELB system health check failure. The new application version takes four minutes to start, but the group's health check grace period is 90 seconds and the target group marks targets unhealthy after three failed checks at 30-second intervals. Raising the grace period to 300 seconds, and adding a launch lifecycle hook that completes only when the application's readiness endpoint responds, stops the loop.

Check yourself

How does a script tell Auto Scaling that a lifecycle hook action is finished?

Show answer

It calls CompleteLifecycleAction.

Exam Tips

Remember for the exam

  • Enable ELB health checks so instances with failed applications are replaced.
  • A grace period shorter than start-up time causes a launch-terminate loop.
  • Lifecycle hooks pause launch or termination for custom actions; complete them with CompleteLifecycleAction.
  • Warm pools hold pre-initialised instances for faster scale-out.