lambda-local
Advanced tools
Comparing version 1.6.1 to 1.6.2
# ChangeLog | ||
## 1.6.2 (2019/04/30) | ||
* Documentation changes. No API changes | ||
## 1.6.1 (2019/04/15) | ||
@@ -4,0 +7,0 @@ * Update for Winston 3 |
{ | ||
"name": "lambda-local", | ||
"version": "1.6.1", | ||
"version": "1.6.2", | ||
"description": "Commandline tool to run Lambda functions on your local machine.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
156
README.md
# Lambda-local | ||
[![NPM](https://nodei.co/npm/lambda-local.png?compact=true)](https://nodei.co/npm/lambda-local/) | ||
[![Build Status](https://travis-ci.org/ashiina/lambda-local.svg?branch=develop)](https://travis-ci.org/ashiina/lambda-local) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/ashiina/lambda-local/badge.svg)](https://snyk.io/test/github/ashiina/lambda-local) | ||
Lambda-local lets you test Amazon Lambda functions on your local machine with sample event data. | ||
Lambda-local lets you test Amazon Lambda functions on your local machine, by providing a simplisitc API and command-line tool. | ||
The `context` of the Lambda function is already loaded so you do not have to worry about it. | ||
You can pass any `event` JSON object as you please. | ||
The calls are fully customizable, as you can pass any `event` (JSON) object to any `handler` function. | ||
@@ -37,19 +41,4 @@ ## Install | ||
## About: CLI | ||
## About: Definitions | ||
### Command | ||
* -l, --lambda-path <lambda index path> (required) Specify Lambda function file name. | ||
* -e, --event-path <event path> (required) Specify event data file name. | ||
* -h, --handler <handler name> (optional) Lambda function handler name. Default is "handler". | ||
* -t, --timeout <timeout> (optional) Seconds until lambda function timeout. Default is 3 seconds. | ||
* -r, --region <aws region> (optional) Sets the AWS region, defaults to us-east-1. | ||
* -P, --profile-path <aws profile name> (optional) Read the specified AWS credentials file. | ||
* -p, --profile <aws profile name> (optional) Use with **-P**: Read the AWS profile of the file. | ||
* -E, --environment <JSON {key:value}> (optional) Set extra environment variables for the lambda | ||
* --wait-empty-event-loop (optional) Sets callbackWaitsForEmptyEventLoop=True => will wait for an empty loop before returning. This is false by default because our implementation isn\'t perfect and only "emulates" it. | ||
* --envdestroy (optional) Destroy added environment on closing. Defaults to false | ||
* -v, --verboselevel <3/2/1/0>', (optional) Default 3. Level 2 dismiss handler() text, level 1 dismiss lambda-local text and level 0 dismiss also the result. | ||
* --envfile <path/to/env/file> (optional) Set extra environment variables from an env file | ||
* --inspect [[host:]port] (optional) Starts lambda-local using the NodeJS inspector (available in nodejs > 8.0.0) | ||
### Event data | ||
@@ -67,3 +56,3 @@ Event sample data are placed in `examples` folder - feel free to use the files in here, or create your own event data. | ||
### Context | ||
The `context` object has been directly extracted from the source visible when running an actual Lambda function on AWS. | ||
The `context` object has been sampled from what's visible when running an actual Lambda function on AWS, and the [available documentation](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html) | ||
They may change the internals of this object, and Lambda-local does not guarantee that this will always be up-to-date with the actual context object. | ||
@@ -73,5 +62,22 @@ | ||
Since the Amazon Lambda can load the AWS-SDK npm without installation, Lambda-local has also packaged AWS-SDK in its dependencies. | ||
If you want to use this, please use the "-p" option with the aws credentials file. More infos here: | ||
If you want to use this, please use the `-p` or `-P` options (or their API counterpart) with the aws credentials file. More infos here: | ||
http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-config-files | ||
## About: CLI | ||
### Available Arguments | ||
* `-l, --lambda-path <lambda index path>` (required) Specify Lambda function file name. | ||
* `-e, --event-path <event path>` (required) Specify event data file name. | ||
* `-h, --handler <handler name>` (optional) Lambda function handler name. Default is "handler". | ||
* `-t, --timeout <timeout>` (optional) Seconds until lambda function timeout. Default is 3 seconds. | ||
* `-r, --region <aws region>` (optional) Sets the AWS region, defaults to us-east-1. | ||
* `-P, --profile-path <aws profile name>` (optional) Read the specified AWS credentials file. | ||
* `-p, --profile <aws profile name>` (optional) Use with **-P**: Read the AWS profile of the file. | ||
* `-E, --environment <JSON {key:value}>` (optional) Set extra environment variables for the lambda | ||
* `--wait-empty-event-loop` (optional) Sets callbackWaitsForEmptyEventLoop=True => will wait for an empty loop before returning. This is false by default because our implementation isn\'t perfect and only "emulates" it. | ||
* `--envdestroy` (optional) Destroy added environment on closing. Defaults to false | ||
* `-v, --verboselevel <3/2/1/0>` (optional) Default 3. Level 2 dismiss handler() text, level 1 dismiss lambda-local text and level 0 dismiss also the result. | ||
* `--envfile <path/to/env/file>` (optional) Set extra environment variables from an env file | ||
* `--inspect [[host:]port]` (optional) Starts lambda-local using the NodeJS inspector (available in nodejs > 8.0.0) | ||
## About: API | ||
@@ -81,5 +87,10 @@ | ||
#### `execute(options)` | ||
API accessible with: | ||
```js | ||
const lambdaLocal = require("lambda-local"); | ||
``` | ||
Executes a lambda given the `options` object where keys are: | ||
#### `lambdaLocal.execute(options)` | ||
Executes a lambda given the `options` object, which is a dictionary where the keys may be: | ||
- `event` - requested event as a json object | ||
@@ -101,10 +112,13 @@ - `lambdaPath` - requested path to the lambda function | ||
#### `setLogger(logger)` | ||
#### `lambdaLocal.setLogger(logger)` | ||
#### `lambdaLocal.getLogger()` | ||
If you are using [winston](https://www.npmjs.com/package/winston), this pass a winston logger instead of the console. | ||
Those functions allow to access the [winston](https://www.npmjs.com/package/winston) logger used by lambda-local. | ||
## Example Usage for API | ||
## API examples | ||
#### Basic: In another node.js script | ||
A lot of examples, especially used among Mocha, may be found in the test files over: [here](https://github.com/ashiina/lambda-local/tree/develop/test) | ||
##### Basic usage: Using Promises | ||
```js | ||
@@ -123,15 +137,11 @@ const lambdaLocal = require('lambda-local'); | ||
profileName: 'default', | ||
timeoutMs: 3000, | ||
callback: function(err, data) { | ||
if (err) { | ||
console.log(err); | ||
} else { | ||
console.log(data); | ||
} | ||
}, | ||
clientContext: JSON.stringify({clientId: 'xxxx'}) | ||
timeoutMs: 3000 | ||
}).then(function(done) { | ||
console.log(done); | ||
}).catch(function(err) { | ||
console.log(err); | ||
}); | ||
``` | ||
##### Using Promises | ||
#### Basic usage: using callbacks | ||
@@ -151,70 +161,18 @@ ```js | ||
profileName: 'default', | ||
timeoutMs: 3000 | ||
}).then(function(done) { | ||
console.log(done); | ||
}).catch(function(err) { | ||
console.log(err); | ||
timeoutMs: 3000, | ||
callback: function(err, data) { | ||
if (err) { | ||
console.log(err); | ||
} else { | ||
console.log(data); | ||
} | ||
}, | ||
clientContext: JSON.stringify({clientId: 'xxxx'}) | ||
}); | ||
``` | ||
### Use lambda-local to Mock | ||
## Other links | ||
You can use Lambda local to mock your lambda then run it, using [MochaJS][1] and [SinonJS][2] | ||
- If you are willing to test an app based on the ASK-SDK, have a look at https://github.com/taimos/ask-sdk-test | ||
In this sample, we assume that you got a test function like this: | ||
```js | ||
/* | ||
* Lambda function used to test mocking. | ||
*/ | ||
exports.getData = function getData(){ | ||
return "WrongData"; | ||
} | ||
exports.handler = function(event, context) { | ||
context.done(null, exports.getData()); | ||
}; | ||
``` | ||
Then you will be able to use in your test.js mocha file, something like: | ||
```js | ||
//An empty event | ||
var jsonPayload = { | ||
} | ||
var done, err; | ||
before(function (cb) { | ||
var lambdalocal = require('lambda-local'); | ||
lambdalocal.setLogger(your_winston_logger); | ||
var lambdaFunc = require("path_to_test-function.js"); | ||
//For instance, this will replace the getData content | ||
sinon.mock(lambdaFunc).expects("getData").returns("MockedData"); | ||
//see on sinonjs page for more options | ||
lambdalocal.execute({ | ||
event: jsonPayload, | ||
lambdaFunc: lambdaFunc, //We are directly passing the lambda function | ||
lambdaHandler: "handler", | ||
callbackWaitsForEmptyEventLoop: true, | ||
timeoutMs: 3000, | ||
callback: function (_err, _done) { //We are storing the results and finishing the before() call => one lambda local call for multiple tests | ||
err = _err; | ||
done = _done; | ||
cb(); | ||
}, | ||
verboseLevel: 1 //only prints a JSON of the final result | ||
}); | ||
}); | ||
describe("Your first test", function () { | ||
it("should return mocked value", function () { | ||
assert.equal(done, "MockedData"); | ||
}); | ||
}); | ||
... Other tests | ||
``` | ||
[1]: https://mochajs.org/ | ||
[2]: http://sinonjs.org/ | ||
## Development | ||
@@ -221,0 +179,0 @@ |
78992
55
182