serverless-multi-lambda-layers v1.0.0
PinataFarms multi-lambda-layers Serverless Plugin
This plugin allows managing node.js lambda dependencies separately and putting each of them into a separate lambda layer.
Objective - be able to easily keep each lambda dependencies in a separate layer.
Why?
There are two main points:
- Keeping lambdas lean (attaching small layers only with needed dependencies)
- Reducing probability of reaching the hard limit of 250MB lambda size (unzipped layers + lambda code)
Thanks to the above-mentioned points we are able to live-patch lambdas in production via the AWS console as lambda code sizes stay below the live edit limit.
How it works
Every time when I mention lambdas directory you should keep in mind that this directory could be overwritten via the plugin configuration (see section below).
This plugin expects you to maintain a similar directory structure for your Serverless service:
$ tree
├── lambdas
│ ├── lambdaA
│ │ ├── handler.js
│ │ ├── package-lock.json
│ │ └── package.json
│ └── lambdaB
│ ├── handler.js
│ ├── package-lock.json
│ └── package.json
├── package-lock.json
├── package.json
└── serverless.yml
The main service package.json
file has only Serverless Framework dependencies (e.g. plugins) and npm scripts in it.
All lambdas functions are put into lambdas directory, each inside their own directory with their own package.json
files where their dependencies are defined.
This plugin has 2 hooks:
before:package:createDeploymentArtifacts
- runs before creation of deployment artifacts (zip archives)after:package:createDeploymentArtifacts
- runs after creation of deployment artifacts (zip archives)
before:package:createDeploymentArtifacts
The plugin goes over functions
block in your serverless.yml
file, sees lambdas with handlers pointing
at lambdas directory (e.g. handler: lambdas/lambdaA/handler.hello
) and processes each of them:
- removes
node_modules
directory if present - installs dependencies (
npm i --production
) - creates
layer/nodejs
directory structure - moves
node_modules
directory into thatlayer/nodejs
directory
after:package:createDeploymentArtifacts
The plugin goes over the same lambdas as in the previous hook and processes each of them:
- moves
node_modules
directory fromlayer/nodejs
directory to a lambda directory - removes
layer/nodejs
directory
Installation
$ npm install @pinatafarms/serverless-multi-lambda-layers
Configuration
- Add
@pinatafarms/serverless-multi-lambda-layers
to plugins in yourserverless.yml
fileplugins: - "@pinatafarms/serverless-multi-lambda-layers"
- By default, all your lambdas should be in lambdas directory.
You could change this directory by adding such configuration to your
serverless.yml
filecustom: serverless-multi-lambda-layers: lambdaDir: my-lambda-dir
Example
Directory structure
$ tree
├── my-lambda-dir
│ ├── lambdaA
│ │ ├── handler.js
│ │ ├── package-lock.json
│ │ └── package.json
│ └── lambdaB
│ ├── handler.js
│ ├── package-lock.json
│ └── package.json
├── package-lock.json
├── package.json
└── serverless.yml
serverless.yml
service: example-service
frameworkVersion: '2'
plugins:
- serverless-multi-lambda-layers
custom:
serverless-multi-lambda-layers:
lambdaDir: my-lambda-dir
provider:
name: aws
runtime: nodejs12.x
stage: ${opt:stage, 'qa'}
region: ${opt:region, 'us-east-1'}
package:
individually: true
functions:
lambdaA:
handler: my-lambda-dir/lambdaA/handler.hello
package:
exclude:
- '**/*'
include:
- my-lambda-dir/lambdaA/handler.js
lambdaB:
handler: my-lambda-dir/lambdaB/handler.hello
package:
exclude:
- '**/*'
include:
- my-lambda-dir/lambdaB/handler.js
5 years ago