Skip to content

fix(lambda): package only bootstrap, not the whole source directory - #647

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/645-sam-codeuri-build-dir
Oct 3, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/645-sam-codeuri-build-dir

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #645.

The bug

The three SAM-deployed lambdas used CodeUri: . (and pipeline-orchestrator had no CodeUri at all), and sam deploy zips that directory with no filtering of any kind. Read from the installed SAM CLI 1.166.2 rather than assumed — samcli/lib/package/utils.py:283:

for root, _, files in os.walk(source_root, followlinks=True):
    for filename in files:

The fix

Each build writes to a dedicated build/ directory containing nothing but bootstrap, with CodeUri pointed explicitly at it. The guarantee is structural rather than a filter: one file in the directory means one file in the zip, regardless of what SAM does or doesn't exclude.

Measured by driving SAM's own make_zip_with_permissions over the fixed directories:

lambda packaged entries was deployed
ttl-reaper 25.2 MiB 1 75.7 MiB
autoscale-orchestrator 7.8 MiB 1 7.1 MiB — not inflated
pipeline-orchestrator 17.1 MiB 1 not found in infra/us-east-1

Correcting my own numbers from the issue: I first wrote "~7 MiB" and ">10x inflation", which inferred the reaper's binary size from a different function's. It's 3x, and autoscale-orchestrator wasn't actually inflated in its last deploy — only latently exposed, since the next deploy from a dirty tree would have inflated it. Corrected on #645 too.

Besides the dev binary, two things were riding along: a nested function.zip holding a second copy of bootstrap (already compressed, hence near-incompressible — which is why 96 MB of files only squeezed to 75.7 MiB), and, after any release build, GoReleaser's ttl-reaper_lambda_linux_arm64.zip.

The part that mattered more than size

ttl-reaper's Makefile generates .deploy-params.yaml immediately before sam deploy, holding every deploy parameter including the NotifyUrl Slack webhook that the Makefile's own comment calls a secret. Because SAM uploads dotfiles, that file would have shipped inside the package, readable by anyone with lambda:GetFunction.

To be precise: nothing leaked. The value was empty in practice (0 matches for hooks.slack.com). But make deploy NOTIFY_URL=... is the documented usage, so it was a live trap, not a past incident. A test now fails if that file — or the Makefile's PARAMS_FILE — is ever inside a CodeUri directory.

Why not sam build

sam build + Metadata: BuildMethod: makefile is the AWS-documented route for provided.al2023 and would also work. Rejected because it changes the deploy workflow and these Makefiles encode hard-won invocations — notably ttl-reaper's file://-params-file workaround for sam's parameter-overrides regex, which exists because inline Key=Value pairs silently mis-key every parameter when one is empty. Smaller blast radius on a path that touches a live production reaper.

Verification

Not verified, stated plainly

No real deploy. sam deploy touches the live production reaper in the infra account, so the offline zip inspection plus sam validate is as far as this goes honestly. The confirmation is the next intentional make deploy, after which get-function-configuration --query CodeSize should read ~25 MiB rather than 75.7 MiB. Already-deployed packages aren't shrunk by merging this.

Open question (not blocking)

Was a real NOTIFY_URL ever passed to make deploy for spawn-ttl-reaper-*? If so that webhook is in the currently-deployed package and should be rotated. The deployed artifact can be downloaded to settle it, which I can do on request — read-only, but it means handling a potential secret, so I'd rather ask.

The three SAM-deployed lambdas used `CodeUri: .` (pipeline-orchestrator had no
CodeUri at all), and `sam deploy` zips that directory with NO filtering of any
kind. Read from the installed SAM CLI 1.166.2 rather than assumed —
samcli/lib/package/utils.py:283:

    for root, _, files in os.walk(source_root, followlinks=True):
        for filename in files:

There is no `.samignore` feature (no such string anywhere in samcli/) and
`.gitignore` is not consulted, so #637 kept the stray dev binary out of git
without keeping it out of the deployment package.

Omitting CodeUri is worse, not better: ServerlessFunctionResource inherits
PACKAGE_NULL_PROPERTY = True, and upload_local_artifacts' own docstring reads
"If path is omitted, this method will zip the current working folder and
upload" — so pipeline-orchestrator packaged whatever cwd happened to be.

Each build now writes to a dedicated build/ directory containing nothing but
bootstrap, with CodeUri pointed explicitly at it. The guarantee is structural:
one file in the directory means one file in the zip, regardless of what SAM
filters.

Measured by driving SAM's own make_zip_with_permissions over the fixed dirs:

  ttl-reaper              25.2 MiB, 1 entry  (was 75.7 MiB deployed)
  autoscale-orchestrator   7.8 MiB, 1 entry  (7.1 MiB deployed — not inflated)
  pipeline-orchestrator   17.1 MiB, 1 entry

Correcting my own figure on the issue: I first claimed ~7 MiB and ">10x", which
inferred the reaper's binary size from a different function's. It is 3x, and
autoscale-orchestrator was not actually inflated in its last deploy — only
latently exposed.

Besides the dev binary, two things were riding along: a nested function.zip
holding a SECOND copy of bootstrap (already compressed, hence near-
incompressible — which is why 96 MB of files only squeezed to 75.7 MiB), and,
after any release build, GoReleaser's ttl-reaper_lambda_linux_arm64.zip.

The part that mattered more than size: ttl-reaper's Makefile generates
.deploy-params.yaml immediately before `sam deploy`, holding every deploy
parameter including the NotifyUrl Slack webhook that its own comment calls a
secret. Because SAM uploads dotfiles, that file would have shipped inside the
package, readable by anyone with lambda:GetFunction. Nothing leaked — the value
was empty in practice — but `make deploy NOTIFY_URL=...` is the documented
usage, so it was a live trap rather than a past incident.

Chose this over `sam build` + Metadata.BuildMethod: makefile (the AWS-documented
route). That would also work, but it changes the deploy workflow and these
Makefiles encode hard-won invocations — notably ttl-reaper's file://-params-file
workaround for sam's parameter-overrides regex. Smaller blast radius on a path
that touches a live production reaper.

Published artifact layout is unchanged: `zip -j` keeps bootstrap at the release
zip's root, which `spawn reaper deploy` (#625) depends on. Verified by running
the GoReleaser before-hook exactly as CI does and listing the zip.

Gates: all four inversions confirmed red (CodeUri: ., CodeUri absent, a stray
file in build/, and .deploy-params.yaml inside build/). `sam validate` passes on
all three templates. `make build` in each leaves git status clean. semgrep run
locally: 0 findings.

NOT verified by a real deploy: `sam deploy` touches the live production reaper in
the infra account. The offline zip inspection and `sam validate` are as far as
this goes honestly; the confirmation is the next intentional `make deploy`, after
which CodeSize should read ~25 MiB rather than 75.7 MiB.

Fixes #645
@codecov

codecov Bot commented Oct 3, 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 cbe402e into main Oct 3, 2026
7 checks passed
@scttfrdmn
scttfrdmn deleted the fix/645-sam-codeuri-build-dir branch October 3, 2026 21:30
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.

SAM packages the entire CodeUri directory, so the ttl-reaper deploys at 75.7 MiB instead of ~7 MiB (and would ship .deploy-params.yaml)

1 participant