How to test aws lambda functions locally

17,489

Solution 1

It doesn't look like there's way to do this right now, but version 1.4.0 is about to be released and, among other things, it should include a new command "jaws serve" which should address your problem.

Here's the PR: https://github.com/jaws-framework/JAWS/pull/269

UPDATE: you can now use the new serverless-serve plugin for this.

UPDATE 2: serverless-serve hasn't been updated in a while, it looks like serverless-offline is a much better option now to emulate Lambda functions.

Solution 2

You can now use lambda-local.

Install it like this:

sudo -H npm install -g lambda-local

Add your parameters as a JSON object to a file, in this example event.json, and call the index.js file like this:

lambda-local -l index.js -h handler -e event.json

Solution 3

I'm not sure if this question is still relevant or not, but I'm using DEEP Framework to test the code locally and/or deploy it on AWS Lambda. Check this out:

npm install deepify -g

deepify run-lambda --help

  [email protected] - Run Lambda function locally 

  Usage example: deepify run-lambda path/to/the/lambda -e='{"Name":"John Doe"}' 

  Arguments:  
    path: The path to the Lambda (directory of handler itself) 

  Options:  
    --event|-e: JSON string used as the Lambda payload 
    --skip-frontend-build|-f: Skip picking up _build path from the microservices Frontend 
    --db-server|-l: Local DynamoDB server implementation (ex. LocalDynamo, Dynalite) 
    --version|-v: Prints command version 
    --help|-h: Prints command help 

Disclosure: I am one of the contributors to this framework

Solution 4

The serverless framework now provide a way to invoke functions locally

With that, you can create queries in json files like

{
  "body": "{\"hello\":\"world\"}",
  "pathParameters": {
    "id": "foo"
  },
  "queryStringParameters": {
    "bar": "42"
  }
  "requestContext": {
    "identity": {
      "cognitoIdentityId": "cognito-id"
    }
  }
}

If your function is described in serverless.yml file, you can then invoke it locally with

serverless invoke local --function yourFunction --path path/to/your/mock.json

Solution 5

The Bespoken sevrerless plugin makes your local Lambdas externally accessible. It is very useful both for local testing with Postman as well as for Webhook-based services (like Alexa, Slack, Twilio, etc.).

The architecture is shown here: enter image description here

To use it, you just install the plugin, then run:

sls proxy

You can then start sending requests to your service locally:

enter image description here

We think it is a very useful tool for testing with serverless.

Share:
17,489

Related videos on Youtube

Supun Induwara
Author by

Supun Induwara

Updated on June 24, 2022

Comments

  • Supun Induwara
    Supun Induwara almost 2 years

    I have a mobile application backend developed with node.js express. I tried it to deploy it as lambda service. For that I created a Serverless Framework project (https://github.com/serverless). Previously I tested mobile app locally by starting express backend application. Now I can't find a method to test my mobile app locally without local backend. jaws run command only run once I called it.

    Is there any method to start the lambda function as a web service? Or is there any alternative to Serverless Framework?

  • tedder42
    tedder42 about 8 years
    can't get this to work :/ hangs after "starting lambda".
  • Supun Induwara
    Supun Induwara over 6 years
    Thanks, serverless can be used not only for offline test but it also helps to deploy and other lots of stuff.
  • Cukic0d
    Cukic0d over 4 years
    Doesn't support everything but at least that's very light
  • Thilina Koggalage
    Thilina Koggalage over 4 years
    This is what I was looking for. Thanks!