https://github.com/coderbyheart/aws-lambda-esm-with-layer
Demonstrates how to use an AWS Lambda with Node.js using ES modules and dependencies from a layer
https://github.com/coderbyheart/aws-lambda-esm-with-layer
Last synced: about 2 months ago
JSON representation
Demonstrates how to use an AWS Lambda with Node.js using ES modules and dependencies from a layer
- Host: GitHub
- URL: https://github.com/coderbyheart/aws-lambda-esm-with-layer
- Owner: coderbyheart
- Created: 2022-01-29T00:10:59.000Z (almost 4 years ago)
- Default Branch: saga
- Last Pushed: 2022-03-17T12:40:37.000Z (almost 4 years ago)
- Last Synced: 2025-04-14T17:55:32.338Z (8 months ago)
- Language: JavaScript
- Size: 3.91 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
> ℹ️ *Note*: [symlinking the node_modules folder](https://github.com/vibe/aws-esm-modules-layer-support) is a simpler solution.
## Instructions
1. Run `make`.
1. Create a Node JS 14 Lambda Function from `dist/lambda-nodejs14.zip`.
1. Create a Lambda Layer from `dist/layer.zip`.
1. Attach layer to the function.
1. Invoke function. Function will load an ESM from the layer.
---
The `NODE_PATH` variable is not supported by the ESM loader in Node, as seen [here](https://nodejs.org/docs/latest-v14.x/api/esm.html#esm_no_node_path). In addition, the `NODE_PATH` is not part of resolving import specifiers, which means the layer cannot be reached.
The [workaround](./user/index.js) would be to use a module from a layer, which would be to expose the dynamic import function from the module that you require.
---
> Using that workaround I cannot `import { s3 } from 'esm_in_layer'` but use explicit, async loading. Adding this boilerplate code to all lambdas adds additional workload which I want to avoid.