I have created a lambda function on node.js and attached a layer with it. But when I execute my lambda function, it is not reading JS bundled in layer. Please let me know if I am making any mistake here.
Followed this post and created a layer 'my-utility' and uploaded nodejs.zip.
AWS Console > Lambda > Layers > Create Layer
Layer Structure
my-utility
- nodejs
- node_modules
- myutil.js
- package.json
- package-lock.json
- nodejs.zip
myutil.js
function myFun(name) {
console.log("Hello.. " + name);
}
Lambda Code (Node.js 10.x)
const myutil = require('/opt/nodejs/myutil.js');
exports.handler = async (event) => {
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
console.log('Layer Code :', myutil);
return response;
};
After testing above lambda, it gives the below result:
Response:
{
"statusCode": 200,
"body": "\"Hello from Lambda!\""
}
Request ID:
"5dbfd4ab-04d2-47eb-89dd-c9c6a650cbb0"
Function Logs:
START RequestId: 5dbfd4ab-04d2-47eb-89dd-c9c6a650cbb0 Version: $LATEST
2019-06-18T18:35:35.125Z 5dbfd4ab-04d2-47eb-89dd-c9c6a650cbb0 INFO Layer Code : {}
END RequestId: 5dbfd4ab-04d2-47eb-89dd-c9c6a650cbb0
REPORT RequestId: 5dbfd4ab-04d2-47eb-89dd-c9c6a650cbb0 Duration: 133.56 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 26 MB
If you notice, when I am trying to print 'myutil' constant, it is printed as empty. That means, layer code is not injected during lambda execution.
INFO Layer Code : {}