How to test aws lambda functions locally

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

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.


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


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