Skip to content

feat(aws): Create unified lambda layer for ESM and CJS #17012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

msonnb
Copy link
Member

@msonnb msonnb commented Jul 15, 2025

This introduces a new AWS lambda layer that supports both ESM and CJS. Instead of bundling the whole SDK, we install the local NPM package and then prune all not strictly necessary files from node_modules by using @vercel/nft to keep the layer size as small as possible.

closes #16876
closes #16883
closes #16886
closes #16879

'build/aws/dist-serverless/nodejs/node_modules/@sentry/aws-serverless/build/npm/cjs/index.js',
);
console.log('Installing local @sentry/aws-serverless into build/aws/dist-serverless/nodejs.');
run('npm install . --prefix ./build/aws/dist-serverless/nodejs --install-links --silent');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use yarn here, for consistency? 🤔 no strong feelings, just thinking about it...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, I think we can use --production here possibly too, to avoid even installing dev dependencies...?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use yarn here, for consistency? 🤔 no strong feelings, just thinking about it...

I couldn't find an equivalent yarn command. There is yarn file:/some/path but from my quick testing this installs the root sentry-javascript package instead of just the one package. (Also there is no --prefix equivalent in yarn v1)

also, I think we can use --production here possibly too, to avoid even installing dev dependencies...?

I tried that but it didn't really work for some reason, node_modules was still >50MB.


console.log('Cleaning up empty directories.');

removeEmptyDirs('./build/aws/dist-serverless/nodejs/node_modules');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it important/necessary that we clean up empty dirs? Just wondering...
Alternate idea, not sure if this makes sense: instead of deleting the files that are not in nft, could we instead just copy the files that are traced by nft into a final place? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it important/necessary that we clean up empty dirs? Just wondering...

Not strictly necessary, just saves a few more bytes I guess...

Alternate idea, not sure if this makes sense: instead of deleting the files that are not in nft, could we instead just copy the files that are traced by nft into a final place? 🤔

This was the original idea behind #3110 but was later abandoned in #5146 since it was too unreliable (and way more complex).


removeEmptyDirs('./build/aws/dist-serverless/nodejs/node_modules');

await minifyJavaScriptFiles(fileList);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how good of an idea it is to do this manually here... 🤔 could we instead leverage a regular rollup build for this step, somehow?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be clear, I am not saying we def. need to do that, just that we should think about it :D what about other things our rollup build does, other than minifying, ...?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how good of an idea it is to do this manually here...

yeah I agree, already encountered some issues with otel. I removed it for now, might tackle later in a follow-up.

@msonnb msonnb self-assigned this Jul 15, 2025
@msonnb msonnb force-pushed the ms/esm-lambda-layer branch 2 times, most recently from 300b155 to 88d24b0 Compare July 16, 2025 10:09
@msonnb msonnb force-pushed the ms/esm-lambda-layer branch from 88d24b0 to 5a9af39 Compare July 18, 2025 07:37
@msonnb msonnb marked this pull request as ready for review July 18, 2025 07:37
@msonnb msonnb requested a review from andreiborza July 18, 2025 07:40
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Build Script Crashes on Missing Files

The fs.unlinkSync() calls for package.json and package-lock.json lack error handling. If these files are missing, the build script will crash. These operations should either be wrapped in try-catch blocks or use fs.rmSync() with force: true.

packages/aws-serverless/scripts/buildLambdaLayer.ts#L26-L28

await pruneNodeModules();
fs.unlinkSync('./build/aws/dist-serverless/nodejs/package.json');
fs.unlinkSync('./build/aws/dist-serverless/nodejs/package-lock.json');

Fix in CursorFix in Web


Was this report helpful? Give feedback by reacting with 👍 or 👎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants