Sub topic 7
Health checks for endpoints
This chapter assumes you can create hosted zones and records, know what a TTL is, and have used the AWS CLI. Health checks are the input that lets routing policies such as failover act on the state of your endpoints. Without a health check, Route 53 answers queries from the records alone and cannot tell whether the target is up.
What a Route 53 health check does
A health check is a Route 53 resource that probes an endpoint, or watches other resources, and reports healthy or unhealthy. Route 53 runs the probes from checkers in several AWS regions. It then combines their results into one status.
A health check does nothing on its own. It takes effect when you attach it to a record by setting HealthCheckId. When the check is unhealthy, Route 53 stops returning that record in answers and uses the routing policy to choose another one.
There are three types of health check:
| Type | What it monitors | Typical use |
|---|---|---|
| Endpoint | An IP address or domain name, over HTTP, HTTPS or TCP | A web server, load balancer or API |
| Calculated | The status of other health checks, combined with AND, OR or a minimum count | A service made of several parts |
| CloudWatch alarm | The state of a CloudWatch alarm | Metrics that Route 53 cannot probe, or endpoints inside a VPC |
How an endpoint check works
Checkers in multiple regions send requests to your endpoint at a fixed interval. Route 53 counts a check as healthy when more than 18 percent of the checkers report the endpoint healthy. Because of this threshold, one regional network problem does not flip the status.
The settings that control the probe are:
- Type:
HTTP,HTTPS,TCP, or the string-matching variantsHTTP_STR_MATCHandHTTPS_STR_MATCH. - Endpoint: an IP address, or a fully qualified domain name (FQDN). With an FQDN, Route 53 resolves it for each probe.
- Port and path: the port to connect to and, for HTTP types, the resource path such as
/health. - Request interval: 30 seconds (standard) or 10 seconds (fast). Fast checks cost more.
- Failure threshold: how many consecutive failed probes make the endpoint unhealthy. The value can be 1 to 10, and the default is 3.
A probe is successful for HTTP and HTTPS when the endpoint returns a status code from 200 up to, but not including, 400. It must also answer within the timeout. Route 53 must complete the TCP connection within 4 seconds, and for HTTP types it must receive the status code within 2 seconds after that. A redirect with status 301 or 302 therefore counts as healthy, even if the redirect target is broken.
Creating an endpoint health check
The following command creates an HTTPS check that requests /health every 30 seconds and marks the endpoint unhealthy after three consecutive failures.
aws route53 create-health-check \
--caller-reference "$(date +%s)" \
--health-check-config '{"Type":"HTTPS","FullyQualifiedDomainName":"www.example.com","Port":443,"ResourcePath":"/health","RequestInterval":30,"FailureThreshold":3}'The --caller-reference value must be unique for each new health check. If you resend a request with the same value, Route 53 treats it as a retry and does not create a second check. This protects you from duplicates when a network error makes you repeat a call. Using the timestamp, as above, is fine for manual use. In automation, use a stable identifier so that retries are safe.
The response contains the health check ID. Save it, because you need it to attach the check to a record and to read its status.
You can read the current status with the following command:
aws route53 get-health-check-status --health-check-id HEALTH_CHECK_IDThe output lists each checker region with its latest result. This is useful when a check flaps, because you can see whether failures come from all regions or from one.
Checking content with string matching
A web server can return status 200 with an error page. To catch this, use HTTP_STR_MATCH or HTTPS_STR_MATCH and set SearchString. Route 53 looks for the string in the first 5,120 bytes of the response body. If the string is missing, the probe fails.
{
"Type": "HTTPS_STR_MATCH",
"FullyQualifiedDomainName": "www.example.com",
"Port": 443,
"ResourcePath": "/health",
"SearchString": "status: ok",
"RequestInterval": 30,
"FailureThreshold": 3
}Keep the string near the top of the response, because content after the 5,120-byte limit is not examined.
Attaching a health check to a record
A record only reacts to a health check when two conditions hold. The record must reference the check through HealthCheckId. The record must also belong to a policy that can choose between answers, such as failover, weighted, latency or multivalue answer.
The failover pair below returns the primary address while its health check passes and the secondary address when it fails.
{
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "www.example.com",
"Type": "A",
"SetIdentifier": "primary",
"Failover": "PRIMARY",
"TTL": 60,
"HealthCheckId": "HEALTH_CHECK_ID",
"ResourceRecords": [{ "Value": "192.0.2.40" }]
}
},
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "www.example.com",
"Type": "A",
"SetIdentifier": "secondary",
"Failover": "SECONDARY",
"TTL": 60,
"ResourceRecords": [{ "Value": "192.0.2.50" }]
}
}
]
}Replace HEALTH_CHECK_ID with the ID from the earlier command. The next chapter covers the routing policies in detail. For now, note that the secondary record has no health check. If every record in a set is unhealthy, or none has a check, Route 53 returns the records as if they were all healthy, rather than returning nothing. Leaving the last-resort record without a check is a deliberate way to guarantee an answer.
How long failover takes
Failover time has three parts:
- Detection: roughly the request interval multiplied by the failure threshold. With a 30-second interval and a threshold of 3, that is about 90 seconds. A 10-second interval with a threshold of 3 is about 30 seconds.
- Propagation inside Route 53: the status change reaches the name servers within a short time.
- Cache expiry: resolvers hold the old answer until the TTL ends.
For a record with a 60-second TTL and a standard check, plan for about two to three minutes. Lowering the interval and threshold shortens detection but raises cost and the chance of false alarms from a single slow response.
Evaluating alias targets
Alias records can use EvaluateTargetHealth instead of a separate health check. When it is true, Route 53 treats the alias as healthy only if the AWS resource it points to is healthy. For an Application Load Balancer, that means it has at least one healthy registered target. This avoids creating and paying for a probe when the target already reports its own health.
EvaluateTargetHealth does not apply in the same way to every target. A CloudFront alias, for example, does not report health this way, so the CloudFront example earlier in this tutorial sets it to false.
Calculated and CloudWatch alarm checks
A calculated health check combines child checks. Set Type to CALCULATED, list the children in ChildHealthChecks, and set HealthThreshold to the number of children that must be healthy. A threshold of 1 behaves like OR. A threshold equal to the number of children behaves like AND.
aws route53 create-health-check \
--caller-reference "app-calculated-1" \
--health-check-config '{"Type":"CALCULATED","ChildHealthChecks":["CHILD_ID_1","CHILD_ID_2"],"HealthThreshold":2}'This pattern suits a service that needs both a web tier and an API tier to be up. Failure of either child makes the parent unhealthy.
A CloudWatch alarm check follows an alarm state. Use it when the endpoint is private. Route 53 checkers sit on the public internet, so they cannot reach an address inside a VPC. Instead, publish a metric from inside the VPC, create an alarm on it, and base the health check on the alarm. If the alarm has insufficient data, you choose whether the check reports healthy, unhealthy, or the last known status.
Common errors
- The check is always unhealthy. The endpoint may block the checker addresses. Allow inbound traffic from the published Route 53 health checker IP ranges in security groups, network ACLs and firewalls. Use
get-health-check-statusto see which regions fail. - The check is healthy but users see errors. A redirect or a generic error page can still return a status below 400. Use string matching and a dedicated
/healthpath that exercises real dependencies. - The record never fails over. Confirm the record has
HealthCheckId, that it belongs to a failover, weighted, latency or multivalue set, and that its TTL is short. - Using the root path with a Host mismatch. For HTTP types, Route 53 sends the FQDN as the
Hostheader. If the endpoint is an IP address and your server routes by host name, setFullyQualifiedDomainNameas well, so the correct site answers. - Health endpoint depends on everything. A
/healthpath that checks a database can take the whole site out of rotation when the database is slow. Decide which dependencies should cause failover. - Cost surprises. Each check has a monthly charge, with extra charges for fast intervals, string matching and HTTPS options. Share one check across several records when they point to the same endpoint.
Verifying behavior
After attaching a check, test the result instead of assuming it works. Stop the primary service or make /health return a 503. Then watch the status and query the name:
aws route53 get-health-check-status --health-check-id HEALTH_CHECK_ID
dig www.example.com A +shortThe status should change to failure first. Within the TTL, the dig answer should change to the secondary address. Restore the primary and confirm the answer returns.
Exercise
A failover record has a health check with a 30-second request interval and a failure threshold of 3, and its TTL is 300 seconds. The primary server goes down. Roughly how long can a client keep receiving the primary address, and what two changes would shorten it?
Show answer
Detection takes about 90 seconds (30 × 3), and a resolver that cached the answer just before the failure can keep it for up to 300 more seconds, so a client may see the dead address for about 6 minutes. Lower the record TTL to something like 60 seconds to shrink the cache part, and use a 10-second interval or a lower failure threshold to shrink the detection part.
Exercise
Your web server is in a private subnet with no public IP, and you want Route 53 to fail over when it stops responding. Why can't an endpoint health check do this, and what should you use instead?
Show answer
Route 53 health checkers probe from the public internet, so they cannot reach a private address. Publish a metric from inside the VPC, create a CloudWatch alarm on it, and create a CloudWatch alarm health check that follows the alarm state. Attach that health check to the record.