How to test aws lambda functions locally

2019-02-16 06:13发布

问题:

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?

回答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.



回答2:

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

  run-lambda@1.6.8 - 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



回答3:

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:

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

sls proxy

You can then start sending requests to your service locally:

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



回答4:

As 1.4 is not yet released...

Last week i needed exactly the same thing. Therefore i created a small mock server that can be run locally: https://github.com/martinlindenberg/JawsLocalServer

  • All headers and parameters were copied to the event-object that is passed to the handler.
  • The context object is a simple object that forwards the data to the response object


回答5:

This may be too late.. But now you can try Serverless-Offline (https://www.npmjs.com/package/serverless-offline) works like a charm

Also this is the github page for it (https://github.com/dherault/serverless-offline)



回答6:

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


回答7:

As of the date of this post, you can run functions locally by doing sls function run [name-of-function]. Any json used in your function's event.json will be passed into your function.

For testing your endpoints, you can also use Serverless Offline which is a fork of the serverless-serve project.