Sub topic 18
Troubleshooting unhealthy targets and scaling problems
This chapter assumes you know the basics of EC2, security groups, subnets, and the load balancer and Auto Scaling group setup from earlier chapters. It gives a repeatable way to diagnose two common problems: targets that stay unhealthy, and scaling that does not trigger.
Start with the target state
Check what the load balancer reports before you change anything. The function below lists each target and its state. If a target is unhealthy, the Reason field in the response gives the cause.
import boto3
elbv2 = boto3.client("elbv2")
def print_target_health(target_group_arn):
response = elbv2.describe_target_health(TargetGroupArn=target_group_arn)
for description in response["TargetHealthDescriptions"]:
target = description["Target"]["Id"]
state = description["TargetHealth"]["State"]
print(target, state)A state of healthy means the target passes checks. unhealthy means checks fail. draining means the target is being deregistered. initial means registration or the first checks are still in progress.
Why targets are unhealthy
Work through these causes in order. Most problems come from the first three.
- Security groups. The target's security group must allow the health check port from the load balancer's security group. The load balancer's own group must allow outbound traffic to the targets.
- Wrong path, port, or success codes. If
/healthreturns 404 or a redirect, the check fails. The default success code is 200. Confirm the path and port on the target group match what the application serves. - Application not listening. The process may have crashed, bound only to localhost, or still be starting up. Connect to the instance and request the health path locally.
- Slow responses. A response slower than the check timeout counts as a failure. Raise the timeout or make the endpoint cheaper.
- Wrong subnet or route. The load balancer needs a subnet in the target's Availability Zone, or the target never receives checks.
Instances that keep being replaced
A common loop appears when the Auto Scaling group uses --health-check-type ELB. The group terminates any instance the load balancer reports as unhealthy. If a new instance needs more than the grace period to boot, it is killed before it can pass a check, and the group launches another.
To see why the group acted, read its recent activity:
aws autoscaling describe-scaling-activities \
--auto-scaling-group-name web-asg \
--max-items 5The Cause and StatusMessage fields explain each launch or termination.
Scaling that does not trigger
When load rises but capacity stays flat, check these items:
- Maximum reached. The group never goes above
--max-size. Raise it if demand needs more. - Metric not reporting. A target tracking policy needs data. Confirm the metric has datapoints in CloudWatch.
- Wrong ResourceLabel. For
ALBRequestCountPerTarget, the label must match the load balancer and target group exactly. A mistake leaves the metric empty. - Target value too high. If the target is 1000 requests per target and traffic never reaches it, no scale-out happens.
- Warmup and cooldown. New instances are excluded from metrics while warming up, so scaling can look slow after a recent action.
- Launch failures. Capacity limits, a missing AMI, or an invalid subnet cause failed launches. These appear in the scaling activities.
The flow below summarizes the order of checks.
Exercise
An Auto Scaling group with ELB health checks launches an instance, terminates it after about five minutes, and repeats. The application needs eight minutes to start. What setting should you change, and to what?
Show answer
Raise the health check grace period above the eight-minute start time, for example to 600 seconds or more. The instance is currently marked unhealthy before the application is ready, so the group replaces it.