aws lambda layer shared library in node_modules not accesible in function
You able to create the Layer and it's version properly and also able to retrieve information about them without any issues.
Even after that Lambda function is not able to use the libraries from the Layer, looks like the Lambda function is not able to access the Layer because it does not have the permission to do so.
You can confirm this by checking if the role associated with the Lambda is having any policy attached to it which allows lambda:GetLayerVersion
permission.
If not you need to create a new policy with following JSON and some meaningful name.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "lambda:GetLayerVersion",
"Resource": "*"
}
]
}
Now you need to add attach this policy to the role arn:aws:iam::MY_ID:role/service-role/MY_ROLE
which associated with the Lambda function.
Both of the above actions can be performed from AWS Console.
Once this is done, the Lambda function will have permission to get the layer version and should be able access the libraries from the Layer.
I hope this would help you resolve your issue. Feel free to ask if you need any clarifications.
I was having an issue with this as well. Turns out you need to make sure you have your zip file set up correctly, using 'nodejs' as the folder name. If you create a folder named 'nodejs' with just your package.json, run npm install, and then zip that up, then you can use 'require' normally.
In my case, I am using the NPM packages axios, aws-sdk, and http. Here's the steps I took:
- Create a 'nodejs' directory
- Copy your package.json file into it
- Run npm install in the nodejs directory
- From one level up, zip the directory. On a Mac:
zip nodejs.zip nodejs/ -r
- Create a layer in AWS, import the zip, and give the layer the Node.js runtime
- Add the layer to your lambda
Now I can use my require statements normally.
const axios = require('axios')
const AWS = require('aws-sdk');
const http = require('http');
Also note that your lambda's role, under Basic Settings, should have lambda permissions. For example, if your role has the AWSLambdaFullAccess policy attached, or another policy with the correct lambda permissions (like "lambda:*"), then you are fine.
I have also stuck in this problem and not able to figure it out because I have applied all mentioned things like permission and node zip but was n't successful after a while I used the const lodash = require('/opt/nodejs/node_modules/lodash') It works for me.