Learning center

Auto Scaling replaces new instances for failing ELB health checks

When new instances are terminated soon after launch, the termination activity's cause text names the health check that removed them: ELB or EC2 status. For an ELB replacement, compare the group's grace period with how long a new target takes to pass its first checks. A target that would pass a few minutes later points at the grace period; a target that never passes while old instances stay healthy points at the release.

Read the cause text first#

Every replacement leaves two scaling activities: one terminating the unhealthy instance and one launching its replacement. The termination activity's Cause names the check. AWS's troubleshooting page for health check replacements is organized by these texts. Unhealthy instance replacement · Troubleshoot unhealthy instances

Cause containsCheck that failedFirst read
an instance was taken out of service in response to an ELB system health check failureA load balancer target group reported the instance unhealthyGroup grace period and target health
an instance was taken out of service in response to an EC2 instance status check failureEC2 status checks reported the instance impairedInstance and system status
an instance was taken out of service in response to an EC2 health check that indicated it had been terminated or stoppedThe instance left the running stateWhether the instance shut itself down or someone stopped it

The first row happens only when the group's health check type includes ELB. By default a group ignores load balancer results; EC2 status checks are always on. With several target groups attached, any one reporting the instance unhealthy is enough for replacement. Elastic Load Balancing health checks · CreateAutoScalingGroup

Use a read-only profile and replace the uppercase placeholders. describe-scaling-activities

aws autoscaling describe-scaling-activities \
  --profile PROFILE --region REGION --auto-scaling-group-name GROUP \
  --query 'Activities[:30].{start:StartTime,end:EndTime,status:StatusCode,description:Description,cause:Cause}' \
  --output json --no-cli-pager

Pair each terminated instance's launch with its termination by the instance ID in Description. The gap between the two start times is the first number to compare with the grace period.

Read the grace period and the target group's thresholds#

aws autoscaling describe-auto-scaling-groups \
  --profile PROFILE --region REGION --auto-scaling-group-names GROUP \
  --query 'AutoScalingGroups[0].{type:HealthCheckType,grace:HealthCheckGracePeriod,warmup:DefaultInstanceWarmup,targetGroups:TargetGroupARNs,instances:Instances[].{id:InstanceId,state:LifecycleState,health:HealthStatus,version:LaunchTemplate.Version}}' \
  --output json --no-cli-pager

aws elbv2 describe-target-groups \
  --profile PROFILE --region REGION --target-group-arns TARGET_GROUP_ARN \
  --query 'TargetGroups[0].{path:HealthCheckPath,port:HealthCheckPort,interval:HealthCheckIntervalSeconds,timeout:HealthCheckTimeoutSeconds,healthyAfter:HealthyThresholdCount,unhealthyAfter:UnhealthyThresholdCount,codes:Matcher.HttpCode}' \
  --output json --no-cli-pager

The grace period is the time Auto Scaling waits after an instance enters InService before it acts on a failed health check. The load balancer starts checking earlier, when the instance registers. A group created in the console gets 300 seconds by default; a group created with the CLI or an SDK gets 0. How long the checks take depends on the target's state. AWS says a newly registered target must pass one health check to be considered healthy. A target that fails UnhealthyThresholdCount checks first, because the application is still starting, turns unhealthy, and then needs HealthyThresholdCount consecutive successes to recover. For HTTP target groups the defaults are two failures to turn unhealthy and five successes to recover, at a 30-second interval, so an application that takes more than a minute to answer needs about two and a half more minutes of passing checks. Health check grace period · Target group health checks · Troubleshoot unhealthy instances · CreateTargetGroup

That gives a rough budget. If the application answers before the target has failed UnhealthyThresholdCount checks, it is boot time, plus application start, plus one HealthCheckIntervalSeconds. If it answers later, add HealthyThresholdCount times the interval instead. A grace period under that budget replaces instances that would have become healthy, and one far above it only delays replacing instances that are really broken.

Watch a new target before it is replaced#

Read target health for the new instance IDs while they are still registered, and compare them with the old instances in the same target group. describe-target-health · TargetHealth

aws elbv2 describe-target-health \
  --profile PROFILE --region REGION --target-group-arn TARGET_GROUP_ARN \
  --query 'TargetHealthDescriptions[].{target:Target.Id,port:Target.Port,state:TargetHealth.State,reason:TargetHealth.Reason,description:TargetHealth.Description}' \
  --output table --no-cli-pager

Elb.InitialHealthChecking at the moment of replacement means the load balancer had not finished deciding. Target.Timeout, Target.FailedHealthChecks or Target.ResponseCodeMismatch on new targets while old targets stay healthy means the new instances answered badly or not at all.

To see whether a target would pass if left alone, keep one replacement alive. This is a change: suspending the ReplaceUnhealthy process stops the group from replacing unhealthy instances until you resume it, which AWS recommends when you do not want instances replaced. Resume it when you have the answer. Health check considerations · suspend-processes

# Change: pauses health replacement for the whole group.
aws autoscaling suspend-processes \
  --profile PROFILE --region REGION --auto-scaling-group-name GROUP \
  --scaling-processes ReplaceUnhealthy

# Change: restores health replacement.
aws autoscaling resume-processes \
  --profile PROFILE --region REGION --auto-scaling-group-name GROUP \
  --scaling-processes ReplaceUnhealthy

For the EC2 rows of the table, read status checks instead. Auto Scaling tolerates an occasional impaired result and waits a few minutes, but treats an instance outside running as an immediate failure, even during the grace period. describe-instance-status · Amazon EC2 health checks

aws ec2 describe-instance-status \
  --profile PROFILE --region REGION --instance-ids INSTANCE_ID --include-all-instances \
  --query 'InstanceStatuses[].{id:InstanceId,state:InstanceState.Name,instance:InstanceStatus.Status,system:SystemStatus.Status}' \
  --output json --no-cli-pager

What the reads mean#

CauseNew target, kept aliveOld targetsReading
ELBPasses a few minutes after it would have been replacedHealthyGrace period shorter than boot, start and the checks the target needs
ELBNever passes; ResponseCodeMismatch or TimeoutHealthyThe release: application, user data, port or network of the new version
ELBNever passesFailing tooHealth check settings, the load balancer's path to instances, or a dependency the check calls
EC2 status checkAnyAnyInstance or system impairment; the load balancer is not involved
Terminated or stoppedAnyAnyThe instance left running, for example a shutdown from its own operating system

For the third row on a Network Load Balancer, the security group path is one lead to check: see NLB targets unhealthy after a security group change. If targets pass but clients see gateway timeouts, the problem has moved to routing: see ALB 504 after a deploy.

A fictional fixture#

The group orders-asg was created with the CLI, uses ELB health checks and has a grace period of 60 seconds. The orders service takes about four minutes to start, and the target group uses the HTTP defaults. Version 8 of the launch template starts rolling out at 10:00.

At 10:03 the first new instance is terminated with the ELB cause text. Target health read at 10:02 showed initial with Elb.InitialHealthChecking. With ReplaceUnhealthy suspended, the next new instance turns healthy at 10:07, and the old instances stayed healthy throughout. The application took four minutes to answer, so each target failed two checks first, turned unhealthy and needed five successes to recover: about four minutes of start plus two and a half minutes of checks, so 60 seconds could never work. Raising the grace period to around 420 seconds, or adding a launch lifecycle hook that completes once the service answers, fixes the rollout.

In a second version of the same morning, the kept instance shows Target.ResponseCodeMismatch with code 500 for fifteen minutes while version 7 instances stay healthy. A longer grace period would only delay each replacement. Roll back version 8 and read its application logs.

Fix and confirm#

Raise the grace period with update-auto-scaling-group --health-check-grace-period, a change, to cover the budget above. AWS also suggests a launch lifecycle hook: the instance registers with the load balancer only when the hook completes, and with a launch hook AWS says the grace period can be 0. Set the default instance warmup as well, which AWS strongly recommends alongside the grace period. Keep the grace period no longer than needed; AWS notes that a high value reduces the effectiveness of health checks. If you use a hook, check that it completes, as the lifecycle hook guide describes. Health check grace period · Lifecycle hooks · Default instance warmup

On the next rollout, confirm that each new target reaches healthy, no termination activity carries the ELB cause text, and HealthStatus on the new instances stays Healthy. Resume ReplaceUnhealthy if you suspended it.

Where this reading stops#

Target health is a snapshot of targets registered now; a replaced instance has already left it. The cause text names the check, not why the check failed. When every instance is reported unhealthy by the load balancer, AWS says the group may replace fewer of them at a time than its usual 10 percent, so a slow trickle of replacements can mean everything is failing. For the other scaling and health signatures, see the signature table, and for a refresh that failed because new instances never became healthy, see instance refresh failures. How Auto Scaling minimizes downtime

Questions#

How long should the health check grace period be for an Auto Scaling group with ELB health checks?

Long enough for boot, application start and the checks the target then needs. A newly registered target needs one passing check. If it failed enough checks while starting to turn unhealthy, it needs the healthy threshold times the interval, which with HTTP defaults is about two and a half minutes after the application first answers.

What is the difference between an ELB health check failure and an EC2 status check failure in Auto Scaling?

An ELB failure means a target group reported the instance unhealthy, and it applies only when the group's health check type includes ELB. An EC2 status check failure means EC2 found the instance or its host impaired, and it applies to every group.

Will a longer grace period fix new instances that fail health checks?

Only if they would pass later. Keep one new instance alive and watch its target health; if it never passes while old instances stay healthy, the release is the cause and a longer grace period only delays each replacement.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading