0

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 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.