Skip to content

feat(infra): an ECR repo for the buildkit layer cache - #405

Merged
athvin merged 1 commit into
mainfrom
bm/ci-buildcache-ecr
Nov 2, 2026
Merged

athvin merged 1 commit into
mainfrom
bm/ci-buildcache-ecr

Conversation

@athvin

@athvin athvin commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Terraform half of the buildkit-cache move build-cost.md has prescribed since the sccache migration ("the structural fix for buildkit is the same shape as sccache's"). A dedicated er-buildcache repo because the image repos can't host it: they're IMMUTABLE (a registry cache ref is overwritten on every push) and their last-30-of-any lifecycle fits images, not the orphaned manifests every cache push produces (here: expire-untagged-after-1-day + a count backstop). scan_on_push off — the layers are scanned on push to their real repos. The existing er-ci-ecr-push role covers it via the ARN list.

Workflow half (images.yml cache-to: type=registry) follows in its own PR after the unattended apply creates the repo — the exact sequencing sccache.tf documents.

🤖 Generated with Claude Code

The Actions cache is a shared 10 GB budget and buildkit's type=gha,mode=max
layer cache (~5 GB measured) is its largest producer — the thing rust.yml's
cache-prune exists to cap. Same move sccache made: a registry backend with
no shared budget. Its own repo because the image repos are IMMUTABLE (a
cache ref is overwritten every push) with a last-30-of-any lifecycle that
fits images, not orphan-producing cache pushes; scan_on_push off because
the layers are scanned on push to their real repos.

Sequence (the sccache lesson, recorded in the file): apply this, THEN point
images.yml at it.

Co-Authored-By: Claude Fable 5 <[email protected]>
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

terraform plan

f248ed0 · run log and full plans

env add change destroy
state-backend (plan only) 0 0 0
core 2 1 0
dev 0 0 0
prod 0 0 0

Planned with -refresh=false, so this is the config-versus-state diff and
not out-of-band drift. That is a security choice, not a speed one: a refreshing
plan reads secret values, and the plan role is assumable from any PR branch —
which can edit the workflow it runs under. Drift is caught at apply time, which
refreshes fully.

core — 2 to add, 1 to change
module.identity_center.data.aws_ssoadmin_instances.this: Reading...
data.aws_iam_policy_document.ci_tf_apply_no_prod_state: Reading...
data.aws_iam_policy_document.ci_tf_drift_refresh: Reading...
data.aws_iam_policy_document.ci_tf_plan_state: Reading...
data.aws_iam_policy_document.ci_tf_plan_state: Read complete after 0s [id=2883378100]
data.aws_iam_policy_document.ci_tf_apply_no_prod_state: Read complete after 0s [id=2395007408]
data.aws_iam_policy_document.ci_tf_drift_refresh: Read complete after 0s [id=2350222434]
data.aws_iam_policy_document.sccache_read: Reading...
data.aws_iam_policy_document.sccache_read: Read complete after 0s [id=2701062528]
data.aws_iam_policy_document.sccache_write: Reading...
module.github_oidc.data.aws_iam_policy_document.trust: Reading...
module.github_oidc.data.aws_iam_policy_document.trust: Read complete after 0s [id=4060172364]
data.aws_iam_policy_document.sccache_write: Read complete after 0s [id=786296009]
data.aws_iam_policy_document.audit_bucket: Reading...
data.aws_iam_policy_document.audit_bucket: Read complete after 0s [id=3031279952]
data.aws_iam_policy_document.cur_delivery: Reading...
data.aws_iam_policy_document.cur_delivery: Read complete after 0s [id=2207400467]
data.aws_iam_policy_document.sccache_trust_write: Reading...
data.aws_iam_policy_document.sccache_trust_read: Reading...
data.aws_iam_policy_document.ci_tf_plan_trust: Reading...
data.aws_iam_policy_document.config_bucket: Reading...
data.aws_iam_policy_document.sccache_trust_write: Read complete after 0s [id=4060172364]
data.aws_iam_policy_document.ci_tf_drift_trust: Reading...
data.aws_iam_policy_document.config_bucket: Read complete after 0s [id=4067691321]
data.aws_iam_policy_document.ci_tf_apply_trust: Reading...
data.aws_iam_policy_document.ci_tf_plan_trust: Read complete after 0s [id=1630650595]
data.aws_iam_policy_document.sccache_trust_read: Read complete after 0s [id=1630650595]
data.aws_iam_policy_document.ci_tf_drift_trust: Read complete after 0s [id=2357770959]
data.aws_iam_policy_document.ci_tf_apply_trust: Read complete after 0s [id=2538345208]
data.aws_iam_policy_document.ci_tf_apply_prod_trust: Reading...
data.aws_iam_policy_document.ci_tf_apply_prod_trust: Read complete after 0s [id=3513372776]
module.identity_center.data.aws_ssoadmin_instances.this: Read complete after 0s [id=us-east-2]
module.identity_center.data.aws_identitystore_user.admin: Reading...
module.identity_center.data.aws_identitystore_user.admin: Read complete after 0s [id=b17b2500-e0b1-7046-405b-23c088cf392b]

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create
  ~ update in-place
 <= read (data resources)

Terraform will perform the following actions:

  # aws_ecr_lifecycle_policy.buildcache will be created
  + resource "aws_ecr_lifecycle_policy" "buildcache" {
      + id          = (known after apply)
      + policy      = jsonencode(
            {
              + rules = [
                  + {
                      + action       = {
                          + type = "expire"
                        }
                      + description  = "Expire orphaned cache manifests after a day"
                      + rulePriority = 1
                      + selection    = {
                          + countNumber = 1
                          + countType   = "sinceImagePushed"
                          + countUnit   = "days"
                          + tagStatus   = "untagged"
                        }
                    },
                  + {
                      + action       = {
                          + type = "expire"
                        }
                      + description  = "Backstop: never hold more than 10 cache images"
                      + rulePriority = 2
                      + selection    = {
                          + countNumber = 10
                          + countType   = "imageCountMoreThan"
                          + tagStatus   = "any"
                        }
                    },
                ]
            }
        )
      + region      = "us-east-2"
      + registry_id = (known after apply)
      + repository  = "er-buildcache"
    }

  # aws_ecr_repository.buildcache will be created
  + resource "aws_ecr_repository" "buildcache" {
      + arn                  = (known after apply)
      + id                   = (known after apply)
      + image_tag_mutability = "MUTABLE"
      + name                 = "er-buildcache"
      + region               = "us-east-2"
      + registry_id          = (known after apply)
      + repository_url       = (known after apply)
      + tags_all             = {
          + "Environment" = "core"
          + "ManagedBy"   = "terraform"
          + "Project"     = "entity-resolution"
        }

      + encryption_configuration {
          + encryption_type = "AES256"
          + kms_key         = (known after apply)
        }

      + image_scanning_configuration {
          + scan_on_push = false
        }
    }

  # module.github_oidc.data.aws_iam_policy_document.push will be read during apply
  # (config refers to values not yet known)
 <= data "aws_iam_policy_document" "push" {
      + id            = (known after apply)
      + json          = (known after apply)
      + minified_json = (known after apply)

      + statement {
          + actions   = [
              + "ecr:GetAuthorizationToken",
            ]
          + effect    = "Allow"
          + resources = [
              + "*",
            ]
          + sid       = "EcrAuth"
        }
      + statement {
          + actions   = [
              + "ecr:BatchCheckLayerAvailability",
              + "ecr:BatchGetImage",
              + "ecr:CompleteLayerUpload",
              + "ecr:DescribeImages",
              + "ecr:DescribeRepositories",
              + "ecr:GetDownloadUrlForLayer",
              + "ecr:InitiateLayerUpload",
              + "ecr:PutImage",
              + "ecr:UploadLayerPart",
            ]
          + effect    = "Allow"
          + resources = [
              + "arn:aws:ecr:us-east-2:797781631727:repository/er-api",
              + "arn:aws:ecr:us-east-2:797781631727:repository/er-pipeline",
              + "arn:aws:ecr:us-east-2:797781631727:repository/er-web",
              + (known after apply),
            ]
          + sid       = "EcrPushPull"
        }
    }

  # module.github_oidc.aws_iam_role_policy.push will be updated in-place
  ~ resource "aws_iam_role_policy" "push" {
        id          = "er-ci-ecr-push:ecr-push"
        name        = "ecr-push"
      ~ policy      = jsonencode(
            {
              - Statement = [
                  - {
                      - Action   = "ecr:GetAuthorizationToken"
                      - Effect   = "Allow"
                      - Resource = "*"
                      - Sid      = "EcrAuth"
                    },
                  - {
                      - Action   = [
                          - "ecr:UploadLayerPart",
                          - "ecr:PutImage",
                          - "ecr:InitiateLayerUpload",
                          - "ecr:GetDownloadUrlForLayer",
                          - "ecr:DescribeRepositories",
                          - "ecr:DescribeImages",
                          - "ecr:CompleteLayerUpload",
                          - "ecr:BatchGetImage",
                          - "ecr:BatchCheckLayerAvailability",
                        ]
                      - Effect   = "Allow"
                      - Resource = [
                          - "arn:aws:ecr:us-east-2:797781631727:repository/er-web",
                          - "arn:aws:ecr:us-east-2:797781631727:repository/er-pipeline",
                          - "arn:aws:ecr:us-east-2:797781631727:repository/er-api",
                        ]
                      - Sid      = "EcrPushPull"
                    },
                ]
              - Version   = "2012-10-17"
            }
        ) -> (known after apply)
        # (2 unchanged attributes hidden)
    }

Plan: 2 to add, 1 to change, 0 to destroy.

@athvin
athvin merged commit 35b8435 into main Nov 2, 2026
31 checks passed
athvin added a commit that referenced this pull request Nov 2, 2026
The gha backend's ~5 GB mode=max cache was the largest producer in the
shared 10 GB Actions budget — the thing cache-prune exists to cap. Both
image builds now cache-to er-buildcache (terraform #405), with
image-manifest=true,oci-mediatypes=true because ECR rejects buildkit's
default cache-manifest media type. The gha line survives as a transitional
cache-FROM only, so the first registry run starts warm; cache-to stops
feeding the Actions cache immediately. cache-prune's comment now records
that both of its budgets are rollback insurance.

Co-authored-by: bmoore813 <[email protected]>
Co-authored-by: Claude Fable 5 <[email protected]>
@athvin
athvin deleted the bm/ci-buildcache-ecr branch November 2, 2026 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants