javascriptnode.jsamazon-web-servicesaws-lambdaaws-lambda-layers

"Runtime.ImportModuleError" trying to access npm package in an AWS lambda function using layers


I'd like to use the npm package "request" in an AWS lambda function.

I'm trying to follow the procedure outlined in this article here: https://medium.com/@anjanava.biswas/nodejs-runtime-environment-with-aws-lambda-layers-f3914613e20e

I've created a directory structure like this:

nodejs
│   package-lock.json
│   package.json
└───node_modules

My package.json looks like this:

{
  "name": "my-package-name",
  "version": "1.0.0",
  "description": "whatever",
  "author": "My Name",
  "license": "MIT",
  "dependencies": {
    "request": "^2.88.0"
  }
}

As far as I can tell from the article, all I should have to do with the above is run npm i, zip up the directory, upload it as a layer, and add the layer to my lambda function.

screenshot

I've done all of that, but all that I get when I try to test my function is this:

{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module 'request'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module 'request'",
    "Require stack:",
    ...

...as if the layer had never been added. The error is exactly the same whether the layer is added or not. If there's some sort of permissions issue that needs to be resolved, there's nothing in the article that indicates that.

I've tried a few different things, like whether or not my .zip file contains the top-level directory "nodejs" or just its contents. I've tried adding "main": "index.js", to my package.json, with an index.js file like this:

export.modules.request = require('request');

...all to no avail.

What am I missing?


Solution

  • Oh, I can't believe it's just this!

    The top-level directory for the .zip file must LITERALLY be named "nodejs"! I was using a different name, and only changed it back to "nodejs" in the text of this post to be more generic, but the directory name was the real problem all along.

    Sigh.