Exciting release!Introducing "safe npm". Learn more →
Socket
Log inDemoInstall

serverless-aws-lambda

Package Overview
Dependencies
4
Maintainers
1
Versions
58
Issues
File Explorer

Advanced tools

serverless-aws-lambda

AWS Application Load Balancer and API Gateway - Lambda dev tool for Serverless. Allows Express synthax in handlers. Supports packaging, local invoking and offline ALB, APG, S3, SNS, SQS, DynamoDB Stream server mocking.

    4.4.2latest
    GitHub

Version published
Maintainers
1
Weekly downloads
302
increased by63.24%

Weekly downloads

Readme

Source

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.

  • Plug & Play (easy to install, configure and use)
  • Highly customizable
  • Functions are bundled by esbuild
  • Offline server uses NodeJS http module
  • Packaging is made by node-archiver

Installation

yarn add -D serverless-aws-lambda # or npm install -D serverless-aws-lambda service: myapp frameworkVersion: "3" configValidationMode: error plugins: - serverless-aws-lambda

Usage

Start the offline server

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

It is also possible to passe port from the CLI with --port or -p.

This will overwrite serverless.yml custom > serverless-aws-lambda values if they are set.

Invoke

Offline server supports Application Load Balancer and API Gateway endponts.
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

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 offline server. But you are still able to inject any event with -d 'someData' sls CLI option.

You can also invoke your Lambdas with a custom event object by making a POST request to:
http://localhost:3000/@invoke/myAwsomeLambda
for aws-sdk Lambda client compatibility it is also possible to request to:
http://localhost:3000/2015-03-31/functions/myAwsomeLambda/invocations

Example with with aws-sdk Lambda Client:

const { LambdaClient, InvokeCommand } = require("@aws-sdk/client-lambda"); const client = new LambdaClient({ region: "PARADISE", 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); });

Environment variable

Lambdas are executed in worker threads. Only variables declared in your serverless.yml are injected into process.env except IS_LOCAL, LOCAL_PORT and NODE_ENV.


Extended properties

  • online
    Adding the param online: false will omit the deployement of your Lambda.
functions: myAwsomeLambda: handler: src/handlers/awsomeLambda.default online: false
  • files
    include additional files into the package.
functions: myAwsomeLambda: handler: src/handlers/awsomeLambda.default files: - ./resources/some/file.png - ./resources/anotherFile.pdf
  • virtualEnvs
    on key-value object which will only be available inside defineConfig.
    by default virtualEnvs are inherited from custom > virtualEnvs if exists.

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: port: 3000 configPath: ./config.default

See defineConfig for advanced configuration.


Use Express syntax with your lambdas:

See docs.


Plugins:

Keywords

FAQs

Last updated on 26 Mar 2023

Did you know?

Socket installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install Socket
Socket
support@socket.devSocket SOC 2 Logo

Product

  • Package Issues
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc