Skip to content

fix(lambda): report the size and digest of the deployed package (#545) - #557

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/lambda-code-size
Aug 4, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/lambda-code-size

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #545

CodeSize was 0 and CodeSha256 empty for every function whose code did not arrive as an inline base64 ZipFile on a direct CreateFunction. A consumer polling CodeSize to decide whether a deploy took effect was told nothing had, whatever it deployed — this release's shape: a request accepted, answered 200, producing no effect a caller can observe.

The issue's premise was confounded, and correcting it doubled the change

It was filed as a CloudFormation problem. Measuring all four combinations against v0.89.0 showed the axis is inline vs. S3 source, not CFN vs. direct:

inline ZipFile S3 source
direct CreateFunction 156 (correct) 0
CloudFormation 0 0

CFN + inline is the row the report never tested, and the one that proves there were two independent gaps rather than one.

CloudFormation sent no Code at all

deployLambdaFunction built a CreateFunction body carrying FunctionName, Runtime, Role, Handler, Description, Timeout and MemorySize and no Code key of any kind. So every CFN-deployed function reported size 0 and an empty digest however its template declared its code, a container-image function lost both its image and its PackageType, and UpdateFunctionCode drift was undetectable. All of ZipFile, S3Bucket, S3Key, S3ObjectVersion and ImageUri are now forwarded, each through the template's Ref and pseudo-parameter resolution like any other property.

An inline template needed more than forwarding

The resource type's Code.ZipFile is "the source code of your Lambda function" — "CloudFormation places it in a file named index and zips it to create a deployment package" — while the API's Code.ZipFile is "the base64-encoded contents of the deployment package". Forwarding the template's string would have handed Lambda something that is not a package and usually not valid base64, leaving CodeSize at 0 exactly as before. Substrate now builds the archive CloudFormation would have built: one entry named index with the extension the runtime reads (.js for nodejs* per the reference, .py for python*, bare index otherwise), carrying no timestamps so the same template deployed twice yields the same package and the same digest. A digest that moved on every deploy would not be worth comparing.

An S3-sourced package is sized, and the digest stopped being random

Both createFunction and updateFunctionCode read the object's recorded length out of substrate's own S3 state, honouring S3ObjectVersion. ZipStored stays false — the bytes are still not staged for execution, which is a separate and correct fact from knowing the size.

UpdateFunctionCode assigned CodeSha256 a fresh random value on every call, so a caller asking "did the code change?" was always told yes. The digest is now derived from the package, an update carrying no package changes neither size nor digest, and an image function's digest tracks its URI from creation rather than only after the first update. RevisionId is not a digest of anything and still advances on every call.

Substrate's own decisions

Two, both stated in docs/services.md:

  • An S3-sourced package's digest is the object's ETag, not a SHA256. Substrate never fetches the bytes; for a single-part upload the ETag is the MD5 of the body, so it changes exactly when the package changes — the question a caller comparing digests is actually asking.
  • An absent S3 object does not fail the create the way real Lambda would. Substrate's S3 and Lambda state are independent and a template may legitimately name an object a test never uploaded, so it logs and reports size 0. A deleted object — one hidden by a delete marker — counts as absent rather than as a zero-length package.

Two adjacent gaps, fixed with it

Leaving either would have kept a write with no observable. Code.ImageUri, the documented spelling, was accepted only at the top level; it now wins over the legacy field and infers PackageType: Image. And GetFunction reported RepositoryType: S3 with a stub presigned URL for an image-packaged function — a claim a caller can act on and be wrong about — and now reports RepositoryType: ECR with ImageUri/ResolvedImageUri and no Location.

Tests

16 tests in emulator/lambda_code_size_test.go, asserting on the JSON GetFunction returns rather than on the state record, since CodeSize is only interesting as an observation a caller makes. With the two source files stashed, 12 fail; the 3 that pass are the deliberate no-regression guards (absent object, CFN absent object, CFN with no Code).

Mutation testing: 21 mutants, 21 CAUGHT, 0 survivors, including the plan's named mutant (Code forwarded for ZipFile but not for S3Bucket), the deployer not calling the Code helper at all (the exact pre-fix state), inline code forwarded unzipped, the entry name ignoring the runtime, a nondeterministic archive, the S3 sizing removed from create and from update, S3ObjectVersion ignored, an absent object reporting a size, the ETag not de-quoted, a delete marker read as an object, and four on the image path.

Three mutants were informative rather than merely caught:

  • Deflate → Store survived and is a genuine equivalent — the compression method is unconstrained; only the bytes' determinism is.
  • hdr.Modified = time.Now() survived because ZIP stores mtime at two-second granularity, so both deploys in a test land in the same slot. Rewritten to put the nondeterminism in the payload → CAUGHT.
  • Randomising the update digest survived and was a real gap: it exposed that an ImageUri update left CodeSha256 untouched. Fixed in the plugin; the mutant and two new ones now fail.

The image create path reporting no digest was found by the live e2e below, not by the tests, and is covered by a new mutant (M21).

Verification

gofmt -l ., go build ./..., go vet ./..., golangci-lint run ./... → 0 issues, make test (race) green, make docs-reference docs-reference-check docs-versions, npm --prefix docs run build, go vet -C test/e2e ./..., go test -C test/e2e ./... all pass.

Live, against substrate server:

cfn-s3      : 156  (was 0)
cfn-inline  : 189  (was 0)
direct-s3   : 156  63c06d925da678da514971ea76cc81a1
missing-s3  : 0    (empty digest, create still succeeds)
img-fn      : Image  Ag8hBGe6jxmsncGBq/TAoOzxwcbYdPXFmQdwPiTnB58=
              Code: {"RepositoryType":"ECR","ImageUri":…,"ResolvedImageUri":…}

Compatibility

A test asserting that CodeSize is 0, that CodeSha256 is empty, or that CodeSha256 changes on every UpdateFunctionCode will go red. That is the fix. A test asserting RepositoryType: S3 for an image-packaged function will also go red.

CodeSize was 0 and CodeSha256 empty for every function whose code did not
arrive as an inline base64 ZipFile on a direct CreateFunction, so a consumer
polling CodeSize to decide whether a deploy took effect was told nothing had.

The issue's premise was confounded. Measuring all four combinations showed the
axis is inline vs. S3 source, not CFN vs. direct: direct+ZipFile=156,
direct+S3=0, CFN+S3=0, and CFN+inline ZipFile=0 — the row the report never
tested, proving two independent gaps.

CloudFormation sent no Code key at all, so a CFN function reported size 0
however its template declared its code and an image function lost both its
image and its PackageType. All of ZipFile, S3Bucket, S3Key, S3ObjectVersion
and ImageUri are now forwarded, Ref-resolved like any other property.

An inline template needed more than forwarding: the resource type's ZipFile is
source text CloudFormation "places in a file named index and zips", while the
API's is "the base64-encoded contents of the deployment package". Substrate now
builds that archive, one entry named for the runtime (.js, .py, or bare index),
with no timestamps so the same template yields the same digest twice.

An S3-sourced package is sized from substrate's own S3 state, honouring
S3ObjectVersion; ZipStored stays false. UpdateFunctionCode no longer randomises
CodeSha256, and an image function's digest tracks its URI from creation.

Two decisions are substrate's own and docs/services.md states both: an
S3-sourced package's digest is the object's ETag, and an absent object deploys
with size 0 rather than failing.

Two adjacent gaps are fixed with it: Code.ImageUri was accepted only at the top
level, and GetFunction claimed RepositoryType S3 with a stub presigned URL for
an image-packaged function, which now reports ECR.
@codecov

codecov Bot commented Aug 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.15842% with 16 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/cfn_deployer.go 78.94% 4 Missing and 4 partials ⚠️
emulator/lambda_plugin.go 87.30% 6 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 61eacc0 into main Aug 4, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the feat/lambda-code-size branch August 9, 2026 17:48
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.

A CloudFormation-deployed AWS::Lambda::Function reports CodeSize: 0

1 participant