Lambda Layer:
Used to share code between functions
Package below into a Lambda layer:
1. Set of reusable functions that can be used across Lambda's.
2. npm module that can be used across Lambda's.
Consider we have set of reusable functions like below in a file named reusable.js:
const getCountryCode = function(countryName) { let countryCodeMapping = { "India" : "IN", "United States of America" : "USA", "United Kingdom" : "UK" }; return countryCodeMapping[countryName]; }; module.exports = { getCountryCode }
Folder structure:
nodejs (FolderName) reusableCodeFiles.js package.json node_modules
How to zip (Windows):
Right click the nodejs folder -> 7-Zip -> Add to "nodejs.zip"
nodejs - foldername
nodejs.zip
How to use the Layer reusable code from Lambda:
const reusableCode = require('/opt/nodejs/reusable'); // reusable is the name of file (.ie. reusable.js) inside nodejs zip file exports.handler = async (event) => { console.log(reusableCode .getCountryCode("India")); //Logs IN };
Top comments (0)