carlos / platform Public

region-host: Accept an existing alerts topic

Route alarm notifications and host publish permissions to a caller-owned SNS
topic so group teardown leaves the alerts topic in place. Preserve the email
creation path and refuse conflicting inputs or topics outside the region.

Cover both modes with module tests and document caller-owned policies and
subscriptions. The full module suite passes; census approval remains with
the G3 controller.
Co-Authored-By: GPT-6 Astra <noreply@openai.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 3a5b73e4dbd23d657aabd461c690f88b143525a3 parent e59db8b
7 files changed, +633 −19
  • infra/modules/region-host/README.md +24 −0
  • infra/modules/region-host/alert_publish.tf +5 −2
  • infra/modules/region-host/group_alarms.tf +5 −5
  • infra/modules/region-host/main.tf +9 −9
  • infra/modules/region-host/outputs.tf +2 −2
  • infra/modules/region-host/tests/alerts_topic.tftest.hcl +569 −0
  • infra/modules/region-host/variables.tf +19 −1

Intent

Human

<task-notification> <task-id>bj7umt3bv</task-id> <tool-use-id>toolu_01KZdxKyY15NepacxNchyZKn</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/f72ef775-81b3-4d2e-a18d-c8edb14aa192/tasks/bj7umt3bv.output</output-file> <status>completed</status> <summary>Background command "sleep 15; .superpowers/sdd/2026-11-07-region-ha-g3-fixes/wait.sh t9" completed (exit code 0)</summary> </task-notification>

Human

<task-notification> <task-id>a67e916379135fe2c</task-id> <tool-use-id>toolu_016xV6X6diHHCaBHx8uQqt7m</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/f72ef775-81b3-4d2e-a18d-c8edb14aa192/tasks/a67e916379135fe2c.output</output-file> <status>completed</status> <summary>Agent "Audit control/edges readers" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>I wrote the audit to `/home/paulca/amadan.net/carlos/platform/.claude/worktrees/region-ha-g3-fixes/.superpowers/sdd/2026-11-07-region-ha-g3-fixes/edges-audit.md`. It covers about 35 readers, each with file:line, what it does with a live member's file, a dead member's stale file and a fixed box's file, its rule and a verdict. It also has the draft spec section "§24 Readers of edges files" (the table, plus the rule for new readers). I changed nothing else and ran no tests. 1. **Main finding: one reader is WRONG.** The console's instance size check (`internal/console/policysize.go:84-140`) never looks at pools. It counts every fresh box in the record's region. The size is then refused if it needs more than a quarter of the smallest box's memory. A member with the dev shape's 927 MiB would refuse even `small` (256 MiB) for apps it can never run, and the refusal names the `g-` box. It also keeps counting a dead member whose stale file still lists a pool host. 2. **Fix for (1):** count a box only if its `served_pools` could serve one of the records' pools; boxes with missing pool evidence keep today's behaviour. The proving test puts a fixed 8 GiB box (shared pool) and a 927 MiB member (`hapair`) in one region. `small` must be accepted on a shared-pool app and refused, naming the member, on a `hapair` app. 3. **Minor WRONG, wording only:** the GeoIP readiness remedy (`apisystemgeoip.go:336`) tells operators to delete a dead box's edges file. Done to a member, that skips the departure scan the spec forbids skipping (I3). Fixing it is a wording change, so it needs copy review. 4. **UNSURE, possibly serious:** the console's DNS advice and domain placements (`server.go:1638-1712`, `domainbuy.go:518`, `apiplacements.go`) only leave out members when `CONSOLE_REGION_INGRESS` is set. If the flagship console doesn't set it, customers are being told to point A records at a member's auto-assigned public IPv4, and bought domains get that address written as their apex. That address is released when the member is replaced. Checking the flagship console's environment settles it. I could not check it without touching AWS. 5. **UNSURE, not on today's flagship:** the steering fix on this branch skips boxes by `serves_shared_pool:false`, not by proven membership. A member that does serve the shared pool could still win a region's latency record with its temporary public address. Whether any member publishes `serves_shared_pool:true` settles it. 6. **Correct today, latent:** the explicit-region gate (`instancesplacement.go:161-214`) ignores `serves_shared_pool:false`. It goes wrong once a region's only boxes are members, which retiring the old box (§13) will create. That fix belongs in §13's plan. 7. **Correct readers:** - The edge home map and everything built on it, including routing, view checks and certificates. Dead-member windows are bounded by dial failure, the W4 holder fix and the departure. - Cold-wake placement (pool-aware). - The capacity observation, which is the reference membership test via the signed identity record. - The hostname-history gate (fix A). - Automatic region eligibility. - Moves, seeds and offline moves (members make these stricter, and HA regions refuse them anyway). - Egress grants, the lifetimes floor, region names, asset status, db forwarding, the alias pass and the operator scripts. 8. **Name shape:** `IsGroupReference` is used only where a false positive makes a gate stricter, never to grant anything. That is safe even though fixed labels can start with `g-`. 9. **Housekeeper candidacy (W2b) never read edges files.** It comes from the box's own environment (`internal/host/housekeeper.go:190`). 10. **Already-fixed defects:** steering, departures, W4 and fix A are classified in a table in the audit. I did not re-audit the departures code, because another agent is editing it.</result> <usage><subagent_tokens>244687</subagent_tokens><tool_uses>93</tool_uses><duration_ms>731372</duration_ms></usage> </task-notification>

Human

<paseo-system version="1" kind="agent-message" source-agent-id="8a3d302a-eac7-407c-b61c-f5ccc66b5544" message-id="4d70f3de-887a-4682-b9a2-d41ee101d7be"> Agent manager 8a3d302: live check done (read-only): no Route 53 record in the flagship account points at any member IP; domain_purchases is empty and domain_registrations has no rows since 12:20Z; annie&apos;s console journal 12:25-13:50Z has no domain, placement or DNS lines (only two &apos;eligible-capacity: placement read incomplete&apos; lines). DNS-advice page views aren&apos;t logged, so a customer-managed external A record can&apos;t be ruled out (US had 2 accounts). Your T5 change (members always excluded) is right; CONSOLE_REGION_INGRESS goes on card rev 4. </paseo-system>

Attributed to this commit: 7.26 g CO₂e (969,368 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on region-ha-g3-fixes, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.

diff --git a/infra/modules/region-host/README.md b/infra/modules/region-host/README.md
index 195ec4447..ac2373a4a 100644
--- a/infra/modules/region-host/README.md
+++ b/infra/modules/region-host/README.md
@@ -140,6 +140,30 @@ is unconditioned, because the infrastructure inventory reads every
region. The HA spec asks for an `ec2:Region` condition on the call the
placer makes. Decide there whether that is a second, narrower statement.
+## Alert notifications
+
+Set `alerts_topic_arn` to use an existing SNS topic in the module's AWS region.
+In fixed and group mode, the module sends alarm notifications to that topic
+and grants the host role `sns:Publish` on it. Group launch-failure events
+also target it. The module creates no topic, subscription or topic policy
+for this input. Destroying the group leaves the external topic in place.
+
+Manage the topic policy and subscriptions outside this module. Allow
+CloudWatch to publish alarms from your account and region. In group mode,
+also allow EventBridge to publish from the rule named `${name}-launch-failed`.
+Confirm any email subscriptions before relying on delivery.
+
+Leave `alerts_email` empty when setting `alerts_topic_arn`. Setting both is
+refused. To keep the module-managed topic and email subscription, set only
+`alerts_email`. If both are empty, alarm notifications stay disabled. The
+`alerts_topic_arn` output returns the ARN in use, or null when neither input
+is set.
+
+`alert_publish_topic_arns` still grants publish access to additional topics.
+Neither input configures the process alert destination. Set
+`CARLOS_ALERT_SNS_TOPIC_ARN` in the host configuration to send in-process
+alerts to the chosen topic.
+
## Attended group maintenance
Group mode installs and starts `amazon-ssm-agent` before CARLOS bootstrap.
diff --git a/infra/modules/region-host/alert_publish.tf b/infra/modules/region-host/alert_publish.tf
index 052f7d292..8bce58495 100644
--- a/infra/modules/region-host/alert_publish.tf
+++ b/infra/modules/region-host/alert_publish.tf
@@ -1,9 +1,12 @@
locals {
- alert_publish_topic_arns = setunion(var.alert_publish_topic_arns, toset(aws_sns_topic.alerts[*].arn))
+ 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] : []
+ alert_publish_topic_arns = setunion(var.alert_publish_topic_arns, toset(local.alerts_actions))
}
resource "aws_iam_role_policy" "alert_publish" {
- count = var.alerts_email != "" || length(var.alert_publish_topic_arns) > 0 ? 1 : 0
+ count = local.alerts_enabled || length(var.alert_publish_topic_arns) > 0 ? 1 : 0
name = "${var.iam_name}-alert-publish"
role = aws_iam_role.host.id
policy = jsonencode({
diff --git a/infra/modules/region-host/group_alarms.tf b/infra/modules/region-host/group_alarms.tf
index d793b3243..f177b6863 100644
--- a/infra/modules/region-host/group_alarms.tf
+++ b/infra/modules/region-host/group_alarms.tf
@@ -18,8 +18,8 @@ resource "aws_cloudwatch_metric_alarm" "termination_queue_age" {
threshold = 600
comparison_operator = "GreaterThanThreshold"
treat_missing_data = "notBreaching"
- alarm_actions = var.alerts_email != "" ? [aws_sns_topic.alerts[0].arn] : []
- ok_actions = var.alerts_email != "" ? [aws_sns_topic.alerts[0].arn] : []
+ alarm_actions = local.alerts_actions
+ ok_actions = local.alerts_actions
dimensions = { QueueName = aws_sqs_queue.terminations.name }
}
@@ -28,7 +28,7 @@ resource "aws_cloudwatch_metric_alarm" "termination_queue_age" {
# EligibleBoxes stays unarmed until P2b supplies its real producer. NLB alarms
# belong to ingress. All actions notify; none replaces or scales a member.
locals {
- group_alarm_actions = var.alerts_email != "" ? [aws_sns_topic.alerts[0].arn] : []
+ group_alarm_actions = local.alerts_actions
}
resource "aws_cloudwatch_metric_alarm" "group_capacity" {
@@ -155,9 +155,9 @@ resource "aws_cloudwatch_event_rule" "group_launch_failed" {
}
resource "aws_cloudwatch_event_target" "group_launch_failed" {
- count = var.mode == "group" && var.alerts_email != "" ? 1 : 0
+ count = var.mode == "group" && local.alerts_enabled ? 1 : 0
rule = aws_cloudwatch_event_rule.group_launch_failed[0].name
- arn = aws_sns_topic.alerts[0].arn
+ arn = local.alerts_topic_arn
}
resource "aws_sns_topic_policy" "group_launch_failed" {
diff --git a/infra/modules/region-host/main.tf b/infra/modules/region-host/main.tf
index b785073fe..b4184ede6 100644
--- a/infra/modules/region-host/main.tf
+++ b/infra/modules/region-host/main.tf
@@ -629,13 +629,13 @@ resource "aws_cloudwatch_metric_alarm" "host_auto_recover" {
# 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
# BREACHING: a box that stops reporting is a box in trouble (or an agent
-# rolled without CARLOS_HOST_METRICS — either way, look). Which is also
-# why the caller should set alerts_email only once the box is publishing
-# (spec §4): wiring it first buys days of ALARM mail about nothing.
+# rolled without CARLOS_HOST_METRICS). Set alerts_email or alerts_topic_arn
+# only once the box is publishing (spec §4). Otherwise, missing metrics
+# trigger alarms before the box is ready.
#
-# The topic lives here, in the host's region, because an alarm can only
-# notify a same-region topic — one account-wide topic passed to four
-# regional hosts would apply and then never deliver for three of them.
+# An alarm can only notify a topic in its own region. alerts_email creates
+# that topic here. alerts_topic_arn selects an existing topic and validates
+# its region. The caller manages that topic's policy and subscriptions.
resource "aws_sns_topic" "alerts" {
count = var.alerts_email != "" ? 1 : 0
name = "${var.name}-alerts"
@@ -649,7 +649,7 @@ resource "aws_sns_topic_subscription" "alerts_email" {
}
resource "aws_cloudwatch_metric_alarm" "host_memory_low" {
- count = var.mode == "fixed" && var.alerts_email != "" ? 1 : 0
+ count = var.mode == "fixed" && local.alerts_enabled ? 1 : 0
alarm_name = "${var.name}-memory-low"
alarm_description = "MemAvailable below 15% for 3 minutes on ${var.name} — a tenant is eating the box; see the memory-pressure spec §4 for what to look at."
namespace = "Carlos/Host"
@@ -661,8 +661,8 @@ resource "aws_cloudwatch_metric_alarm" "host_memory_low" {
threshold = 15
comparison_operator = "LessThanThreshold"
treat_missing_data = "breaching"
- alarm_actions = [aws_sns_topic.alerts[0].arn]
- ok_actions = [aws_sns_topic.alerts[0].arn]
+ alarm_actions = local.alerts_actions
+ ok_actions = local.alerts_actions
dimensions = {
InstanceId = aws_instance.host[0].id
}
diff --git a/infra/modules/region-host/outputs.tf b/infra/modules/region-host/outputs.tf
index d0aee991e..d7d6e36b2 100644
--- a/infra/modules/region-host/outputs.tf
+++ b/infra/modules/region-host/outputs.tf
@@ -24,8 +24,8 @@ output "role_name" {
}
output "alerts_topic_arn" {
- description = "ARN of this module's alerts topic. Null when alerts_email is empty."
- value = one(aws_sns_topic.alerts[*].arn)
+ description = "ARN used for alert notifications. Null when both alerts_email and alerts_topic_arn are empty."
+ value = local.alerts_topic_arn
}
output "ami_id" {
diff --git a/infra/modules/region-host/tests/alerts_topic.tftest.hcl b/infra/modules/region-host/tests/alerts_topic.tftest.hcl
new file mode 100644
index 000000000..bc6084714
--- /dev/null
+++ b/infra/modules/region-host/tests/alerts_topic.tftest.hcl
@@ -0,0 +1,569 @@
+mock_provider "aws" {
+ mock_resource "aws_launch_template" { defaults = { id = "lt-0123456789abcdef0" } }
+ mock_resource "aws_security_group" { defaults = { id = "sg-0123456789abcdef1" } }
+}
+override_data {
+ target = data.aws_region.current
+ values = { region = "eu-west-1" }
+}
+override_data {
+ target = data.aws_partition.current
+ values = { partition = "aws", dns_suffix = "amazonaws.com" }
+}
+override_data {
+ target = data.aws_caller_identity.current
+ values = { account_id = "123456789012" }
+}
+override_data {
+ target = data.aws_vpc.host
+ values = { cidr_block = "10.0.0.0/16", cidr_block_associations = [] }
+}
+override_data {
+ target = data.aws_subnets.group
+ values = { ids = ["subnet-00000000000000001", "subnet-00000000000000002"] }
+}
+override_data {
+ target = data.aws_route_table.group
+ values = { routes = [{ cidr_block = "0.0.0.0/0", gateway_id = "igw-0123456789abcdef0", carrier_gateway_id = "", core_network_arn = "", destination_prefix_list_id = "", egress_only_gateway_id = "", instance_id = "", ipv6_cidr_block = "", local_gateway_id = "", nat_gateway_id = "", network_interface_id = "", odb_network_arn = "", transit_gateway_id = "", vpc_endpoint_id = "", vpc_peering_connection_id = "" }] }
+}
+override_data {
+ target = data.aws_lb.group
+ values = {
+ vpc_id = "vpc-0123456789abcdef0"
+ internal = false
+ load_balancer_type = "network"
+ ip_address_type = "ipv4"
+ enable_cross_zone_load_balancing = true
+ security_groups = ["sg-0123456789abcdef0"]
+ subnet_mapping = [
+ { subnet_id = "subnet-00000000000000001", allocation_id = "eipalloc-00000000000000001", ipv6_address = "", outpost_id = "", private_ipv4_address = "" },
+ { subnet_id = "subnet-00000000000000002", allocation_id = "eipalloc-00000000000000002", ipv6_address = "", outpost_id = "", private_ipv4_address = "" }
+ ]
+ }
+}
+override_data {
+ target = data.aws_lb_target_group.group_http
+ values = { vpc_id = "vpc-0123456789abcdef0", port = 80, protocol = "TCP", target_type = "instance", load_balancer_arns = ["arn:aws:elasticloadbalancing:eu-west-1:123456789012:loadbalancer/net/test/1234"], preserve_client_ip = "true" }
+}
+override_data {
+ target = data.aws_lb_target_group.group_https
+ values = { vpc_id = "vpc-0123456789abcdef0", port = 443, protocol = "TCP", target_type = "instance", load_balancer_arns = ["arn:aws:elasticloadbalancing:eu-west-1:123456789012:loadbalancer/net/test/1234"], preserve_client_ip = "true" }
+}
+override_data {
+ target = data.aws_eip.group["eu-west-1a"]
+ values = { public_ip = "198.51.100.10" }
+}
+override_data {
+ target = data.aws_eip.group["eu-west-1b"]
+ values = { public_ip = "198.51.100.11" }
+}
+override_resource {
+ target = aws_sqs_queue.terminations
+ values = { arn = "arn:aws:sqs:eu-west-1:123456789012:carlos-dev-ie-group-terminations", id = "https://sqs.eu-west-1.amazonaws.com/123456789012/carlos-dev-ie-group-terminations" }
+}
+override_resource {
+ target = aws_cloudwatch_event_rule.terminations
+ values = { arn = "arn:aws:events:eu-west-1:123456789012:rule/carlos-dev-ie-group-terminations" }
+}
+override_resource {
+ target = aws_iam_role.host
+ values = { id = "carlos-dev-ie-group", arn = "arn:aws:iam::123456789012:role/carlos-dev-ie-group" }
+}
+variables {
+ mode = "group"
+ name = "carlos-dev-ie-group"
+ iam_name = "carlos-dev-ie-group"
+ sg_description = "test group"
+ instance_type = "t4g.micro"
+ root_volume_size = 20
+ user_data = ""
+ deployment_bucket_arn = "arn:aws:s3:::carlos-dev-platform"
+ data_bucket_arn = "arn:aws:s3:::carlos-dev-ie"
+ ssm_parameter_arn = ""
+ vpc_id = "vpc-0123456789abcdef0"
+ box_count = 2
+ group_config = {
+ deployment = "dev"
+ region = "ie"
+ placement_region = "ie"
+ pools = ["default"]
+ ami_id = "ami-0123456789abcdef0"
+ release = "v0.100.0"
+ configuration_kms_key_arn = "arn:aws:kms:eu-west-1:123456789012:key/00000000-0000-0000-0000-000000000001"
+ }
+ group_zones = {
+ eu-west-1a = { subnet_id = "subnet-00000000000000001", allocation_id = "eipalloc-00000000000000001" }
+ eu-west-1b = { subnet_id = "subnet-00000000000000002", allocation_id = "eipalloc-00000000000000002" }
+ }
+ group_ingress = {
+ nlb_arn = "arn:aws:elasticloadbalancing:eu-west-1:123456789012:loadbalancer/net/test/1234"
+ nlb_security_group_id = "sg-0123456789abcdef0"
+ target_group_arns = {
+ "80" = "arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/test-80/1234"
+ "443" = "arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/test-443/1234"
+ }
+ public_ips = { eu-west-1a = "198.51.100.10", eu-west-1b = "198.51.100.11" }
+ }
+}
+
+override_data {
+ target = data.aws_ami.group
+ values = { id = "ami-0123456789abcdef0", root_device_name = "/dev/xvda", root_device_type = "ebs" }
+}
+override_data {
+ target = data.aws_ami.debian_arm64
+ values = { id = "ami-0123456789abcdef0" }
+}
+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_sns_topic.alerts
+ values = { arn = "arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts" }
+}
+
+override_resource {
+ target = aws_cloudwatch_event_rule.group_launch_failed
+ values = { arn = "arn:aws:events:eu-west-1:123456789012:rule/carlos-dev-ie-group-launch-failed" }
+}
+
+run "fixed_existing_topic" {
+ command = plan
+ variables {
+ mode = "fixed"
+ user_data = "#!/bin/sh\n"
+ subnet_id = "subnet-00000000000000001"
+ group_config = null
+ group_zones = {}
+ group_ingress = null
+ termination_queue_alarm = true
+ alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"
+ }
+ assert {
+ condition = length(aws_sns_topic.alerts) == 0 && length(aws_sns_topic_subscription.alerts_email) == 0 && length(aws_sns_topic_policy.group_launch_failed) == 0
+ error_message = "topic ownership must remain with the selected source"
+ }
+ assert {
+ condition = output.alerts_topic_arn == "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"
+ error_message = "output must return the selected topic or null"
+ }
+ assert {
+ condition = aws_cloudwatch_metric_alarm.termination_queue_age[0].alarm_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"]) && aws_cloudwatch_metric_alarm.termination_queue_age[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"])
+ error_message = "termination queue notifications must use the selected topic"
+ }
+ assert {
+ condition = length(aws_cloudwatch_metric_alarm.host_memory_low) == 1 && length(aws_cloudwatch_event_target.group_launch_failed) == 0
+ error_message = "notification resources must follow the mode and topic selection"
+ }
+ assert {
+ condition = aws_cloudwatch_metric_alarm.host_memory_low[0].alarm_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"]) && aws_cloudwatch_metric_alarm.host_memory_low[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"])
+ error_message = "fixed memory notifications must use the selected topic"
+ }
+ assert {
+ condition = length(aws_iam_role_policy.alert_publish) == 1
+ error_message = "publish grant must follow the configured topic"
+ }
+ assert {
+ condition = aws_iam_role_policy.alert_publish[0].role == aws_iam_role.host.id && aws_iam_role_policy.alert_publish[0].policy == jsonencode({
+ Version = "2012-10-17"
+ Statement = [{ Effect = "Allow", Action = ["sns:Publish"], Resource = sort(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"]) }]
+ })
+ error_message = "host role must receive only publish access to the exact selected topics"
+ }
+}
+
+run "fixed_email_topic" {
+ command = plan
+ variables {
+ mode = "fixed"
+ user_data = "#!/bin/sh\n"
+ subnet_id = "subnet-00000000000000001"
+ group_config = null
+ group_zones = {}
+ group_ingress = null
+ termination_queue_alarm = true
+ alerts_email = "ops@example.com"
+ }
+ assert {
+ condition = length(aws_sns_topic.alerts) == 1 && length(aws_sns_topic_subscription.alerts_email) == 1 && length(aws_sns_topic_policy.group_launch_failed) == 0
+ error_message = "topic ownership must remain with the selected source"
+ }
+ assert {
+ condition = output.alerts_topic_arn == "arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts"
+ error_message = "output must return the selected topic or null"
+ }
+ assert {
+ condition = aws_cloudwatch_metric_alarm.termination_queue_age[0].alarm_actions == toset(["arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts"]) && aws_cloudwatch_metric_alarm.termination_queue_age[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts"])
+ error_message = "termination queue notifications must use the selected topic"
+ }
+ assert {
+ condition = length(aws_cloudwatch_metric_alarm.host_memory_low) == 1 && length(aws_cloudwatch_event_target.group_launch_failed) == 0
+ error_message = "notification resources must follow the mode and topic selection"
+ }
+ assert {
+ condition = aws_cloudwatch_metric_alarm.host_memory_low[0].alarm_actions == toset(["arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts"]) && aws_cloudwatch_metric_alarm.host_memory_low[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts"])
+ error_message = "fixed memory notifications must use the selected topic"
+ }
+ assert {
+ condition = aws_sns_topic_subscription.alerts_email[0].topic_arn == "arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts" && aws_sns_topic_subscription.alerts_email[0].protocol == "email" && aws_sns_topic_subscription.alerts_email[0].endpoint == "ops@example.com"
+ error_message = "email path must retain its topic and subscription"
+ }
+ assert {
+ condition = length(aws_iam_role_policy.alert_publish) == 1
+ error_message = "publish grant must follow the configured topic"
+ }
+ assert {
+ condition = aws_iam_role_policy.alert_publish[0].role == aws_iam_role.host.id && aws_iam_role_policy.alert_publish[0].policy == jsonencode({
+ Version = "2012-10-17"
+ Statement = [{ Effect = "Allow", Action = ["sns:Publish"], Resource = sort(["arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts"]) }]
+ })
+ error_message = "host role must receive only publish access to the exact selected topics"
+ }
+}
+
+run "fixed_disabled_topic" {
+ command = plan
+ variables {
+ mode = "fixed"
+ user_data = "#!/bin/sh\n"
+ subnet_id = "subnet-00000000000000001"
+ group_config = null
+ group_zones = {}
+ group_ingress = null
+ termination_queue_alarm = true
+ }
+ assert {
+ condition = length(aws_sns_topic.alerts) == 0 && length(aws_sns_topic_subscription.alerts_email) == 0 && length(aws_sns_topic_policy.group_launch_failed) == 0
+ error_message = "topic ownership must remain with the selected source"
+ }
+ assert {
+ condition = output.alerts_topic_arn == null
+ error_message = "output must return the selected topic or null"
+ }
+ assert {
+ condition = length(aws_cloudwatch_metric_alarm.termination_queue_age[0].alarm_actions) == 0 && length(aws_cloudwatch_metric_alarm.termination_queue_age[0].ok_actions) == 0
+ error_message = "termination queue notifications must use the selected topic"
+ }
+ assert {
+ condition = length(aws_cloudwatch_metric_alarm.host_memory_low) == 0 && length(aws_cloudwatch_event_target.group_launch_failed) == 0
+ error_message = "notification resources must follow the mode and topic selection"
+ }
+ assert {
+ condition = length(aws_iam_role_policy.alert_publish) == 0
+ error_message = "publish grant must follow the configured topic"
+ }
+}
+
+run "fixed_existing_union_topic" {
+ command = plan
+ variables {
+ mode = "fixed"
+ user_data = "#!/bin/sh\n"
+ subnet_id = "subnet-00000000000000001"
+ group_config = null
+ group_zones = {}
+ group_ingress = null
+ termination_queue_alarm = true
+ alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"
+ alert_publish_topic_arns = ["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts", "arn:aws:sns:eu-west-1:123456789012:additional-alerts"]
+ }
+ assert {
+ condition = length(aws_sns_topic.alerts) == 0 && length(aws_sns_topic_subscription.alerts_email) == 0 && length(aws_sns_topic_policy.group_launch_failed) == 0
+ error_message = "topic ownership must remain with the selected source"
+ }
+ assert {
+ condition = output.alerts_topic_arn == "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"
+ error_message = "output must return the selected topic or null"
+ }
+ assert {
+ condition = aws_cloudwatch_metric_alarm.termination_queue_age[0].alarm_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"]) && aws_cloudwatch_metric_alarm.termination_queue_age[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"])
+ error_message = "termination queue notifications must use the selected topic"
+ }
+ assert {
+ condition = length(aws_cloudwatch_metric_alarm.host_memory_low) == 1 && length(aws_cloudwatch_event_target.group_launch_failed) == 0
+ error_message = "notification resources must follow the mode and topic selection"
+ }
+ assert {
+ condition = aws_cloudwatch_metric_alarm.host_memory_low[0].alarm_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"]) && aws_cloudwatch_metric_alarm.host_memory_low[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"])
+ error_message = "fixed memory notifications must use the selected topic"
+ }
+ assert {
+ condition = length(aws_iam_role_policy.alert_publish) == 1
+ error_message = "publish grant must follow the configured topic"
+ }
+ assert {
+ condition = aws_iam_role_policy.alert_publish[0].role == aws_iam_role.host.id && aws_iam_role_policy.alert_publish[0].policy == jsonencode({
+ Version = "2012-10-17"
+ Statement = [{ Effect = "Allow", Action = ["sns:Publish"], Resource = sort(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts", "arn:aws:sns:eu-west-1:123456789012:additional-alerts"]) }]
+ })
+ error_message = "host role must receive only publish access to the exact selected topics"
+ }
+}
+
+run "group_existing_topic" {
+ command = plan
+ variables {
+ termination_queue_alarm = true
+ alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"
+ }
+ assert {
+ condition = length(aws_sns_topic.alerts) == 0 && length(aws_sns_topic_subscription.alerts_email) == 0 && length(aws_sns_topic_policy.group_launch_failed) == 0
+ error_message = "topic ownership must remain with the selected source"
+ }
+ assert {
+ condition = output.alerts_topic_arn == "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"
+ error_message = "output must return the selected topic or null"
+ }
+ assert {
+ condition = aws_cloudwatch_metric_alarm.termination_queue_age[0].alarm_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"]) && aws_cloudwatch_metric_alarm.termination_queue_age[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"])
+ error_message = "termination queue notifications must use the selected topic"
+ }
+ assert {
+ condition = length(aws_cloudwatch_metric_alarm.host_memory_low) == 0 && length(aws_cloudwatch_event_target.group_launch_failed) == 1
+ error_message = "notification resources must follow the mode and topic selection"
+ }
+ assert {
+ condition = alltrue([for alarm in [aws_cloudwatch_metric_alarm.group_capacity[0], aws_cloudwatch_metric_alarm.group_memory[0], aws_cloudwatch_metric_alarm.group_clock[0], aws_cloudwatch_metric_alarm.group_clock_missing[0], aws_cloudwatch_metric_alarm.group_suspect_holder[0]] : alarm.alarm_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"]) && alarm.ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"])])
+ error_message = "all group alarm and recovery notifications must use the selected topic"
+ }
+ assert {
+ condition = aws_cloudwatch_event_target.group_launch_failed[0].arn == "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts" && aws_cloudwatch_event_target.group_launch_failed[0].rule == aws_cloudwatch_event_rule.group_launch_failed[0].name
+ error_message = "launch failure notifications must use the selected topic and rule"
+ }
+ assert {
+ condition = length(aws_iam_role_policy.alert_publish) == 1
+ error_message = "publish grant must follow the configured topic"
+ }
+ assert {
+ condition = aws_iam_role_policy.alert_publish[0].role == aws_iam_role.host.id && aws_iam_role_policy.alert_publish[0].policy == jsonencode({
+ Version = "2012-10-17"
+ Statement = [{ Effect = "Allow", Action = ["sns:Publish"], Resource = sort(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"]) }]
+ })
+ error_message = "host role must receive only publish access to the exact selected topics"
+ }
+}
+
+run "group_email_topic" {
+ command = plan
+ variables {
+ termination_queue_alarm = true
+ alerts_email = "ops@example.com"
+ }
+ assert {
+ condition = length(aws_sns_topic.alerts) == 1 && length(aws_sns_topic_subscription.alerts_email) == 1 && length(aws_sns_topic_policy.group_launch_failed) == 1
+ error_message = "topic ownership must remain with the selected source"
+ }
+ assert {
+ condition = output.alerts_topic_arn == "arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts"
+ error_message = "output must return the selected topic or null"
+ }
+ assert {
+ condition = aws_cloudwatch_metric_alarm.termination_queue_age[0].alarm_actions == toset(["arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts"]) && aws_cloudwatch_metric_alarm.termination_queue_age[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts"])
+ error_message = "termination queue notifications must use the selected topic"
+ }
+ assert {
+ condition = length(aws_cloudwatch_metric_alarm.host_memory_low) == 0 && length(aws_cloudwatch_event_target.group_launch_failed) == 1
+ error_message = "notification resources must follow the mode and topic selection"
+ }
+ assert {
+ condition = alltrue([for alarm in [aws_cloudwatch_metric_alarm.group_capacity[0], aws_cloudwatch_metric_alarm.group_memory[0], aws_cloudwatch_metric_alarm.group_clock[0], aws_cloudwatch_metric_alarm.group_clock_missing[0], aws_cloudwatch_metric_alarm.group_suspect_holder[0]] : alarm.alarm_actions == toset(["arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts"]) && alarm.ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts"])])
+ error_message = "all group alarm and recovery notifications must use the selected topic"
+ }
+ assert {
+ condition = aws_cloudwatch_event_target.group_launch_failed[0].arn == "arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts" && aws_cloudwatch_event_target.group_launch_failed[0].rule == aws_cloudwatch_event_rule.group_launch_failed[0].name
+ error_message = "launch failure notifications must use the selected topic and rule"
+ }
+ assert {
+ condition = aws_sns_topic_subscription.alerts_email[0].topic_arn == "arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts" && aws_sns_topic_subscription.alerts_email[0].protocol == "email" && aws_sns_topic_subscription.alerts_email[0].endpoint == "ops@example.com"
+ error_message = "email path must retain its topic and subscription"
+ }
+ assert {
+ condition = length(aws_iam_role_policy.alert_publish) == 1
+ error_message = "publish grant must follow the configured topic"
+ }
+ assert {
+ condition = aws_iam_role_policy.alert_publish[0].role == aws_iam_role.host.id && aws_iam_role_policy.alert_publish[0].policy == jsonencode({
+ Version = "2012-10-17"
+ Statement = [{ Effect = "Allow", Action = ["sns:Publish"], Resource = sort(["arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts"]) }]
+ })
+ error_message = "host role must receive only publish access to the exact selected topics"
+ }
+}
+
+run "group_disabled_topic" {
+ command = plan
+ variables {
+ termination_queue_alarm = true
+ }
+ assert {
+ condition = length(aws_sns_topic.alerts) == 0 && length(aws_sns_topic_subscription.alerts_email) == 0 && length(aws_sns_topic_policy.group_launch_failed) == 0
+ error_message = "topic ownership must remain with the selected source"
+ }
+ assert {
+ condition = output.alerts_topic_arn == null
+ error_message = "output must return the selected topic or null"
+ }
+ assert {
+ condition = length(aws_cloudwatch_metric_alarm.termination_queue_age[0].alarm_actions) == 0 && length(aws_cloudwatch_metric_alarm.termination_queue_age[0].ok_actions) == 0
+ error_message = "termination queue notifications must use the selected topic"
+ }
+ assert {
+ condition = length(aws_cloudwatch_metric_alarm.host_memory_low) == 0 && length(aws_cloudwatch_event_target.group_launch_failed) == 0
+ error_message = "notification resources must follow the mode and topic selection"
+ }
+ assert {
+ condition = alltrue([for alarm in [aws_cloudwatch_metric_alarm.group_capacity[0], aws_cloudwatch_metric_alarm.group_memory[0], aws_cloudwatch_metric_alarm.group_clock[0], aws_cloudwatch_metric_alarm.group_clock_missing[0], aws_cloudwatch_metric_alarm.group_suspect_holder[0]] : length(alarm.alarm_actions) == 0 && length(alarm.ok_actions) == 0])
+ error_message = "all group alarm and recovery notifications must use the selected topic"
+ }
+ assert {
+ condition = length(aws_iam_role_policy.alert_publish) == 0
+ error_message = "publish grant must follow the configured topic"
+ }
+}
+
+run "group_existing_union_topic" {
+ command = plan
+ variables {
+ termination_queue_alarm = true
+ alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"
+ alert_publish_topic_arns = ["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts", "arn:aws:sns:eu-west-1:123456789012:additional-alerts"]
+ }
+ assert {
+ condition = length(aws_sns_topic.alerts) == 0 && length(aws_sns_topic_subscription.alerts_email) == 0 && length(aws_sns_topic_policy.group_launch_failed) == 0
+ error_message = "topic ownership must remain with the selected source"
+ }
+ assert {
+ condition = output.alerts_topic_arn == "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"
+ error_message = "output must return the selected topic or null"
+ }
+ assert {
+ condition = aws_cloudwatch_metric_alarm.termination_queue_age[0].alarm_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"]) && aws_cloudwatch_metric_alarm.termination_queue_age[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"])
+ error_message = "termination queue notifications must use the selected topic"
+ }
+ assert {
+ condition = length(aws_cloudwatch_metric_alarm.host_memory_low) == 0 && length(aws_cloudwatch_event_target.group_launch_failed) == 1
+ error_message = "notification resources must follow the mode and topic selection"
+ }
+ assert {
+ condition = alltrue([for alarm in [aws_cloudwatch_metric_alarm.group_capacity[0], aws_cloudwatch_metric_alarm.group_memory[0], aws_cloudwatch_metric_alarm.group_clock[0], aws_cloudwatch_metric_alarm.group_clock_missing[0], aws_cloudwatch_metric_alarm.group_suspect_holder[0]] : alarm.alarm_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"]) && alarm.ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"])])
+ error_message = "all group alarm and recovery notifications must use the selected topic"
+ }
+ assert {
+ condition = aws_cloudwatch_event_target.group_launch_failed[0].arn == "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts" && aws_cloudwatch_event_target.group_launch_failed[0].rule == aws_cloudwatch_event_rule.group_launch_failed[0].name
+ error_message = "launch failure notifications must use the selected topic and rule"
+ }
+ assert {
+ condition = length(aws_iam_role_policy.alert_publish) == 1
+ error_message = "publish grant must follow the configured topic"
+ }
+ assert {
+ condition = aws_iam_role_policy.alert_publish[0].role == aws_iam_role.host.id && aws_iam_role_policy.alert_publish[0].policy == jsonencode({
+ Version = "2012-10-17"
+ Statement = [{ Effect = "Allow", Action = ["sns:Publish"], Resource = sort(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts", "arn:aws:sns:eu-west-1:123456789012:additional-alerts"]) }]
+ })
+ error_message = "host role must receive only publish access to the exact selected topics"
+ }
+}
+
+run "fixed_conflict_topic" {
+ command = plan
+ variables {
+ mode = "fixed"
+ user_data = "#!/bin/sh\n"
+ subnet_id = "subnet-00000000000000001"
+ group_config = null
+ group_zones = {}
+ group_ingress = null
+ termination_queue_alarm = true
+ alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"
+ alerts_email = "ops@example.com"
+ }
+ expect_failures = [var.alerts_topic_arn]
+}
+
+run "fixed_wildcard_topic" {
+ command = plan
+ variables {
+ mode = "fixed"
+ user_data = "#!/bin/sh\n"
+ subnet_id = "subnet-00000000000000001"
+ group_config = null
+ group_zones = {}
+ group_ingress = null
+ termination_queue_alarm = true
+ alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts*"
+ }
+ expect_failures = [var.alerts_topic_arn]
+}
+
+run "fixed_subscription_topic" {
+ command = plan
+ variables {
+ mode = "fixed"
+ user_data = "#!/bin/sh\n"
+ subnet_id = "subnet-00000000000000001"
+ group_config = null
+ group_zones = {}
+ group_ingress = null
+ termination_queue_alarm = true
+ alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts:subscription"
+ }
+ expect_failures = [var.alerts_topic_arn]
+}
+
+run "fixed_wrong_region_topic" {
+ command = plan
+ variables {
+ mode = "fixed"
+ user_data = "#!/bin/sh\n"
+ subnet_id = "subnet-00000000000000001"
+ group_config = null
+ group_zones = {}
+ group_ingress = null
+ termination_queue_alarm = true
+ alerts_topic_arn = "arn:aws:sns:us-east-1:123456789012:permanent-ha-alerts"
+ }
+ expect_failures = [var.alerts_topic_arn]
+}
+
+run "group_conflict_topic" {
+ command = plan
+ variables {
+ termination_queue_alarm = true
+ alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"
+ alerts_email = "ops@example.com"
+ }
+ expect_failures = [var.alerts_topic_arn]
+}
+
+run "group_wildcard_topic" {
+ command = plan
+ variables {
+ termination_queue_alarm = true
+ alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts*"
+ }
+ expect_failures = [var.alerts_topic_arn]
+}
+
+run "group_subscription_topic" {
+ command = plan
+ variables {
+ termination_queue_alarm = true
+ alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts:subscription"
+ }
+ expect_failures = [var.alerts_topic_arn]
+}
+
+run "group_wrong_region_topic" {
+ command = plan
+ variables {
+ termination_queue_alarm = true
+ alerts_topic_arn = "arn:aws:sns:us-east-1:123456789012:permanent-ha-alerts"
+ }
+ expect_failures = [var.alerts_topic_arn]
+}
diff --git a/infra/modules/region-host/variables.tf b/infra/modules/region-host/variables.tf
index 4ec6a50aa..4bbbd82db 100644
--- a/infra/modules/region-host/variables.tf
+++ b/infra/modules/region-host/variables.tf
@@ -207,11 +207,29 @@ variable "enable_cost_explorer" {
}
variable "alerts_email" {
- description = "Email that receives this host's memory alarm (memory-pressure spec §2.4). A CloudWatch alarm can only notify an SNS topic in its own region, so the module creates a regional topic + subscription per host rather than taking a topic ARN. Empty: no topic, no alarm; the PutMetricData grant is unconditional so the agent's memwatch duty can publish either way."
+ description = "Email address for a regional alerts topic created by this module. Leave empty when using alerts_topic_arn. If both inputs are empty, alarm notifications are disabled."
type = string
default = ""
}
+variable "alerts_topic_arn" {
+ description = "Existing SNS topic ARN for alarm notifications and host alert publishing. The topic must be in this module's AWS region. Manage its policy and subscriptions outside this module. Leave alerts_email empty. This input does not configure CARLOS_ALERT_SNS_TOPIC_ARN."
+ type = string
+ default = ""
+ nullable = false
+ validation {
+ condition = var.alerts_topic_arn == "" || var.alerts_email == ""
+ error_message = "Set only one of alerts_topic_arn and alerts_email."
+ }
+ validation {
+ condition = var.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.alerts_topic_arn)) &&
+ try(split(":", var.alerts_topic_arn)[3] == data.aws_region.current.region, false)
+ )
+ error_message = "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)