Skip to content

Approval-gated CI deploy of code-only Lambda changes (#1445) - #1450

Merged
scttfrdmn merged 6 commits into
mainfrom
feat/1445-deploy-lambdas
Oct 30, 2026
Merged

scttfrdmn merged 6 commits into
mainfrom
feat/1445-deploy-lambdas

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #1445

This is the workflow half of #1445. The role half shipped in #1449. The role, its policy and the production environment are unchanged.

What it adds

  • scripts/deploylambdas is a Go deployer in the scripts module. It calls AWS only through the aws CLI, behind a seam (lambdaCLI), so it adds no AWS SDK dependency. Its input is scripts/deploychecklist --json for <prev tag>..<tag>.
    • What it deploys. selectTargets is the only place that decides: status: changed and deploy_kind: code-only. Everything else goes in the record as "manual, not deployed": unchanged, manual, added and removed Lambdas, and every changed deploy input.
    • Pre-flight, build and save run for every function before any is changed:
      • (a) It snapshots get-function-configuration, then refuses unless all of these hold:
        • the ARN is in 966362334030 and us-east-1;
        • the function is Active and its last update is Successful;
        • PackageType is Zip;
        • Architectures is the checklist build's GOARCH;
        • Runtime and Handler are what the component's own mechanisms declare at the tag (SAM Runtime:/Handler:, shell --runtime/--handler). All mechanisms must agree, the runtime must be provided.*, and the handler must be bootstrap.
      • (b) It builds exactly the checklist's go build line (GOOS, GOARCH, -tags, -ldflags with ${SPAWN_VERSION} filled in) from the clean tag checkout. Any other token refuses. The zip has bootstrap at its root, mode 0755, with a fixed timestamp, so the CodeSha256 is reproducible.
      • (c) It downloads get-function's Code.Location and checks that its sha matches the live CodeSha256.
    • Deploy, one function at a time:
      • It re-reads the config, which must equal the snapshot.
      • update-function-code --revision-id <snapshot>, then it polls until the update settles. Polls are counted, not timed.
      • It checks that the remote CodeSha256 is the local one.
      • The config diff against the snapshot must be empty. The excluded fields are code, update-status and RuntimeVersionConfig, because Lambda moves the runtime version on an update under automatic runtime management. The record notes when that happens.
      • It stamps spawn:version=<tag without v> with tag-resource and reads it back.
    • Rollback. Any failure at or after the update restores the saved zip, re-verifies its sha, restores the previous spawn:version, and fails the run. The functions after it are "not attempted".
    • Dry run (the default) stops after (c) and records what it would replace and stamp.
  • .github/workflows/deploy-lambdas.yaml has workflow_call and workflow_dispatch triggers, each with dry_run (boolean, default true). It has two jobs:
    • a plan job with no credentials. It refuses a non-vX.Y.Z ref before anyone is asked to approve, and puts the checklist in its summary.
    • the deploy job. It sets environment: production, permissions: id-token: write, contents: read, pull-requests: write, and concurrency: deploy-lambdas-production with cancel-in-progress: false. configure-aws-credentials is pinned to the same SHA as in release.yaml, with the role. The record goes to the job summary and as a comment on the release PR, found the way the deploy-checklist job finds it. It is posted on failure too.
  • release.yaml gets a new deploy-lambdas job: needs: goreleaser, uses: ./.github/workflows/deploy-lambdas.yaml. No existing job changed and none needs it, so a pending approval blocks nothing.
  • Docs and CHANGELOG. docs/infra-account.md gets a new section, "Deploying code-only changes from CI", covering approve, dry run, the checks, rollback, SAM drift and what could not be ported. CHANGELOG has an entry under Added.

Design decisions

  • SAM-managed functions (pipeline-orchestrator, ttl-reaper) are deployed, and their drift is documented. This follows Approval-gated CI deploy of code-only Lambda changes after a release (OIDC role, production environment) #1445's owner-approved line ("code-only updates directly on the function… SAM drift is then the expected state for Code and must be documented"). Classifying them as manual was the alternative.
    • Why deploying is safe: a template, Makefile or params-helper change is a mechanism change, so the checklist already makes it manual. A code-only line means the template is byte-identical. CloudFormation re-applies Code only when the template's code location changes, and a parameter-only stack update leaves code and tags alone. The next make -C lambda/<x> deploy from a release tag uploads its own build and asserts Version (both Makefiles always pass it), which reconciles the drift.
    • The one real trap: a later stack update that changes code, then fails and rolls back, restores the stack's older code. It is documented, and every SAM line in the record names the stack (from the aws:cloudformation:stack-name tag) and the drift.
    • Why not manual: both functions would then always stay manual, even though sam deploy needs cloudformation:* and iam:PassRole, which this role must never have.
  • Release-triggered runs are dry runs until vars.LAMBDA_DEPLOY_MODE == 'live'. workflow_dispatch needs the workflow file at the dispatched ref, so no tag up to and including v0.151.0 can run it. Without this switch, v0.152.0's release run, after approval, would be the deployer's first use, and live. With it, the first use is a dry run on the release path. Going live is gh variable set LAMBDA_DEPLOY_MODE --repo spore-host/spawn --body live. If you'd rather it be live from the start, change the expression to false.
  • cgo is off for every build: the checklist computes the file set with CGO_ENABLED=0, and the manual deploys cross-compile from macOS, where cgo is off. On an ubuntu runner a native amd64 build would otherwise link glibc.
  • The bootstrap body is rendered the way release.yaml renders it. That means go run -ldflags "-X github.com/spore-host/spawn/cmd.Version=$V" . bootstrap-body, not the sweep script's bare go run .. Matching how the published body was produced is the stronger check. TestBodyRenderMatchesTheRelease keeps the two in step. The body is fetched over public HTTPS, as the script's curl does, so the role needs no s3:GetObject.
  • The account check reads the FunctionArn instead of calling sts get-caller-identity.

Pre-flights not ported (the role doesn't grant the call)

  • deploy-custom-dns.sh: the Function URL AuthType == AWS_IAM check needs lambda:GetFunctionUrlConfig. A code update doesn't change the URL config.
  • lambda/dashboard-api/deploy.sh: the IAM role existence check needs iam:GetRole, and only matters on create.

Every config write in the scripts is deliberately left out: timeout, memory, environment, runtime, role policies, log retention, Function URL and permissions. dashboard-api has two mechanisms with different ldflags, and the deployer uses the checklist's line (-ldflags="-s -w").

Gates, each watched failing against a compiling revert

Gate Revert (compiles) Result
TestEveryLambdaCallIsGrantedByTheCIRole: subcommands discovered from every callLambda literal (AST), exactly one aws exec, mapped kebab to Pascal against actions parsed from the script's POLICY_DOCUMENT; tag key against its aws:TagKeys; plus a dynamic run whose calls must be in the static set add a get-function-url-config call FAIL: lambda:GetFunctionUrlConfig not granted
same drop "lambda:GetFunction" from the script FAIL
same versionTag = "spawn:deployed-version" FAIL: tag key not allowed
TestOnlyChangedCodeOnlyLambdasAreDeployed (4 statuses × 3 kinds) && to || in selectTargets FAIL: changed/manual deployed
TestMismatchedLiveFunctionRefuses / TestArchitectureComesFromTheChecklistBuild drop the Architectures check FAIL
same drop the Runtime check FAIL
TestRemoteShaMismatchRollsBack false && on the sha check FAIL
TestConfigDiffRollsBack false && on the diff check FAIL
TestDryRunMakesNoMutatingCall (mutating = any non-get-/list- verb) if d.dryRun && false FAIL
TestDeployRoleIsAssumedOnlyInTheProductionEnvironment: every job in every workflow that assumes the role or runs the deployer environment: staging / removed / role ARN changed / script ROLE_NAME changed FAIL ×4
TestDeployWorkflowDefaultsToDryRunAndBlocksNoReleaseJob call default false; deploy-checklist needs: deploy-lambdas FAIL ×2

Also added:

  • TestEveryOperatedLambdaIsDeployable: the real inventory (deploychecklist HEAD..HEAD) parses for all 9 Lambdas, and the body check is found for sweep-orchestrator.
  • TestRealBuildZipLayout (non-short): a real sweep-orchestrator build twice, which gives one entry bootstrap, executable, and the same sha both times.
  • rollback, tag-restore, "changed since pre-flight", "already current", and dirty/untagged/pre-release checkout tests.

internal/iamcoverage: scripts/deploylambdas is registered as not-a-principal for the SDK call-graph analysis, since it calls AWS only through the CLI, with a pointer to the gate above.

Checks

  • GOTOOLCHAIN=go1.27.3 make check (from /private/tmp) passes. It includes golangci-lint, clean in all 10 modules, and the hermetic short tests.
  • The full (non-short) scripts/deploylambdas tests pass, including the real build and the real body render.
  • semgrep scan --config=auto --error .: 0 findings.
  • actionlint on deploy-lambdas.yaml and release.yaml: clean. The one actionlint note in the repo, SC2001 in ci.yml, is already on main.

Not exercised

There has been no AssumeRoleWithWebIdentity and no live run; no AWS calls were made for this PR.

@codecov

codecov Bot commented Oct 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit a661004 into main Oct 30, 2026
9 checks passed
@scttfrdmn
scttfrdmn deleted the feat/1445-deploy-lambdas branch October 30, 2026 21:26
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.

Approval-gated CI deploy of code-only Lambda changes after a release (OIDC role, production environment)

1 participant