0
votes

I have created a lambda function that relies on some dependencies in a node_modules folder.

I had originally created the lambda with a node_modules folder inside of it, and it worked fine when testing.

I then created a layer, uploaded the node_modules folder to it, and set the runtime to Node.js 8.10.

I then added the layer to the lambda, created a policy with the following JSON, and added the policy to the role associated with the lambda:

{
  "Version": "2012-10-17",
  "Statement": [
    {
        "Effect": "Allow",
        "Action": "lambda:GetLayerVersion",
        "Resource": "*"
    }
  ]
}

When I test the Lambda, then dependencies that I am trying to include from the node_modules folder located within the associated layer are not found.

Update:

I've also run the following command:

aws lambda add-layer-version-permission --layer-name node_modules --version-number 2 \ --statement-id publish --action lambda:GetLayerVersion --principal "*"

But the problem has not been resolved.

2

2 Answers

2
votes

You can also set the NODE_PATH environment variable for your lambda to be

NODE_PATH: './:/opt/node_modules'

This way you don't need to have opt/node_modules in your code when you import something.

0
votes

The layer worked after I required dependencies with "/opt/node_modules" path like so:

const mongoose = require("/opt/node_modules/mongoose");
const mongo = require("/opt/node_modules/mongodb");