AWS Lambda dev tool for Serverless. Supports packaging, local invoking with Application Load Balancer and API Gateway, S3, SQS, SNS, DynamoStream server mocking.
- Plug & Play (easy to install, configure and use)
- Highly customizable
- Functions are bundled by esbuild
- Local server uses NodeJS
httpmodule - Packaging is made by node-archiver
- NodeJS
- Python
- Ruby
- Node v18.20.4+
- Serverless 2.0.0+ < 4.0
npx degit github:inqnuam/serverless-aws-lambda/templates/simple my-project
cd my-project && yarn install
yarn startUsual node module installation...
yarn add -D serverless-aws-lambda
# or
npm install -D serverless-aws-lambdaThen add the plugin to your serverless plugins list
service: myappframeworkVersion: "3"configValidationMode: errorplugins:
- serverless-aws-lambdaStart the local server
SLS_DEBUG="*" sls aws-lambda -s devDuring development the env variable SLS_DEBUG="*" is strongly recommanded as it will print a bunch of useful information.
It is also possible to set server port from the CLI with --port or -p.
This will overwrite serverless.yml custom > serverless-aws-lambda > port value if it is set.
For more options see advanced configuration.
Succefull execution:
Failed execution:

Local server supports Application Load Balancer, API Gateway and Function URL endpoints out of box.
See plugins for more triggers (SNS, SQS, etc.).
Appropriate event object is sent to the handler based on your lambda declaration.
functions:
myAwsomeLambda:
handler: src/handlers/awsomeLambda.defaultevents:
- alb:
listenerArn: arn:aws:elasticloadbalancing:eu-west-3:170838072631:listener/app/myAlb/bf88e6ec8f3d91df/e653b73728d04626priority: 939conditions:
path: "/paradise"method: GETAll available local endpoints will be printed to the console when SLS_DEBUG="*" is set.
myAwsomeLambda is available at http://localhost:PORT/paradise
However if your declare both alb and http or httpApi inside a single lambda events with the same path you have to specify desired server by setting alb or apg inside your request's:
- header with
X-Mock-Type. - or in query string with
x_mock_type.
Please note that invoking a lambda from sls CLI (sls invoke local -f myFunction) will not trigger the local server. But will still make your handler ready to be invoked.
To invoke your Lambda like with AWS Console's Test button, prefix your Lambda name by @invoke/.
Example:
http://localhost:3000/@invoke/myAwsomeLambda
Function URL is available with @url/ prefix. (must be enabled inside lambda declaration).
Example:
functions:
myAwsomeLambda:
handler: src/handlers/awsomeLambda.defaulturl: truehttp://localhost:3000/@url/myAwsomeLambda
Works out of box.
see options
Example:
serverless invoke local -f myAwsomeLambda
Invoking with aws-sdk Lambda Client requires to set client endpoint to local server host.
Example:
import{LambdaClient,InvokeCommand}from"@aws-sdk/client-lambda";constclient=newLambdaClient({region: "us-east-1",endpoint: "http://localhost:3000"});constDryRun="DryRun";constEvent="Event";constRequestResponse="RequestResponse";constcmd=newInvokeCommand({FunctionName: "myAwsomeLambda",InvocationType: RequestResponse,Payload: Buffer.from(JSON.stringify({foo: "bar"})),});client.send(cmd).then((data)=>{data.Payload=newTextDecoder("utf-8").decode(data.Payload);console.log(data);}).catch((error)=>{// 🥲console.log("error",error);});Stream responses are supported out of box through Function URL invoke or AWS SDK invoke.
See example:
functions:
myAwsomeLambda:
handler: src/handlers/awsomeLambda.defaulturl: # required only for Function URL invokeinvokeMode: RESPONSE_STREAM// awsomeLambda.tsimportstreamfrom"stream";import{promisify}from"util";constpipeline=promisify(stream.pipeline);import{createReadStream}from"fs";exportconsthandler=awslambda.streamifyResponse(async(event,responseStream,context)=>{responseStream.setContentType("image/png");// https://svs.gsfc.nasa.gov/vis/a030000/a030800/a030877/frames/5760x3240_16x9_01p/BlackMarble_2016_928m_europe_labeled.pngconststreamImage=createReadStream("BlackMarble_2016_928m_europe_labeled.png");awaitpipeline(streamImage,responseStream);});Example with AWS SDK:
import{LambdaClient,InvokeWithResponseStreamCommand}from"@aws-sdk/client-lambda";constclient=newLambdaClient({region: "eu-west-3",endpoint: "http://localhost:3000",});constcmd=newInvokeWithResponseStreamCommand({FunctionName: "myAwsomeLambda",InvocationType: "RequestResponse",Payload: Buffer.from(JSON.stringify({hello: "world"})),ClientContext: Buffer.from(JSON.stringify({anything: "some value"})).toString("base64"),});constdata=awaitclient.send(cmd);forawait(constxofdata.EventStream){if(x.PayloadChunk){console.log(x.PayloadChunk.Payload);}}Lambdas are executed in worker threads. *Only variables declared in your serverless.yml are injected into process.env.
*In local mode following env variables are set for sls invoke, serverless-offline and AWS SAM compatibility.
- IS_LOCAL
- IS_OFFLINE
- AWS_SAM_LOCAL
If NODE_ENV is present it will be injected in both local mode, while deploying also during bundle process for optimized output.
serverless-aws-lambda bundles every (nodejs) handler separetly (with esbuild) and creates the artifact zip archive.
Archive will include bundeled handler and sourcemap (if enabled in esbuild).
By default bundle produced assets (css, png, svg etc.) are excluded.
To include all assets set assets to true.
For all functions set it at top-level package:
package:
individually: trueassets: true # default falsefunctions:
myAwsomeLambda:
description: inherits assets from top-level packagehandler: src/handlers/awsomeLambda.defaultor by function:
functions:
myAwsomeLambda:
package:
assets: falsedescription: don't includes assetshandler: src/handlers/awsomeLambda.defaultinclude assets by file extension:
functions:
myAwsomeLambda:
package:
assets: .cssdescription: include only css fileshandler: src/handlers/awsomeLambda.defaultfunctions:
myAwsomeLambda:
package:
assets:
- .css
- .svgdescription: include only css and svg fileshandler: src/handlers/awsomeLambda.defaultTo preserve your project directories structure inside the archive set preserveDir globally or at function level.
package:
individually: truepreserveDir: true # default truefunctions:
myAwsomeLambda:
description: directories preservedhandler: src/handlers/awsomeLambda.defaultdummyLambda:
package:
preserveDir: falsedescription: directories NOT preservedhandler: src/handlers/dummyLambda.defaultinclude additional files or directories into the package.
functions:
myAwsomeLambda:
handler: src/handlers/awsomeLambda.defaultpackage:
files:
- ./resources/some/file.png
- ./resources/anotherFile.pdf
- ./imagesBy default files are inherited from top level package's files.
This can be disabled with inheritFiles at function level.
functions:
myAwsomeLambda:
handler: src/handlers/awsomeLambda.defaultpackage:
inheritFiles: falsefiles:
- ./resources/some/file.png
- ./node_modules/my-modulefiles may be added to the archive with custom path:
functions:
myAwsomeLambda:
handler: src/handlers/awsomeLambda.defaultpackage:
files:
- { at: "./resources/some/file.png", as: "./documents/important.png" }Adding files with a filter:
functions:
myAwsomeLambda:
handler: src/handlers/awsomeLambda.defaultpackage:
files:
- { pattern: "./resources/some/*.png" }If you need to preserve pattern's directories structure inside the archive but search for files in another directory set dir value.
This will search for all .png files inside ./resources/images but only images directory will be created inside the archive.
functions:
myAwsomeLambda:
handler: src/handlers/awsomeLambda.defaultpackage:
files:
- { pattern: "images/*.png", dir: "./resources" }Adding inline files:
functions:
myAwsomeLambda:
handler: src/handlers/awsomeLambda.defaultpackage:
files:
- { text: "Hello world", dir: "./documents/hello.txt" }Adding the param online: false will omit the deployement of your Lambda.
functions:
myAwsomeLambda:
handler: src/handlers/awsomeLambda.defaultonline: falseTo deploy a lambda by stage(s) set online's value to target stage(s)
functions:
lambdaOnlyInDev:
handler: src/handlers/awsomeLambda.defaultonline: devfunctions:
lambdaOnlyInDevAndTest:
handler: src/handlers/awsomeLambda.defaultonline:
- dev
- testvirtualEnvs
a key-value object which will only be available inside defineConfig.
by default virtualEnvs are inherited from custom > virtualEnvs if exists.
To have more control over the plugin you can passe a config file via configPath param in plugin options:
custom:
serverless-aws-lambda:
configPath: ./config.defaultSee defineConfig for advanced configuration.
Hardware and software:
- iMac Pro 2017 (10 cors, 32Gb RAM)
- macOS Ventura (13.2.1)
- NodeJS v18.16.0
- Serverless 3.32.2
- serverless-aws-lambda 4.5.9
- serverless-offline 12.0.4
- serverless-esbuild 1.45.1
Handler:
// src/handlers/visitor.jsletcount=0;exportconsthandler=async()=>{count++;return{statusCode: 200,body: `Visit count ${count}`,};};functions:
visitor:
handler: src/handlers/visitor.handlerevents:
- http: ANY /visitor| 200 + 200 executions | Time (in seconds) | Memory used (mb) | CPU (core) usage | last invoke response | note |
|---|---|---|---|---|---|
| serverless-aws-lambda cmd: serverless aws-lambda | sequential: 0.644 concurrent: 0.414 | idle: 125 peak: 169 | idle: 0,1% peak: 15% | Visit count 400 | |
| serverless-offline + serverless-esbuild cmd: serverless offline --reloadHandler | sequential: 10.4 concurrent: 2.8 | idle: 110 peak: 3960 | idle: 0,1% peak: 537% | Visit count 1 | most of concurrent invocations fail |
