carlos / platform Public

region-host: status-check alarms notify; reboot on instance check

On 2026-11-09 dev dennis-stenciling was impaired for about nine hours
and nobody heard: the auto-recover alarm's only action was ec2:recover,
which fired and failed silently.

- auto-recover alarm: add the alerts topic to alarm_actions and
  ok_actions (ec2:recover kept).
- new <name>-instance-check alarm per fixed box:
  StatusCheckFailed_Instance, 2 of 2 x 60 s, missing data = missing,
  alarm -> ec2:reboot + topic, ok -> topic. Same gate as auto-recover.
- new input health_alerts_topic_arn: a same-region topic for these two
  alarms only (no memory alarm, no publish grant). Empty falls back to
  the alerts_topic_arn / alerts_email topic.

Tests: tests/health_alarms.tftest.hcl (9 runs; two mutations caught).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 846784f0ec4d63b08a14849b3ea80acc63c205ae parent 32c3f2a
4 files changed, +260 −2
  • infra/modules/region-host/alert_publish.tf +1 −0
  • infra/modules/region-host/main.tf +31 −1
  • infra/modules/region-host/tests/health_alarms.tftest.hcl +213 −0
  • infra/modules/region-host/variables.tf +15 −1

CI cancelled — superseded by a newer push — run details

diff --git a/infra/modules/region-host/alert_publish.tf b/infra/modules/region-host/alert_publish.tf
index 8bce58495..4d42d1f84 100644
--- a/infra/modules/region-host/alert_publish.tf
+++ b/infra/modules/region-host/alert_publish.tf
@@ -2,6 +2,7 @@ locals {
alerts_enabled = var.alerts_topic_arn != "" || var.alerts_email != ""
alerts_topic_arn = var.alerts_topic_arn != "" ? var.alerts_topic_arn : one(aws_sns_topic.alerts[*].arn)
alerts_actions = local.alerts_enabled ? [local.alerts_topic_arn] : []
+ health_alerts_actions = var.health_alerts_topic_arn != "" ? [var.health_alerts_topic_arn] : local.alerts_actions
alert_publish_topic_arns = setunion(var.alert_publish_topic_arns, toset(local.alerts_actions))
}
diff --git a/infra/modules/region-host/main.tf b/infra/modules/region-host/main.tf
index 5ebe466f0..7eee0ad32 100644
--- a/infra/modules/region-host/main.tf
+++ b/infra/modules/region-host/main.tf
@@ -607,6 +607,11 @@ resource "aws_eip" "host" {
# HA ladder rung 0 (2026-08-12 spec): system-status failure recovers the
# instance onto healthy hardware, keeping instance id, EIP and volumes.
+#
+# Both status alarms also tell someone (local.health_alerts_actions). On
+# 2026-11-09 a dev box sat impaired for about nine hours: the recover action
+# fired, failed, and nothing else happened, because these alarms had no
+# notification. An alarm with only an EC2 action fails silently.
resource "aws_cloudwatch_metric_alarm" "host_auto_recover" {
count = var.mode == "fixed" && var.enable_auto_recovery ? 1 : 0
alarm_name = "${var.name}-auto-recover"
@@ -618,13 +623,38 @@ resource "aws_cloudwatch_metric_alarm" "host_auto_recover" {
evaluation_periods = 2
threshold = 1
comparison_operator = "GreaterThanOrEqualToThreshold"
- alarm_actions = ["arn:aws:automate:${data.aws_region.current.region}:ec2:recover"]
+ alarm_actions = concat(["arn:aws:automate:${data.aws_region.current.region}:ec2:recover"], local.health_alerts_actions)
+ ok_actions = local.health_alerts_actions
dimensions = {
InstanceId = aws_instance.host[0].id
}
tags = { Name = "${var.name}-auto-recover" }
}
+# The other half of the status checks: the hardware is fine but the guest has
+# stopped answering (kernel hang, exhausted memory, broken network config).
+# EC2's own remedy for that is a reboot; recover does not apply.
+resource "aws_cloudwatch_metric_alarm" "host_instance_check" {
+ count = var.mode == "fixed" && var.enable_auto_recovery ? 1 : 0
+ alarm_name = "${var.name}-instance-check"
+ alarm_description = "EC2 instance-status-check failure on ${var.name} for 2 minutes: the box is rebooted. If it fails again after the reboot, read the instance's console output."
+ namespace = "AWS/EC2"
+ metric_name = "StatusCheckFailed_Instance"
+ statistic = "Maximum"
+ period = 60
+ evaluation_periods = 2
+ datapoints_to_alarm = 2
+ threshold = 1
+ comparison_operator = "GreaterThanOrEqualToThreshold"
+ treat_missing_data = "missing"
+ alarm_actions = concat(["arn:aws:automate:${data.aws_region.current.region}:ec2:reboot"], local.health_alerts_actions)
+ ok_actions = local.health_alerts_actions
+ dimensions = {
+ InstanceId = aws_instance.host[0].id
+ }
+ tags = { Name = "${var.name}-instance-check" }
+}
+
# Memory-pressure spec §2.4: the early warning the status checks can't give.
# On 2026-08-28 the flagship thrashed for 15 minutes with EC2 status ok/ok;
# MemAvailable would have crossed 15% minutes earlier. Missing data is
diff --git a/infra/modules/region-host/tests/health_alarms.tftest.hcl b/infra/modules/region-host/tests/health_alarms.tftest.hcl
new file mode 100644
index 000000000..09c41242f
--- /dev/null
+++ b/infra/modules/region-host/tests/health_alarms.tftest.hcl
@@ -0,0 +1,213 @@
+# Plan-time tests for the fixed host's EC2 status-check alarms: each one
+# keeps its EC2 action and tells someone. mock_provider: no AWS account.
+mock_provider "aws" {
+ mock_data "aws_region" { defaults = { region = "eu-west-1" } }
+}
+
+override_data {
+ target = data.aws_vpcs.default
+ values = { ids = ["vpc-0aaaaaaaaaaaaaaaa"] }
+}
+
+override_data {
+ target = data.aws_caller_identity.current
+ values = { account_id = "123456789012" }
+}
+
+# The termination queue's ARN feeds an EventBridge target, which the provider
+# validates as an ARN; the mock's random string is not one.
+override_resource {
+ target = aws_sqs_queue.terminations
+ values = { arn = "arn:aws:sqs:eu-west-1:123456789012:carlos-test-host-terminations" }
+}
+
+variables {
+ name = "carlos-test-ie"
+ sg_description = "carlos test host"
+ iam_name = "carlos-test-host"
+ instance_type = "t4g.small"
+ root_volume_size = 20
+ user_data = "#!/bin/sh\n"
+ deployment_bucket_arn = "arn:aws:s3:::carlos-test-platform"
+ data_bucket_arn = "arn:aws:s3:::carlos-test-ie"
+ ssm_parameter_arn = "arn:aws:ssm:eu-west-1:123456789012:parameter/carlos/*"
+}
+
+
+override_data {
+ target = data.aws_ami.debian_arm64
+ values = { id = "ami-0123456789abcdef0" }
+}
+override_data {
+ target = data.aws_vpc.host
+ values = { cidr_block = "10.0.0.0/16" }
+}
+override_resource {
+ target = aws_instance.host
+ values = { id = "i-0123456789abcdef0" }
+}
+override_resource {
+ target = aws_security_group.host
+ values = { id = "sg-0123456789abcdef0" }
+}
+override_resource {
+ target = aws_iam_role.host
+ values = { arn = "arn:aws:iam::123456789012:role/carlos-test-host" }
+}
+override_resource {
+ target = aws_eip.host
+ values = { public_ip = "192.0.2.10" }
+}
+override_resource {
+ target = aws_sns_topic.alerts
+ values = { arn = "arn:aws:sns:eu-west-1:123456789012:carlos-test-ie-alerts" }
+}
+
+run "status_alarms_without_any_topic" {
+ command = plan
+ assert {
+ condition = (
+ aws_cloudwatch_metric_alarm.host_auto_recover[0].alarm_actions == toset(["arn:aws:automate:eu-west-1:ec2:recover"]) &&
+ length(aws_cloudwatch_metric_alarm.host_auto_recover[0].ok_actions) == 0 &&
+ aws_cloudwatch_metric_alarm.host_instance_check[0].alarm_actions == toset(["arn:aws:automate:eu-west-1:ec2:reboot"]) &&
+ length(aws_cloudwatch_metric_alarm.host_instance_check[0].ok_actions) == 0
+ )
+ error_message = "with no topic configured the status alarms keep only their EC2 actions"
+ }
+ assert {
+ condition = (
+ aws_cloudwatch_metric_alarm.host_instance_check[0].alarm_name == "carlos-test-ie-instance-check" &&
+ aws_cloudwatch_metric_alarm.host_instance_check[0].namespace == "AWS/EC2" &&
+ aws_cloudwatch_metric_alarm.host_instance_check[0].metric_name == "StatusCheckFailed_Instance" &&
+ aws_cloudwatch_metric_alarm.host_instance_check[0].statistic == "Maximum" &&
+ aws_cloudwatch_metric_alarm.host_instance_check[0].period == 60 &&
+ aws_cloudwatch_metric_alarm.host_instance_check[0].evaluation_periods == 2 &&
+ aws_cloudwatch_metric_alarm.host_instance_check[0].datapoints_to_alarm == 2 &&
+ aws_cloudwatch_metric_alarm.host_instance_check[0].threshold == 1 &&
+ aws_cloudwatch_metric_alarm.host_instance_check[0].comparison_operator == "GreaterThanOrEqualToThreshold" &&
+ aws_cloudwatch_metric_alarm.host_instance_check[0].treat_missing_data == "missing" &&
+ aws_cloudwatch_metric_alarm.host_instance_check[0].dimensions == tomap({ InstanceId = "i-0123456789abcdef0" })
+ )
+ error_message = "instance-check alarm must watch this instance's instance status check, 2 of 2 one-minute periods"
+ }
+ assert {
+ condition = (
+ aws_cloudwatch_metric_alarm.host_auto_recover[0].metric_name == "StatusCheckFailed_System" &&
+ aws_cloudwatch_metric_alarm.host_auto_recover[0].period == 60 &&
+ aws_cloudwatch_metric_alarm.host_auto_recover[0].evaluation_periods == 2 &&
+ aws_cloudwatch_metric_alarm.host_auto_recover[0].dimensions == tomap({ InstanceId = "i-0123456789abcdef0" })
+ )
+ error_message = "auto-recover alarm's metric and timing must not change"
+ }
+}
+
+run "status_alarms_use_the_alerts_email_topic" {
+ command = plan
+ variables {
+ alerts_email = "ops@example.com"
+ }
+ assert {
+ condition = (
+ aws_cloudwatch_metric_alarm.host_auto_recover[0].alarm_actions == toset(["arn:aws:automate:eu-west-1:ec2:recover", "arn:aws:sns:eu-west-1:123456789012:carlos-test-ie-alerts"]) &&
+ aws_cloudwatch_metric_alarm.host_auto_recover[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:carlos-test-ie-alerts"]) &&
+ aws_cloudwatch_metric_alarm.host_instance_check[0].alarm_actions == toset(["arn:aws:automate:eu-west-1:ec2:reboot", "arn:aws:sns:eu-west-1:123456789012:carlos-test-ie-alerts"]) &&
+ aws_cloudwatch_metric_alarm.host_instance_check[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:carlos-test-ie-alerts"])
+ )
+ error_message = "status alarms must notify the module's alerts topic on alarm and on clear"
+ }
+}
+
+run "status_alarms_use_the_alerts_topic_arn" {
+ command = plan
+ variables {
+ alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"
+ }
+ assert {
+ condition = (
+ aws_cloudwatch_metric_alarm.host_auto_recover[0].alarm_actions == toset(["arn:aws:automate:eu-west-1:ec2:recover", "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"]) &&
+ aws_cloudwatch_metric_alarm.host_auto_recover[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"]) &&
+ aws_cloudwatch_metric_alarm.host_instance_check[0].alarm_actions == toset(["arn:aws:automate:eu-west-1:ec2:reboot", "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"]) &&
+ aws_cloudwatch_metric_alarm.host_instance_check[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"])
+ )
+ error_message = "status alarms must notify the selected alerts topic"
+ }
+}
+
+# The dev case: a box with no memory alarm and no publish grant, told through
+# a topic the deployment owns.
+run "health_topic_alone_arms_nothing_else" {
+ command = plan
+ variables {
+ health_alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts"
+ }
+ assert {
+ condition = (
+ aws_cloudwatch_metric_alarm.host_auto_recover[0].alarm_actions == toset(["arn:aws:automate:eu-west-1:ec2:recover", "arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts"]) &&
+ aws_cloudwatch_metric_alarm.host_auto_recover[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts"]) &&
+ aws_cloudwatch_metric_alarm.host_instance_check[0].alarm_actions == toset(["arn:aws:automate:eu-west-1:ec2:reboot", "arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts"]) &&
+ aws_cloudwatch_metric_alarm.host_instance_check[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts"])
+ )
+ error_message = "status alarms must notify the health topic"
+ }
+ assert {
+ condition = (
+ length(aws_cloudwatch_metric_alarm.host_memory_low) == 0 &&
+ length(aws_iam_role_policy.alert_publish) == 0 &&
+ length(aws_sns_topic.alerts) == 0 &&
+ output.alerts_topic_arn == null
+ )
+ error_message = "the health topic must not arm the memory alarm, grant publish, or create a topic"
+ }
+}
+
+run "health_topic_overrides_the_alerts_topic" {
+ command = plan
+ variables {
+ alerts_email = "ops@example.com"
+ health_alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts"
+ }
+ assert {
+ condition = (
+ aws_cloudwatch_metric_alarm.host_auto_recover[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts"]) &&
+ aws_cloudwatch_metric_alarm.host_instance_check[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts"]) &&
+ aws_cloudwatch_metric_alarm.host_memory_low[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:carlos-test-ie-alerts"])
+ )
+ error_message = "the health topic takes the status alarms; the memory alarm keeps the alerts topic"
+ }
+}
+
+run "auto_recovery_off_arms_neither_status_alarm" {
+ command = plan
+ variables {
+ enable_auto_recovery = false
+ health_alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts"
+ }
+ assert {
+ condition = length(aws_cloudwatch_metric_alarm.host_auto_recover) == 0 && length(aws_cloudwatch_metric_alarm.host_instance_check) == 0
+ error_message = "enable_auto_recovery = false must arm neither status alarm"
+ }
+}
+
+run "health_topic_wrong_region" {
+ command = plan
+ variables {
+ health_alerts_topic_arn = "arn:aws:sns:eu-west-2:123456789012:dev-ha-alerts"
+ }
+ expect_failures = [var.health_alerts_topic_arn]
+}
+
+run "health_topic_wildcard" {
+ command = plan
+ variables {
+ health_alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:dev-ha-*"
+ }
+ expect_failures = [var.health_alerts_topic_arn]
+}
+
+run "health_topic_subscription_suffix" {
+ command = plan
+ variables {
+ health_alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts:0f1e2d3c-aaaa-bbbb-cccc-123456789012"
+ }
+ expect_failures = [var.health_alerts_topic_arn]
+}
diff --git a/infra/modules/region-host/variables.tf b/infra/modules/region-host/variables.tf
index 7de97fd8d..ad57700c7 100644
--- a/infra/modules/region-host/variables.tf
+++ b/infra/modules/region-host/variables.tf
@@ -195,7 +195,7 @@ variable "steering_record_names" {
}
variable "enable_auto_recovery" {
- description = "Arm an EC2 auto-recovery alarm on the host (HA ladder rung 0)."
+ description = "Arm the host's EC2 status-check alarms (HA ladder rung 0): a system-check failure recovers the instance, an instance-check failure reboots it."
type = bool
default = true
}
@@ -230,6 +230,20 @@ variable "alerts_topic_arn" {
}
}
+variable "health_alerts_topic_arn" {
+ description = "SNS topic ARN told when a fixed host's EC2 status-check alarms fire or clear. Empty means the alerts topic from alerts_topic_arn or alerts_email, when one is set. Unlike alerts_topic_arn, this input arms no memory alarm and grants the host no publish right. The topic must be in this module's AWS region; its policy must let CloudWatch alarms publish, and the caller manages it."
+ type = string
+ default = ""
+ nullable = false
+ validation {
+ condition = var.health_alerts_topic_arn == "" || (
+ can(regex("^arn:aws(-[a-z]+)*:sns:[a-z]{2}(-[a-z]+)+-[0-9]+:[0-9]{12}:([A-Za-z0-9_-]{1,256}|[A-Za-z0-9_-]{1,251}\\.fifo)$", var.health_alerts_topic_arn)) &&
+ try(split(":", var.health_alerts_topic_arn)[3] == data.aws_region.current.region, false)
+ )
+ error_message = "health_alerts_topic_arn must be a concrete SNS topic ARN in this module's AWS region, without wildcards or a subscription suffix."
+ }
+}
+
variable "alert_publish_topic_arns" {
description = "Additional SNS topic ARNs the host may publish alerts to. This grant does not configure CARLOS_ALERT_SNS_TOPIC_ARN."
type = set(string)