This repo is derived from AnomalyInnovations/serverless-nodejs-starter
It provides the set up to create an AWS lambda layer with serverless using babel 7
- jest, eslint and prettier configured
Layers can then be used in any of your other lambdas
- e.g. a layer could be common code you use in many lambdas
A template with support for these layers: https://github.com/robcronin/serverless-lambda-with-optional-layer-template
For an example repo of a lambda with a layer created with these templates see here
sls install --url https://github.com/robcronin/serverless-layer-template --name [NAME_OF_YOUR_LAYER]
or
git clone https://github.com/robcronin/serverless-layer-template
cd [NAME_OF_YOUR_LAYER]yarn./setUp.sh- answer the prompts
Edit the code in the src directory as required
By default it is a simple sum function
yarn deployor./deploy.sh- This will transpile your code with babel, create the serverless package and deploy it
- The output will give you the
arnyou will need in your lambda functions
- In your actual lambda elsewhere add this
arnto your function config:
functions:
hello:
handler: handler.hello
events:
- http:
path: hello
method: get
layers:
- [YOUR_ARN_HERE]- At runtime the code in your layer will now be located at
/opt - If you had deployed the default sum function in this repo, you could then import it with:
import { sum } from '/opt/[LAYER_NAME]';- As you are not creating endpoints you can't use the useful
sls invokeorsls offline start - If you wish to use your layer locally in another lambda, one solution is to copy your code to your local
/opt./local.shwill do this for you (requiressudopermission)- While developing the code in your layer you can use
yarn watchwhich will runlocal.shafter any file changes (requiressudopermission)
- Note: if you use webpack in your main function you may run into issues with this, use https://github.com/robcronin/serverless-lambda-with-optional-layer-template for a working template
yarn test will run:
yarn lintyarn jest