fix(layers): add createRequire
banner in esm
#2232
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of your changes
This PR introduces a new step in the layer publisher workflow that should fix a bug affecting customers using the Powertools Lambda Layers with functions bundled using ESM format.
As described in the linked issue, when the above conditions are met, the function throws a runtime error with the stack trace pointing at the
/opt/nodejs/node_modules
, which is where the code for our utilities is stored when using Layers.The fix mirrors the recommendation that we give to customers having to support the
require
keyword and adds a polyfill at the top of the file causing the issue.The step adds this banner dynamically when the Lambda layers are built and it does so only on the ESM build and on one specific file, which is importing the AWS X-Ray SDK for Node.js. While this is not a great solution, it's the only one that I could come up with that will allow us to have the polyfill present only in this specific case.
Adding the polyfill to our source would cause it to appear also when bundling for CJS, which would lead to a different runtime error, this time due to the
require
keyword defined twice.While this PR doesn't add any test, I have tested manually the resulting layer build with functions in CJS and ESM both with and without Lambda Layers and even when adding a banner to the function bundle, it appears to fix the issue.
By the end of this iteration, or the following at the latest, I'll work on adding ESM builds to our integration test matrix.
Related issues, RFCs
Issue number: #2231
Checklist
Breaking change checklist
Is it a breaking change?: NO
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.