How to test aws lambda functions locally
Question:
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?
Answers:
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.
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
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
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.
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.
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)
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
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
Python
An example lambda_function.py
:
def lambda_handler(*args, **kwargs):
return 'hello'
Example invocation of a local lambda function:
pip install python-lambda-local
echo '{}' >> event.json
python-lambda-local -t 15 -f lambda_handler lambda_function.py event.json
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?
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.
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
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
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.
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.
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)
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
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
Python
An example lambda_function.py
:
def lambda_handler(*args, **kwargs):
return 'hello'
Example invocation of a local lambda function:
pip install python-lambda-local
echo '{}' >> event.json
python-lambda-local -t 15 -f lambda_handler lambda_function.py event.json