Sub topic 7
Health Checks and What Makes a Target Healthy
This chapter assumes you know what a target group is and how a listener forwards requests to it. It covers how a load balancer decides whether a target can receive traffic, and how Auto Scaling uses that decision.
How a health check works
A load balancer sends a request to each registered target on a fixed schedule. It sends traffic only to targets in the healthy state. The check is configured on the target group, not on the load balancer.
| Setting | Meaning | Typical value |
|---|---|---|
| Protocol and port | Where the probe goes | HTTP on the traffic port |
| Path | URL requested (HTTP/HTTPS only) | `/health` |
| Interval | Seconds between probes | 30 |
| Timeout | Seconds to wait for a reply | 5 |
| Healthy threshold | Consecutive successes to become healthy | 5 |
| Unhealthy threshold | Consecutive failures to become unhealthy | 2 |
| Success codes | HTTP codes counted as a pass | 200 |
A new target starts in initial. It becomes healthy after the healthy threshold is met. If it then fails the unhealthy threshold number of times in a row, it becomes unhealthy and the load balancer stops routing to it. It keeps probing, so the target can return to healthy when it recovers.
Choosing a good health check path
The path decides what "healthy" means. Pick it deliberately.
- A shallow check, such as returning 200 from
/healthwithout touching other systems, tells you the web process is running. - A deep check, which queries the database, tells you more. It can also cause every target to fail at once if the database is down. The load balancer then has no healthy targets, and a dependency outage becomes a full outage.
- Keep the endpoint fast and unauthenticated. A redirect (301/302) or a 401 does not match
200unless you widen the success codes.
For a Network Load Balancer, a TCP check only confirms that the port accepts connections. It does not show that the application behaves correctly.
Inspecting target health
Use this function as a first diagnostic step. It lists each target and its state.
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)Each description also has a Reason and Description for non-healthy states, such as Target.Timeout or Target.ResponseCodeMismatch. These point to the cause: a security group block, a wrong port, or an unexpected status code.
Health checks and Auto Scaling
By default, an Auto Scaling group uses EC2 status checks only. An instance whose application has crashed still passes them, so it stays in service. Setting the health check type to ELB makes the group also act on the load balancer's verdict:
aws autoscaling update-auto-scaling-group \
--auto-scaling-group-name web-asg \
--health-check-type ELB \
--health-check-grace-period 300The grace period is the time after launch during which the group ignores failed checks. Set it longer than your instance's boot and application startup time. If it is too short, the group terminates instances that are still starting, and it can loop through launch and terminate cycles.
Exercise
An Auto Scaling group with the default health check type has an instance whose web server crashed. The instance is still running, and the target shows unhealthy. Why is it not replaced, and what change fixes it?
Show answer
The group uses EC2 status checks, which pass while the instance is running even if the application is dead. Set the group's health check type to ELB so it replaces instances the load balancer reports as unhealthy, and set a grace period longer than the startup time.