The gap
Two public services run on an always-on device at home. I wanted to know within minutes if either stopped answering, and I wanted the check to come from outside my network. Route 53 health checks do that: sixteen checkers around the world request a URL every thirty seconds and vote. My infrastructure tool had no command to create one, so I went to add it, and read the documentation closely enough to notice the problem with the obvious design.
A health check records its status as a CloudWatch metric,
HealthCheckStatus in the AWS/Route53
namespace. That is all it does. It does not notify anyone. Unless
something watches that metric and publishes somewhere, a failed
check is a red icon on a console page nobody has open. So the
command I wrote does not create a health check. It creates a health
check and its alarm, as one unit, and refuses to pretend otherwise:
run it without a topic and it prints that the check has no alarm and
will notify nobody.
The unit
The check. HTTPS by default, port 443, a resource
path, SNI on for HTTPS, thirty-second interval, three consecutive
failures before a checker calls the endpoint unhealthy. An optional
search string switches the type to string matching. Every check is
tagged Name, and the command looks checks up by that
tag before creating anything, so running it twice creates one check
and re-asserts one alarm. One detail from the tag API: it accepts at
most ten resource IDs per call, so the lookup batches in tens.
The alarm. This is the part that turns a metric into an email. The settings, in full:
def alarm_kwargs(name, hc_id, topic_arn):
return {
"AlarmName": f"r53-hc-{name}",
"Namespace": "AWS/Route53",
"MetricName": "HealthCheckStatus",
"Dimensions": [{"Name": "HealthCheckId", "Value": hc_id}],
"Statistic": "Minimum",
"Period": 60,
"EvaluationPeriods": 2,
"Threshold": 1,
"ComparisonOperator": "LessThanThreshold",
"TreatMissingData": "breaching",
"AlarmActions": [topic_arn],
"OKActions": [topic_arn],
}
Read it as a sentence: if the minimum status over a one-minute period is below 1, meaning at least one evaluation saw the check unhealthy, for two periods in a row, alarm; publish to the topic; and publish again when it recovers, so the recovery email arrives without anyone checking.
Missing data counts as breaching. This is the one setting people argue with. If the check stops reporting at all, which is itself a failure, the default would hold the alarm in whatever state it was in last, which is usually OK. I would rather hear about it. The price is that a brand-new alarm starts in ALARM for about two minutes, until the first datapoint lands, then flips to OK on its own. The first email you get from a new check is a false alarm, by design, and the second is the recovery. Accept that.
The metric lives in one region. Route 53 is a
global service, and its health-check metrics are published only in
the partition's home region: us-east-1 in the standard
partition, a different home region in the other partition. An alarm
created anywhere else watches an empty metric and, with missing
data breaching, stays in ALARM forever. The tool picks the metric
region from the partition it is talking to, and that choice has its
own unit test.
What to point it at. Check the origin, not the CDN, because a CDN in front of a dead origin answers "successfully" with an error page and the check stays green. One more thing I learned from the documentation: Route 53 counts 2xx and 3xx responses as healthy, so a health route that answers with a login redirect is a valid target, and a route that answers 401 is not.
Proof it ran
Two checks, created with the command, each with its alarm pointed
at one topic. Both went OK after the first datapoint, which is what
the missing-data rule predicts for a new alarm. The list command shows each
check with its endpoint, the checker consensus as a count out of
sixteen, and the paired alarm's state next to it, so a check whose
alarm has gone missing reads NO ALARM in the same line.
Thirteen unit tests pin the config shape, the alarm arguments above,
the name lookup, the batching, and that a dry run makes no calls.
The third piece, and the lie
An alarm publishes to an SNS topic. A topic delivers to its
subscriptions. An email subscription starts in
PendingConfirmation and delivers nothing until the
recipient clicks the link in the confirmation email. So far, so
documented. The problem was in my own tool's subscribe command,
which printed subscribed and confirmed the moment it
subscribed my address, and I believed it.
The cause is one flag. The Subscribe API has an option,
ReturnSubscriptionArn, that makes it return the
subscription's ARN immediately. Without the flag, an unconfirmed
email subscription answers with the literal string
pending confirmation instead of an ARN. The command
passed the flag, got an ARN back, and treated any ARN as confirmed.
It was reporting that the call succeeded and calling that a
confirmed subscription. Those are different facts.
Two fixes work. Drop the flag and treat only
a string that starts with arn: as confirmed:
def subscription_confirmed(subscription_arn):
return subscription_arn.startswith("arn:")
resp = sns.subscribe(TopicArn=arn, Protocol="email", Endpoint=endpoint)
if subscription_confirmed(resp.get("SubscriptionArn", "")):
print(f"subscribed and confirmed: {endpoint}")
else:
print(f"subscribed: {endpoint}, PENDING, recipient must click the link")
Or keep the flag and ask SNS directly: call
GetSubscriptionAttributes on the returned ARN and read
PendingConfirmation, which is the string
true until the click. Either way, the list command
prints PENDING next to the subscription and a count of
pending subscriptions beside the topic, so the state is visible
without trusting the subscribe output. Four unit tests make sure a
pending response is never reported as confirmed.
Two more rules about pending subscriptions that I did not know until I hit them. AWS will not let you delete an unconfirmed subscription; it expires on its own after three days. And since a topic delivering to nobody is exactly how an outage goes unnoticed, the unsubscribe command now refuses to remove the last confirmed subscriber of a topic unless forced.
My own mistake here is simple to state. On the day I created the checks and alarms, the subscription was pending, and I did not click the confirmation until the next day. For that day, two correct alarms published into a topic that delivered to no one, and every tool I had said things were fine.
The checklist
- Never create a health check without creating its alarm in the same step. Make the tool refuse, or at least say so loudly.
- Alarm on
HealthCheckStatus, minimum below 1, two one-minute periods, missing data breaching, alarm and OK actions to the topic. - Create the alarm in the partition's home region, or it watches nothing.
- Expect the first alarm from a new check to be a false alarm that clears in two minutes.
- List the topic's subscriptions and read the word confirmed yourself. A subscribe call returning an ARN proves nothing.
- Click the confirmation link the same hour, not the next day.