aws-lambda-esm-with-layer

Demonstrates how to use an AWS Lambda with Node.js using ES modules and dependencies from a layer

Stars
6

Note: symlinking the node_modules folder is a simpler solution.

Instructions

  1. Run make.
  2. Create a Node JS 14 Lambda Function from dist/lambda-nodejs14.zip.
  3. Create a Lambda Layer from dist/layer.zip.
  4. Attach layer to the function.
  5. 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. In addition, the NODE_PATH is not part of resolving import specifiers, which means the layer cannot be reached.

The workaround 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.