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 deploy
or./deploy.sh
- This will transpile your code with babel, create the serverless package and deploy it
- The output will give you the
arn
you will need in your lambda functions
- In your actual lambda elsewhere add this
arn
to 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 invoke
orsls 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.sh
will do this for you (requiressudo
permission)- While developing the code in your layer you can use
yarn watch
which will runlocal.sh
after any file changes (requiressudo
permission)
- 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 lint
yarn jest