We want to create an AWS layer zip file containing all the node_modules used by a particular AWS lambda function.
Stepwise, here’s how we can do it:
Add a script in the lambda function’s package.json. Let’s call it build:layer. It should do the following:
- Create
nodejsdirectory (mkdir nodejs) - Copy Lambda’s
package.jsontonodejs(cp package.json ./nodejs) - Change directory to
nodejs(cd nodejs) - Installing node_modules while omitting dev dependencies (
npm i --omit=dev) - Move out of
nodejs(cd ..) - Zip the contents of
nodejsinnodejs.zip(zip -r nodejs.zip nodejs) - Remove the temporary folder
nodejsand its contents (rm -rf nodejs)
Here’s the full script:
"build:layer": "mkdir nodejs && cp package.json ./nodejs && cd nodejs && npm i --omit=dev && cd .. && zip -r nodejs.zip nodejs && rm -rf nodejs"
Now, you can upload this generated nodejs.zip file as an AWS layer and use it in your lambda code.
See also
- Preventing Privilege Escalation in NoSQL & SQL: Secure Node.js Practices
- Node JS Mongo Client for Atlas Data API
- SignatureDoesNotMatch: The request signature we calculated does not match the signature you provided. Check your key and signing method.
- Exactly Same Query Behaving Differently in Mongo Client and Mongoose
- In Node JS HTML to PDF conversion, Populate Images From URLs
- Convert HTML to PDF in Nodejs
- JavaScript Rollbar: Unknown Unhandled Rejection Error Getting Reason From Event