Skip to content

fix: correct sharp cross-platform bundling for image handler Lambda#647

Open
ButterflyJuicer0 wants to merge 1 commit into
aws-solutions:mainfrom
ButterflyJuicer0:fix/sharp-cross-platform-bundling
Open

fix: correct sharp cross-platform bundling for image handler Lambda#647
ButterflyJuicer0 wants to merge 1 commit into
aws-solutions:mainfrom
ButterflyJuicer0:fix/sharp-cross-platform-bundling

Conversation

@ButterflyJuicer0

@ButterflyJuicer0 ButterflyJuicer0 commented May 29, 2026

Copy link
Copy Markdown

Fixes #646

Summary

The afterBundling command hook for the image handler Lambda produced a macOS (darwin-arm64) sharp binary that fails to load on the linux-x64 Lambda runtime, causing Could not load the "sharp" module and HTTP 502 for every request when synthesized on macOS.

This PR fixes three compounding issues in a single hook command:

  • cd was split from the install. The hook returned two array elements; CDK runs each in its own shell, so cd ${outputDir} never applied to the npm install. They are now chained into one command.
  • @img/ binaries were not removed. sharp 0.33+ ships native binaries under @img/sharp-<platform>, so removing only node_modules/sharp left the host darwin binary. node_modules/@img is now removed too.
  • The lock file pinned darwin deps. The copied package-lock.json overrode the --os/--cpu flags. It is now removed, and --include=optional is added so npm 11 actually pulls the linux-x64 @img package.

修复说明

afterBundling 钩子打进 Lambda 的是 macOS 版 sharp 二进制,在 linux-x64 运行时加载失败,导致每个请求 502。本 PR 在单条钩子命令里修复三个叠加问题:cd 与 install 合并为一条命令;额外删除 @img/(sharp 0.33+ 的原生二进制目录);删除锁定 darwin 的 package-lock.json 并加 --include=optional 以适配 npm 11。

Test plan

  • cdk synth on macOS now produces @img/sharp-linux-x64 (verified the staged asset).
  • Deployed v7-Stack to us-east-1; image handler Lambda loads sharp successfully.
  • Verified via CloudFront: original passthrough, resize (300x200), and grayscale + WebP conversion all return HTTP 200.

The afterBundling hook produced a darwin sharp binary that fails to load
on the linux-x64 Lambda runtime ("Could not load the sharp module"),
returning 502 for every request when synthesized on macOS.

Three compounding issues are fixed:
- The `cd` and `npm install` were separate array elements; CDK runs each
  in its own shell, so the directory change never applied to the install.
  They are now a single chained command.
- Only `node_modules/sharp` was removed, but sharp 0.33+ ships native
  binaries under `@img/sharp-<platform>`, so the host binary survived.
  `node_modules/@img` is now removed too.
- The copied package-lock.json pinned darwin deps and overrode the
  --os/--cpu flags. The lock is now removed and --include=optional is
  added so npm 11 actually pulls the linux-x64 @img package.
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.

[Bug] sharp module fails to load on Lambda (linux-x64) when synthesized on macOS — 502 errors

1 participant