├── .gitignore ├── README.md ├── handler.js ├── layer └── nodejs │ └── package-lock.json └── serverless.yml /.gitignore: -------------------------------------------------------------------------------- 1 | # package directories 2 | node_modules 3 | jspm_packages 4 | 5 | # Serverless directories 6 | .serverless -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Lodash and Moment Lambda layer 2 | 3 | example of How To Add NodeJs Library Dependencies in a AWS Lambda Layer With Serverless Framework 4 | https://medium.com/neami-apps/how-to-add-nodejs-library-dependencies-in-a-aws-lambda-layer-with-serverless-framework-d774cb867197 5 | 6 | ## How to setup 7 | 8 | ``` 9 | cd layer/nodejs 10 | npm install --save lodash moment 11 | ``` 12 | 13 | Go back to root directory 14 | 15 | ``` 16 | sls deploy 17 | ``` 18 | 19 | ## How to run 20 | 21 | ``` 22 | sls invoke --function hello --data=2 23 | ``` 24 | -------------------------------------------------------------------------------- /handler.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const _ = require("lodash"); 4 | const moment = require("moment"); 5 | 6 | module.exports.hello = async (event, context, callback) => { 7 | const curried = _.curry(calculateDate); 8 | callback(null, curried(moment().format())(event)); 9 | }; 10 | 11 | const calculateDate = function(datetime, days) { 12 | return moment() 13 | .add(days, "days") 14 | .format("Do MMMM YYYY, h:mm:ss a"); 15 | }; 16 | -------------------------------------------------------------------------------- /layer/nodejs/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "requires": true, 3 | "lockfileVersion": 1, 4 | "dependencies": { 5 | "lodash": { 6 | "version": "4.17.11", 7 | "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", 8 | "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" 9 | }, 10 | "moment": { 11 | "version": "2.22.2", 12 | "resolved": "https://registry.npmjs.org/moment/-/moment-2.22.2.tgz", 13 | "integrity": "sha1-PCV/mDn8DpP/UxSWMiOeuQeD/2Y=" 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | service: myTestLayer # NOTE: update this with your service name 2 | 3 | provider: 4 | name: aws 5 | runtime: nodejs8.10 6 | region: ap-southeast-2 7 | 8 | 9 | functions: 10 | hello: 11 | handler: handler.hello 12 | layers: 13 | # Ref name is generated by TitleCasing the layer name & appending LambdaLayer 14 | - {Ref: CommonLibsLambdaLayer} 15 | 16 | layers: 17 | commonLibs: 18 | path: layer 19 | compatibleRuntimes: 20 | - nodejs8.10 21 | --------------------------------------------------------------------------------