Skip to content

fix(webapp): CloudFront Functions fail - #1906

Merged
cdklabs-automation merged 1 commit into
mainfrom
mrgrain/fix/webapp/cloudfront-function-module-prologue
Jul 28, 2026
Merged

fix(webapp): CloudFront Functions fail#1906
cdklabs-automation merged 1 commit into
mainfrom
mrgrain/fix/webapp/cloudfront-function-module-prologue

Conversation

@mrgrain

@mrgrain mrgrain commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Fixes the v0.4.515 issue where requests to a Construct Hub deployment returned HTTP 503 with x-cache: FunctionExecutionError from cloudfront. The compiled code of the AddHeadersFunction (and the BadgeRedirectFunction) is inlined verbatim into AWS::CloudFront::Function resources, but it was produced by plain tsc. After the toolchain restructuring in #1897, the compiler started prepending the CommonJS prologue Object.defineProperty(exports, "__esModule", { value: true }); to the output. The CloudFront Functions runtime has no module system and no exports binding, so the function threw a ReferenceError on every invocation, taking down the whole distribution.

Relying on tsc emit for these files was always fragile, because whether the prologue appears depends on tsconfig details that are unrelated to these files and can change in routine dependency or config upgrades — exactly what happened in #1897. Both handlers are now bundled with a dedicated esbuild configuration (esm format so no module prologue is emitted, es5 target, neutral platform, tree-shaking disabled so the never-exported handler survives), modelled after the CloudFront function bundler in mrgrain/cdk-esbuild. The bundle step runs at the end of compile and replaces the tsc output in lib/, so the shipped artifact is deterministic regardless of tsconfig changes. The bundling script additionally fails the build if the output contains any module system artifacts or lacks a top-level function handler(...) declaration.

To guard against any future regression of this class, a new test loads the shipped bundles into a bare vm sandbox that — like the CloudFront runtime — provides no exports, module, or require, and invokes the handlers to verify their behaviour. This test fails against the v0.4.515 artifact.

While touching these functions, both are upgraded from cloudfront-js-1.0 to cloudfront-js-2.0. The 2.0 runtime is backwards compatible for this code, lifts the ES 5.1 restrictions, and the esbuild configuration derives its feature lowering from the target runtime, so future handler code can use modern syntax safely.

Tested with yarn build: all unit tests pass (including the new regression tests), integ snapshots are unchanged, and the jest snapshots were regenerated to reflect the clean function code and new runtime.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

…iled output contains a CommonJS module prologue

The compiled code of the AddHeadersFunction and BadgeRedirectFunction is
inlined verbatim into AWS::CloudFront::Function resources, but was produced
by plain tsc. Depending on the tsconfig, tsc prepends
`Object.defineProperty(exports, "__esModule", { value: true });` — `exports`
does not exist in the CloudFront Functions runtime, so the function throws
on every invocation and the distribution returns 503 for all requests
(regression introduced by the toolchain changes in #1897, shipped in
v0.4.515).

Bundle both handlers with a dedicated esbuild configuration (esm format,
es5 target, neutral platform, no tree-shaking) modelled after
mrgrain/cdk-esbuild's CloudFront function code bundler, replacing the tsc
output at compile time. The bundling script validates that the output
contains no module system artifacts and a top-level handler declaration.

Add a regression test that executes the shipped bundles in a bare vm
sandbox — like the CloudFront runtime, without exports/module/require —
and invokes the handlers.

Also upgrade both functions to cloudfront-js-2.0.

Fixes the v0.4.515 outage report.
@github-actions

Copy link
Copy Markdown
Contributor

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

@mrgrain mrgrain changed the title fix(webapp): CloudFront Functions crash with ReferenceError from CommonJS module prologue fix(webapp): CloudFront Functions fails Jul 28, 2026
@mrgrain mrgrain changed the title fix(webapp): CloudFront Functions fails fix(webapp): CloudFront Functions fail Jul 28, 2026
@cdklabs-automation
cdklabs-automation added this pull request to the merge queue Jul 28, 2026
Merged via the queue into main with commit a130aec Jul 28, 2026
12 checks passed
@cdklabs-automation
cdklabs-automation deleted the mrgrain/fix/webapp/cloudfront-function-module-prologue branch July 28, 2026 16:51
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.

3 participants