carlos / platform Public

infra: data buckets expire noncurrent versions after 7 days (#336)

The data buckets are versioned and had no lifecycle rule, so every litestream
file replaced or deleted after compaction stayed as a noncurrent version
forever (one flagship data bucket: 14.5 GB current, 1,657 GB billed on
2026-10-24). deployment-buckets and region-remote-host now give the data
bucket one rule: noncurrent versions expire after data_noncurrent_days
(default 7, the restore window), expired delete markers go, abandoned
multipart uploads go after 7 days; current objects never expire. tofu tests
pin the default, the variable and its floor. The offline-move runbook's
note on recovering deleted objects now says the window is 7 days.

Applied by hand on carloku.com via carloku-infrastructure#21.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev d63a2a839f665cf0df5769295bee67ad5ec0fb69 parent 6e2d981
7 files changed, +175 −1
  • docs/superpowers/plans/2026-10-21-offline-move-runbook.md +1 −1
  • infra/modules/deployment-buckets/main.tf +29 −0
  • infra/modules/deployment-buckets/tests/data_lifecycle.tftest.hcl +47 −0
  • infra/modules/deployment-buckets/variables.tf +11 −0
  • infra/modules/region-remote-host/main.tf +29 −0
  • infra/modules/region-remote-host/tests/data_lifecycle.tftest.hcl +47 −0
  • infra/modules/region-remote-host/variables.tf +11 −0

CI failed — run details

diff --git a/docs/superpowers/plans/2026-10-21-offline-move-runbook.md b/docs/superpowers/plans/2026-10-21-offline-move-runbook.md
index c31df766c..8a8c7705d 100644
--- a/docs/superpowers/plans/2026-10-21-offline-move-runbook.md
+++ b/docs/superpowers/plans/2026-10-21-offline-move-runbook.md
@@ -419,7 +419,7 @@ L "cleanup" "bash $W/cleanup.sh /data/.offline-moves/$H/<move-id>/source/instanc
3. removes the parked local replica `.../source/instance/.activator-<host>.db-litestream/ltx` on the source box, by exact path (a few hundred KB);
4. deletes every object under `instances/<host>/` **by exact key, keeping `lock.json`** (the lifted tombstone stays as the record), and logs each key with the version id it removed and its size.
-The data buckets are **versioned with no lifecycle rule**. A delete only adds a delete marker; the bytes stay as noncurrent versions, are still billed, and can be brought back from the logged version id. Removing the noncurrent versions is a separate decision. The parked database, env and `config.env` under `source/instance/` stay too.
+The data buckets are **versioned, and noncurrent versions expire 7 days after they are replaced** (platform #336). A delete only adds a delete marker; the bytes stay as noncurrent versions for 7 days, are billed until then, and can be brought back from the logged version id within that window. The parked database, env and `config.env` under `source/instance/` stay too.
Never run this for an instance whose move was aborted: its source replica is the live one.
diff --git a/infra/modules/deployment-buckets/main.tf b/infra/modules/deployment-buckets/main.tf
index 91dd68c26..625635b54 100644
--- a/infra/modules/deployment-buckets/main.tf
+++ b/infra/modules/deployment-buckets/main.tf
@@ -92,6 +92,35 @@ resource "aws_s3_bucket_versioning" "data" {
versioning_configuration { status = "Enabled" }
}
+# Old versions in the data bucket expire (platform #336). The bucket is
+# versioned and litestream rewrites and compacts its files constantly, so
+# without this every replaced or deleted object lived on as a noncurrent
+# version forever: on 2026-10-24 one flagship data bucket held 14.5 GB of
+# current objects and was billed for 1,657 GB. A noncurrent version expires
+# data_noncurrent_days after it is replaced (7 by default, the restore
+# window), delete markers with nothing behind them go, and abandoned
+# multipart uploads go after 7 days. Current objects never expire.
+resource "aws_s3_bucket_lifecycle_configuration" "data" {
+ bucket = aws_s3_bucket.data.id
+
+ rule {
+ id = "expire-noncurrent"
+ status = "Enabled"
+ filter {}
+ noncurrent_version_expiration {
+ noncurrent_days = var.data_noncurrent_days
+ }
+ expiration {
+ expired_object_delete_marker = true
+ }
+ abort_incomplete_multipart_upload {
+ days_after_initiation = 7
+ }
+ }
+
+ depends_on = [aws_s3_bucket_versioning.data]
+}
+
resource "aws_s3_bucket_public_access_block" "deployment" {
bucket = aws_s3_bucket.deployment.id
block_public_acls = true
diff --git a/infra/modules/deployment-buckets/tests/data_lifecycle.tftest.hcl b/infra/modules/deployment-buckets/tests/data_lifecycle.tftest.hcl
new file mode 100644
index 000000000..287a9be87
--- /dev/null
+++ b/infra/modules/deployment-buckets/tests/data_lifecycle.tftest.hcl
@@ -0,0 +1,47 @@
+# Platform #336: the data bucket's noncurrent versions expire after
+# data_noncurrent_days (7 by default), expired delete markers go, and
+# current objects never expire.
+
+mock_provider "aws" {}
+
+variables {
+ deployment_bucket_name = "dep"
+ data_bucket_name = "data"
+}
+
+run "data_noncurrent_versions_expire_after_seven_days" {
+ command = plan
+
+ assert {
+ condition = length([
+ for r in aws_s3_bucket_lifecycle_configuration.data.rule : r
+ if r.id == "expire-noncurrent" && r.status == "Enabled" &&
+ one(r.noncurrent_version_expiration).noncurrent_days == 7 &&
+ one(r.expiration).expired_object_delete_marker == true
+ ]) == 1
+ error_message = "the data bucket must expire noncurrent versions after 7 days and clear expired delete markers"
+ }
+}
+
+run "data_noncurrent_days_is_configurable" {
+ command = plan
+
+ variables {
+ data_noncurrent_days = 30
+ }
+
+ assert {
+ condition = one(one(aws_s3_bucket_lifecycle_configuration.data.rule).noncurrent_version_expiration).noncurrent_days == 30
+ error_message = "data_noncurrent_days must set the noncurrent expiry"
+ }
+}
+
+run "data_noncurrent_days_refuses_zero" {
+ command = plan
+
+ variables {
+ data_noncurrent_days = 0
+ }
+
+ expect_failures = [var.data_noncurrent_days]
+}
diff --git a/infra/modules/deployment-buckets/variables.tf b/infra/modules/deployment-buckets/variables.tf
index a3a542254..e1af55cd3 100644
--- a/infra/modules/deployment-buckets/variables.tf
+++ b/infra/modules/deployment-buckets/variables.tf
@@ -27,3 +27,14 @@ variable "box_fence_writer_role_arns" {
error_message = "Fence writers must be exact IAM role ARNs without wildcards."
}
}
+
+variable "data_noncurrent_days" {
+ description = "Days a replaced or deleted object in the data bucket is kept as a noncurrent version before it expires. Defaults to the 7-day restore window. Longer retention for a plan belongs in litestream's per-instance retention, not here."
+ type = number
+ default = 7
+
+ validation {
+ condition = var.data_noncurrent_days >= 1 && floor(var.data_noncurrent_days) == var.data_noncurrent_days
+ error_message = "data_noncurrent_days must be a whole number of days, at least 1."
+ }
+}
diff --git a/infra/modules/region-remote-host/main.tf b/infra/modules/region-remote-host/main.tf
index a5e4a203d..322fbee8d 100644
--- a/infra/modules/region-remote-host/main.tf
+++ b/infra/modules/region-remote-host/main.tf
@@ -34,6 +34,35 @@ resource "aws_s3_bucket_versioning" "data" {
versioning_configuration { status = "Enabled" }
}
+# Old versions in the data bucket expire (platform #336). The bucket is
+# versioned and litestream rewrites and compacts its files constantly, so
+# without this every replaced or deleted object lived on as a noncurrent
+# version forever: on 2026-10-24 one flagship data bucket held 14.5 GB of
+# current objects and was billed for 1,657 GB. A noncurrent version expires
+# data_noncurrent_days after it is replaced (7 by default, the restore
+# window), delete markers with nothing behind them go, and abandoned
+# multipart uploads go after 7 days. Current objects never expire.
+resource "aws_s3_bucket_lifecycle_configuration" "data" {
+ bucket = aws_s3_bucket.data.id
+
+ rule {
+ id = "expire-noncurrent"
+ status = "Enabled"
+ filter {}
+ noncurrent_version_expiration {
+ noncurrent_days = var.data_noncurrent_days
+ }
+ expiration {
+ expired_object_delete_marker = true
+ }
+ abort_incomplete_multipart_upload {
+ days_after_initiation = 7
+ }
+ }
+
+ depends_on = [aws_s3_bucket_versioning.data]
+}
+
resource "aws_s3_bucket_public_access_block" "data" {
bucket = aws_s3_bucket.data.id
block_public_acls = true
diff --git a/infra/modules/region-remote-host/tests/data_lifecycle.tftest.hcl b/infra/modules/region-remote-host/tests/data_lifecycle.tftest.hcl
new file mode 100644
index 000000000..fcc0bed6f
--- /dev/null
+++ b/infra/modules/region-remote-host/tests/data_lifecycle.tftest.hcl
@@ -0,0 +1,47 @@
+# Platform #336: the data bucket's noncurrent versions expire after
+# data_noncurrent_days (7 by default), expired delete markers go, and
+# current objects never expire.
+
+mock_provider "aws" {}
+
+variables {
+ region_code = "uk"
+ data_bucket_name = "data"
+}
+
+run "data_noncurrent_versions_expire_after_seven_days" {
+ command = plan
+
+ assert {
+ condition = length([
+ for r in aws_s3_bucket_lifecycle_configuration.data.rule : r
+ if r.id == "expire-noncurrent" && r.status == "Enabled" &&
+ one(r.noncurrent_version_expiration).noncurrent_days == 7 &&
+ one(r.expiration).expired_object_delete_marker == true
+ ]) == 1
+ error_message = "the data bucket must expire noncurrent versions after 7 days and clear expired delete markers"
+ }
+}
+
+run "data_noncurrent_days_is_configurable" {
+ command = plan
+
+ variables {
+ data_noncurrent_days = 30
+ }
+
+ assert {
+ condition = one(one(aws_s3_bucket_lifecycle_configuration.data.rule).noncurrent_version_expiration).noncurrent_days == 30
+ error_message = "data_noncurrent_days must set the noncurrent expiry"
+ }
+}
+
+run "data_noncurrent_days_refuses_zero" {
+ command = plan
+
+ variables {
+ data_noncurrent_days = 0
+ }
+
+ expect_failures = [var.data_noncurrent_days]
+}
diff --git a/infra/modules/region-remote-host/variables.tf b/infra/modules/region-remote-host/variables.tf
index 9fe68ecd6..0dfa71070 100644
--- a/infra/modules/region-remote-host/variables.tf
+++ b/infra/modules/region-remote-host/variables.tf
@@ -19,3 +19,14 @@ variable "sibling_staging_arns" {
type = list(string)
default = []
}
+
+variable "data_noncurrent_days" {
+ description = "Days a replaced or deleted object in the data bucket is kept as a noncurrent version before it expires. Defaults to the 7-day restore window. Longer retention for a plan belongs in litestream's per-instance retention, not here."
+ type = number
+ default = 7
+
+ validation {
+ condition = var.data_noncurrent_days >= 1 && floor(var.data_noncurrent_days) == var.data_noncurrent_days
+ error_message = "data_noncurrent_days must be a whole number of days, at least 1."
+ }
+}