Sub topic 21
Troubleshooting Unhealthy Targets and Scaling Problems
This chapter assumes you have built the web tier from the earlier chapters: an Application Load Balancer, a target group with a health check, a launch template, and an Auto Scaling group with a target tracking policy. You should be comfortable with security groups, the AWS CLI, and reading CloudWatch alarms.
Troubleshoot along the request path
Most problems fall into one of two groups. Either the load balancer considers targets unhealthy, or the Auto Scaling group does not add, remove, or keep instances as you expect. Check the pieces in the order a request and a scaling signal travel:
- Can the load balancer reach the target on the health check port?
- Does the application answer the health check path with an accepted status code in time?
- Is the Auto Scaling group registering instances with the target group, and acting on health results?
- Is the metric reaching CloudWatch, and does the alarm move to the ALARM state?
- Is the group allowed to act (capacity limits, suspended processes, launch failures)?
Read the target health state first
The reason code explains most health problems. The following function prints the state, and also the reason and description when AWS provides them.
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"]
health = description["TargetHealth"]
print(
target,
health["State"],
health.get("Reason", "-"),
health.get("Description", ""),
)
if __name__ == "__main__":
# Replace with your target group ARN.
print_target_health(
"arn:aws:elasticloadbalancing:us-east-1:123456789012:"
"targetgroup/web-tg/943f017f100becff"
)| Reason code | Usual meaning | First thing to check |
|---|---|---|
| Elb.InitialHealthChecking | Registration is new and checks have not finished | Wait for the healthy threshold to be met |
| Target.Timeout | No answer within the timeout | Target security group, network ACL, application listening on the right port |
| Target.FailedHealthChecks | Connection worked but the check failed | Health check path, protocol, application errors |
| Target.ResponseCodeMismatch | Status code not in the success codes | Redirects (301/302), 401/403, 404 on the path |
| Target.NotInUse | Target group is not attached to a listener, or the target's AZ is not enabled | Listener rules, load balancer subnets |
| Target.DeregistrationInProgress | Target is draining | Normal during scale-in or deployments |
Common causes of unhealthy targets
Security groups. The load balancer sends health checks from its own network interfaces. The target's security group must allow inbound traffic from the load balancer's security group on the traffic port, or on the health check port if you override it. This is the most common cause of Target.Timeout.
Wrong port, protocol, or path. The default success code is 200. A path that redirects to HTTPS, requires a login, or does not exist returns 301, 302, 401, or 404 and fails the check. Create a dedicated lightweight path such as /health that returns 200 without calling a database.
Application not ready. Test from the instance itself:
curl -i http://localhost:80/healthIf this fails, the problem is the application or the user data script, not the load balancer. Check the service status and logs.
Thresholds and timeouts. The health check interval, timeout, healthy threshold, and unhealthy threshold decide how fast a target changes state. A timeout that is too short for a slow response marks a working target unhealthy. A very strict unhealthy threshold removes targets after one slow response.
Fail-open behavior. If every target in a target group is unhealthy, the load balancer sends requests to all targets anyway. Do not read "traffic still flows" as "the targets are fine."
Instances that keep being replaced
With --health-check-type ELB, the Auto Scaling group terminates instances that the load balancer reports as unhealthy once the grace period has ended. If the application needs longer to start than the grace period, new instances are terminated before they can pass a check, and the group launches replacements in a loop.
The scaling activity history shows why each instance was terminated:
aws autoscaling describe-scaling-activities \
--auto-scaling-group-name web-asg \
--max-items 10Scaling that does not trigger
Work through these checks in order.
- Capacity limits. Desired capacity can never exceed the maximum or fall below the minimum. A group already at its maximum of 6 will not scale out, and the activity history may show no attempt.
- Suspended processes. Run
aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names web-asgand look atSuspendedProcesses. Launch, Terminate, and AlarmNotification being suspended each stop scaling. - The policy and its alarms. Target tracking creates alarms named like
TargetTracking-web-asg-AlarmHigh-.... List them withaws cloudwatch describe-alarms --alarm-name-prefix TargetTracking-web-asg. An alarm stuck in INSUFFICIENT_DATA means the metric is not arriving. - The ResourceLabel. For
ALBRequestCountPerTarget, the label must match the load balancer and target group exactly (app/<lb-name>/<id>/targetgroup/<tg-name>/<id>), and the target group must be attached to the group. - Warmup and cooldown. During instance warmup, new instances are not counted fully in the metric, so the group may wait before adding more. This is intended.
- Scale-in is slower than scale-out. Target tracking scales in conservatively, so instances staying for a while after a traffic drop is normal.
Scaling that is attempted but fails
When the activity history shows status Failed, the StatusMessage field names the cause. Typical causes:
- The account's instance quota (vCPU limit) is reached.
- The chosen instance type has no capacity in a subnet's Availability Zone. Use several subnets and, where possible, several instance types.
- The launch template references an AMI, key pair, security group, or instance profile that was deleted or lives in another Region.
- The subnet has no free IP addresses.
Fix the underlying cause and the group retries on its own.
Exercise
An Auto Scaling group with an ELB health check launches instances that join the target group as Elb.InitialHealthChecking and are then terminated about five minutes later. The application takes eight minutes to start and the grace period is 300 seconds. What do you change, and where do you confirm the cause?
Show answer
Raise the health check grace period to more than the real startup time, for example 600 seconds, using update-auto-scaling-group. Confirm the cause in the scaling activity history (describe-scaling-activities), where the terminations are listed as unhealthy replacements, and by checking that the same instance passes the health check once the application is up.