Lambda@Edge "function ... exceeds the maximum" size in CI
Lambda@Edge enforces stricter package limits than regular Lambda. Viewer request and viewer response functions are capped at 1 MB (compressed), and origin events at 50 MB. A package over the limit is rejected when you associate it with CloudFront.
What this error means
The deploy or CloudFront association fails with "The function code with layers exceeds the maximum size" or an InvalidLambdaFunctionAssociation error citing the size limit for the event type.
An error occurred (InvalidLambdaFunctionAssociation) when calling the
UpdateDistribution operation: The function code with layers exceeds the
maximum size of 1048576 bytes for the viewer request event.Common causes
A viewer function over the 1 MB cap
Viewer request/response functions allow only 1 MB compressed. Bundling dependencies into a viewer function quickly exceeds that.
Bundled dependencies that belong elsewhere
Heavy SDKs or assets are bundled into the edge function that could run at the origin or be fetched at runtime.
How to fix it
Shrink the viewer function
- Remove dependencies the viewer function does not need.
- Tree-shake and minify the bundle so it fits under 1 MB.
- Use a viewer function only for lightweight header and URL logic.
Move heavy logic to an origin event
Origin request/response functions allow up to 50 MB. Run dependency-heavy logic there instead of on viewer events.
How to prevent it
- Keep viewer functions tiny; reserve origin events for larger code.
- Audit the bundle size against the 1 MB / 50 MB caps in CI.
- Fetch data at runtime rather than bundling it into the package.