Runbook: teardown#

tofu destroy will not work in one pass, and that is intentional. Three resources carry prevent_destroy, each guarding a failure mode that is painful or irreversible.

Read the DNSSEC emergency runbook first if the reason for teardown is that something is broken — restoring service and tearing down are different tasks.

Why the guards exist#

ResourceGuardWhat it prevents
aws_route53_zone.thisprevent_destroyRecreation issues new nameservers; the domain goes dark until the registrar is updated.
aws_kms_key.dnssecprevent_destroyDeleting the signing key while DS is published blackholes the domain, and deletion is irreversible after the window.
aws_s3_bucket.state (seed)prevent_destroyLosing state makes the remaining infrastructure unmanageable.

Order of operations#

DNSSEC must be unwound before anything else. Getting this wrong causes an outage during a teardown, which is a genuinely annoying way to cause one.

1. Remove the DS record at the registrar#

Delete it, then wait for it to leave caches:

watch -n 30 'dig +short DS testz.dev @1.1.1.1; dig +short DS testz.dev @8.8.8.8'

Do not proceed until both return nothing. See the emergency runbook for why this ordering is non-negotiable.

2. Disable zone signing#

cd terraform/environments/prod
tofu apply -var 'enable_dnssec=false'

Confirm:

aws route53 get-dnssec --hosted-zone-id "$(tofu output -raw zone_id)"

This removes the KSK and leaves the KMS key orphaned but intact — prevent_destroy holds it.

3. Empty the S3 buckets#

Versioned buckets cannot be deleted while they contain object versions.

SITE_BUCKET="$(tofu output -raw site_bucket_name)"
aws s3 rm "s3://${SITE_BUCKET}" --recursive

# Delete every version and delete-marker.
aws s3api list-object-versions --bucket "$SITE_BUCKET" \
  --query '{Objects: Versions[].{Key:Key,VersionId:VersionId}}' --output json \
  > /tmp/versions.json
aws s3api delete-objects --bucket "$SITE_BUCKET" --delete file:///tmp/versions.json

aws s3api list-object-versions --bucket "$SITE_BUCKET" \
  --query '{Objects: DeleteMarkers[].{Key:Key,VersionId:VersionId}}' --output json \
  > /tmp/markers.json
aws s3api delete-objects --bucket "$SITE_BUCKET" --delete file:///tmp/markers.json

4. Destroy the environment layer#

tofu destroy

This will fail on the hosted zone and the KMS key. Expected. Everything else — CloudFront, ACM, Lambda, alarms, budget, records — is removed.

CloudFront distribution deletion takes 15–20 minutes: it must be disabled and fully propagate before AWS will delete it. Terraform handles the sequence but is slow.

5. Remove the guards deliberately#

In a separate commit, so the intent is unambiguous in history:

# terraform/modules/dns-zone/main.tf
resource "aws_route53_zone" "this" {
  # ...
  # lifecycle {
  #   prevent_destroy = true
  # }
}

Same for aws_kms_key.dnssec. Then:

tofu destroy

The KMS key enters a 30-day deletion window rather than disappearing. To cancel within that window:

aws kms cancel-key-deletion --key-id <key-id>

6. Registrar cleanup#

Point the nameservers away from the deleted zone, or the domain will have a dangling delegation to nameservers no longer serving it — a subdomain-takeover style risk if the zone name is ever recreated in someone else's account.

7. CI roles (phase 1), then the seed (phase 0), last#

Only when you are certain nothing else needs the state. With the phase 0 AWS profile:

tofu -chdir=terraform/bootstrap init -backend-config="bucket=testz-tfstate-<id>"
tofu -chdir=terraform/bootstrap destroy -var "expected_account_id=<id>"

The seed holds the state bucket, so copy anything worth keeping first. Move the seed's state back to local (so destroying the bucket does not destroy the state mid-run), empty the bucket including all versions (as in step 3), remove prevent_destroy from aws_s3_bucket.state, then:

printf 'terraform {\n  backend "local" {}\n}\n' > terraform/seed/backend_override.tf
tofu -chdir=terraform/seed init -migrate-state -force-copy
tofu -chdir=terraform/seed destroy -var "account_id=<id>"

Partial teardown#

Cheaper than a full destroy if the goal is just to stop paying:

To removeSetSaves
The $1/month KMS keyenable_dnssec = false (after removing DS!)$1.00/month
The Lambda endpointenable_edge_api = false$0.00 (already free)
Access logsenable_access_logs = falsepennies

enable_dnssec = false undoes a decision the production test suite asserts, so tofu test fails until the assertion changes with it. Change the assertion in the same pull request and record why (ADR-0023).

Setting enable_dnssec = false alone takes the bill from ~$1.55 to ~$0.55/month while keeping the domain, the zone and the site working. That is usually the right answer if the motivation is cost rather than removal.

Verify it is gone#

aws cloudfront list-distributions --query 'DistributionList.Items[].DomainName'
aws route53 list-hosted-zones --query 'HostedZones[].Name'
aws kms list-aliases --query 'Aliases[?starts_with(AliasName, `alias/testz`)]'
aws s3 ls | grep testz
aws budgets describe-budgets --account-id "$(aws sts get-caller-identity --query Account --output text)"

Then check the next month's bill actually reflects it.

Built 2026-11-25 from commit 33bfabf6a27d4baeecf228c31bd5d636a3965070. This page was generated from the same source that provisions the infrastructure it describes.