I am trying to add a layer to my lambda functions where I can include a module from NPM. I use this module for several lambda functions and read that a layer is a great way to reuse the code.
I am using node 18.x and, therefore, need to 'import' the module which should be available once I have created and added the layer.
import { CognitoJwtVerifier } from "aws-jwt-verify"
When I test the function the error message states that it can't find the package. The process.env.NODE_Path shows: /opt/nodejs/node18/node_modules:/opt/nodejs/node_modules:/var/runtime/node_modules:/var/runtime:/var/task. I have included this package before with my lambda functions by uploading a .zip archive with the package included and it has always worked before.
While it is possible that I have not created the layer properly, I followed clear instructions for doing so and don't think this is the problem.
I have come across several 'solutions' including using symlinks, but most of those seem to be for older versions of Node that don't work well when trying to use ES6. For that matter, AWS suggests that path problems are resolved when using node 18.x. See https://aws.amazon.com/blogs/compute/node-js-18-x-runtime-now-available-in-aws-lambda/
Are there any common problems I could be missing?
Got it working. Here is the issue and the solution that worked for me:
When you add the Layer to your Lambda (I used the console), you can optionally specify the Compatible Runtimes. I am using Node 18.x, so I specified that runtime, but the function would never find the modules I added via the layer.
I checked the PATH (as stated in my original question above) and noticed that in addition to the regular path (ie. opt/nodejs/node_modules) it also showed the runtime specific path of /opt/nodejs/node18/node_modules (bolding added by me to highlight difference).
I then recreated the module from scratch with that altered directory structure (ie. nodejs/node18/node_modules) using the same procedure as before and uploaded that layer. Everything then worked perfectly.