Repository navigation
fix(lambda): package only bootstrap, not the whole source directory - #647
Merged
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #645.
The bug
The three SAM-deployed lambdas used
CodeUri: .(andpipeline-orchestratorhad noCodeUriat all), andsam deployzips 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:.samignoreis not a SAM CLI feature — no such string anywhere insamcli/. (I suggested adding one on A 22 MiB compiled binary is tracked in git at lambda/autoscale-orchestrator/autoscale-orchestrator #637; that was wrong, and this PR replaces that suggestion.).gitignoreis not consulted either, so A 22 MiB compiled binary is tracked in git at lambda/autoscale-orchestrator/autoscale-orchestrator #637 kept the dev binary out of git without keeping it out of the deployment package.CodeUriis worse, not better:ServerlessFunctionResourceinheritsPACKAGE_NULL_PROPERTY = True, andupload_local_artifacts' own docstring reads "If path is omitted, this method will zip the current working folder and upload" — sopipeline-orchestratorpackaged whatevercwdhappened to be.The fix
Each build writes to a dedicated
build/directory containing nothing butbootstrap, withCodeUripointed 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_permissionsover the fixed directories:ttl-reaperautoscale-orchestratorpipeline-orchestratorCorrecting 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-orchestratorwasn'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.zipholding a second copy ofbootstrap(already compressed, hence near-incompressible — which is why 96 MB of files only squeezed to 75.7 MiB), and, after any release build, GoReleaser'sttl-reaper_lambda_linux_arm64.zip.The part that mattered more than size
ttl-reaper's Makefile generates.deploy-params.yamlimmediately beforesam deploy, holding every deploy parameter including theNotifyUrlSlack 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 withlambda:GetFunction.To be precise: nothing leaked. The value was empty in practice (0 matches for
hooks.slack.com). Butmake 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'sPARAMS_FILE— is ever inside aCodeUridirectory.Why not
sam buildsam build+Metadata: BuildMethod: makefileis the AWS-documented route forprovided.al2023and would also work. Rejected because it changes the deploy workflow and these Makefiles encode hard-won invocations — notably ttl-reaper'sfile://-params-file workaround for sam's parameter-overrides regex, which exists because inlineKey=Valuepairs silently mis-key every parameter when one is empty. Smaller blast radius on a path that touches a live production reaper.Verification
CodeUri: .,CodeUriabsent, a stray file inbuild/, and.deploy-params.yamlinsidebuild/.sam validatepasses on all three templates.make buildin each leavesgit statusclean — the A 22 MiB compiled binary is tracked in git at lambda/autoscale-orchestrator/autoscale-orchestrator #637 footgun stays shut.spawn reaper deploy(No supported way to run the TTL reaper in your own account, so governed orgs can't have reaper coverage at all #625) consumes it: ran the GoReleaser before-hook exactly as CI does and listed the zip —bootstrapat the root, viazip -j.make check-fmt,go vet, fullgo test -short ./...,make check-docs, andsemgrep scan --config=auto --error .(0 findings) — all run locally before pushing.Not verified, stated plainly
No real deploy.
sam deploytouches the live production reaper in the infra account, so the offline zip inspection plussam validateis as far as this goes honestly. The confirmation is the next intentionalmake deploy, after whichget-function-configuration --query CodeSizeshould 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_URLever passed tomake deployforspawn-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.