Home

Awesome

serverless-aws-lambda

NPM

Description

AWS Lambda dev tool for Serverless. Allows Express synthax in handlers. Supports packaging, local invoking and offline Application Load Balancer and API Gateway lambda server mocking.

Supported Runtimes

Minimum requirements


Table of Contents

Installation

Quick start

npx degit github:inqnuam/serverless-aws-lambda/templates/simple my-project
cd my-project && yarn install
yarn start

Manual installation

Usual node module installation...

yarn add -D serverless-aws-lambda
# or
npm install -D serverless-aws-lambda

Then add the plugin to your serverless plugins list

service: myapp

frameworkVersion: "3"
configValidationMode: error

plugins:
  - serverless-aws-lambda

Usage

Start the local server

SLS_DEBUG="*" sls aws-lambda -s dev

During 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.


Invoke

Lambda execution lifecycles.

Succefull execution: lambda success Failed execution: lambda error

Events

Offline 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.default
    events:
      - alb:
          listenerArn: arn:aws:elasticloadbalancing:eu-west-3:170838072631:listener/app/myAlb/bf88e6ec8f3d91df/e653b73728d04626
          priority: 939
          conditions:
            path: "/paradise"
            method: GET

All 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:

Please note that invoking a lambda from sls CLI (sls invoke local -f myFunction) will not trigger the offline server. But will still make your handler ready to be invoked.

AWS Console Test button

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

Function URL is available with @url/ prefix. (must be enabled inside lambda declaration).
Example:

functions:
  myAwsomeLambda:
    handler: src/handlers/awsomeLambda.default
    url: true
http://localhost:3000/@url/myAwsomeLambda

Serverless Invoke local

Works out of box.
see options
Example:

serverless invoke local -f myAwsomeLambda

AWS SDK

Invoking with aws-sdk Lambda Client requires to set client endpoint to local server host.

Example:

import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";

const client = new LambdaClient({ region: "us-east-1", endpoint: "http://localhost:3000" });
const DryRun = "DryRun";
const Event = "Event";
const RequestResponse = "RequestResponse";

const cmd = new InvokeCommand({
  FunctionName: "myAwsomeLambda",
  InvocationType: RequestResponse,
  Payload: Buffer.from(JSON.stringify({ foo: "bar" })),
});

client
  .send(cmd)
  .then((data) => {
    data.Payload = new TextDecoder("utf-8").decode(data.Payload);
    console.log(data);
  })
  .catch((error) => {
    // 🥲
    console.log("error", error);
  });

AWS Lambda Response Stream

Stream responses are supported out of box through Function URL invoke or AWS SDK invoke.
See example:

functions:
  myAwsomeLambda:
    handler: src/handlers/awsomeLambda.default
    url: # required only for Function URL invoke
      invokeMode: RESPONSE_STREAM
// awsomeLambda.ts
import stream from "stream";
import { promisify } from "util";
const pipeline = promisify(stream.pipeline);
import { createReadStream } from "fs";

export const handler = 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.png
  const streamImage = createReadStream("BlackMarble_2016_928m_europe_labeled.png");

  await pipeline(streamImage, responseStream);
});

Example with AWS SDK:

import { LambdaClient, InvokeWithResponseStreamCommand } from "@aws-sdk/client-lambda";

const client = new LambdaClient({
  region: "eu-west-3",
  endpoint: "http://localhost:3000",
});

const cmd = new InvokeWithResponseStreamCommand({
  FunctionName: "myAwsomeLambda",
  InvocationType: "RequestResponse",
  Payload: Buffer.from(JSON.stringify({ hello: "world" })),
  ClientContext: Buffer.from(JSON.stringify({ anything: "some value" })).toString("base64"),
});

const data = await client.send(cmd);

for await (const x of data.EventStream) {
  if (x.PayloadChunk) {
    console.log(x.PayloadChunk.Payload);
  }
}

Environment variables

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.

If NODE_ENV is present it will be injected in both local mode, while deploying also during bundle process for optimized output.


Package

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).

- assets

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: true
  assets: true # default false

functions:
  myAwsomeLambda:
    description: inherits assets from top-level package
    handler: src/handlers/awsomeLambda.default

or by function:

functions:
  myAwsomeLambda:
    package:
      assets: false
    description: don't includes assets
    handler: src/handlers/awsomeLambda.default

include assets by file extension:

functions:
  myAwsomeLambda:
    package:
      assets: .css
    description: include only css files
    handler: src/handlers/awsomeLambda.default
functions:
  myAwsomeLambda:
    package:
      assets:
        - .css
        - .svg
    description: include only css and svg files
    handler: src/handlers/awsomeLambda.default

- preserveDir :

To preserve your project directories structure inside the archive set preserveDir globally or at function level.

package:
  individually: true
  preserveDir: true # default true

functions:
  myAwsomeLambda:
    description: directories preserved
    handler: src/handlers/awsomeLambda.default

  dummyLambda:
    package:
      preserveDir: false
    description: directories NOT preserved
    handler: src/handlers/dummyLambda.default

- files

include additional files or directories into the package.

functions:
  myAwsomeLambda:
    handler: src/handlers/awsomeLambda.default
    package:
      files:
        - ./resources/some/file.png
        - ./resources/anotherFile.pdf
        - ./images

By 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.default
    package:
      inheritFiles: false
      files:
        - ./resources/some/file.png
        - ./node_modules/my-module

files may be added to the archive with custom path:

functions:
  myAwsomeLambda:
    handler: src/handlers/awsomeLambda.default
    package:
      files:
        - { at: "./resources/some/file.png", as: "./documents/important.png" }

Adding files with a filter:

functions:
  myAwsomeLambda:
    handler: src/handlers/awsomeLambda.default
    package:
      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.default
    package:
      files:
        - { pattern: "images/*.png", dir: "./resources" }

Adding inline files:

functions:
  myAwsomeLambda:
    handler: src/handlers/awsomeLambda.default
    package:
      files:
        - { text: "Hello world", dir: "./documents/hello.txt" }

Deploy

Adding the param online: false will omit the deployement of your Lambda.

functions:
  myAwsomeLambda:
    handler: src/handlers/awsomeLambda.default
    online: false

To deploy a lambda by stage(s) set online's value to target stage(s)

functions:
  lambdaOnlyInDev:
    handler: src/handlers/awsomeLambda.default
    online: dev
functions:
  lambdaOnlyInDevAndTest:
    handler: src/handlers/awsomeLambda.default
    online:
      - dev
      - test

Extended properties


Advanced configuration:

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.default

See defineConfig for advanced configuration.


Plugins:


Benchmarks

Hardware and software:

Handler:

// src/handlers/visitor.js
let count = 0;
export const handler = async () => {
  count++;

  return {
    statusCode: 200,
    body: `Visit count ${count}`,
  };
};
functions:
  visitor:
    handler: src/handlers/visitor.handler
    events:
      - http: ANY /visitor
200 + 200 executionsTime (in seconds)Memory used (mb)CPU (core) usagelast invoke responsenote
serverless-aws-lambda <br/> cmd: serverless aws-lambdasequential: 0.644<br/> concurrent: 0.414idle: 125<br/> peak: 169idle: 0,1%<br/> peak: 15%Visit count 400
serverless-offline + serverless-esbuild <br/> cmd: serverless offline --reloadHandlersequential: 10.4<br/> concurrent: 2.8idle: 110<br/> peak: 3960idle: 0,1%<br/> peak: 537%Visit count 1most of concurrent invocations fail

Use Express syntax with your lambdas:

See docs.