38
loading...
This website collects cookies to deliver better user experience
There are only a few public npm packages which are available natively in lambda environment. For all other npm packages, you will have to create common lambda layers that can be used in your whole AWS environment.
Custom node modules which you have made for your system specific requirements.
npm install { package-name }
.NOTE: Your custom node module must have above 3 items & you can link that custom node module to your system node environment through command npm link
. You can also install a linked custom module in any other node module by running command npm link { custom-module-name }
.
npm init -y
. Name parameters for your project as you like. A new file named "package.json" will be created.npm link { custom-module-name }
. You will find a folder named node_modules.We have to make a build of node_modules folder. Nodejs environment on AWS lambda extracts node modules from a folder named nodejs which furthure contains node-modules folder having all modules.
Paste following command in Scripts field of Package.json.
"scripts": {
"build": "npm install && mkdir -p nodejs && cp -r node_modules nodejs/ && zip -r {file-name}.zip nodejs"
}
Give relative name to your build zip file, save it & run command npm run build
. You will see a zip file which is basically build of your node modules.
Now, go to layers section in your AWS Lambda console. Create a new layer, upload your build right there or attach it through a s3 link. (s3 links are recommended, if build file is greater than 13MB).
Your lambda layer is created. Code in that layer is now COMMON to whole AWS account and resides at one place. Attach the layer to any lambda function in your account. That lambda function will be able to access the code. Thats it😀.
For cross account sharing of lambda layers, visit official docs here.