|
1 | | -Lambda-local |
2 | | -============ |
| 1 | +# Lambda-local |
3 | 2 |
|
4 | | -Lambda-local lets you test Amazon Lambda functions on your local machine with sample event data. |
5 | | -The `context` of the Lambda function is already loaded so you do not have to worry about it. |
6 | | -You can pass any `event` JSON object as you please. |
| 3 | +[](https://travis-ci.org/ashiina/lambda-local) |
7 | 4 |
|
| 5 | +Lambda-local lets you test Amazon Lambda functions on your local machine with sample event data. |
| 6 | +The `context` of the Lambda function is already loaded so you do not have to worry about it. |
| 7 | +You can pass any `event` JSON object as you please. |
| 8 | + |
| 9 | +## Install |
8 | 10 |
|
9 | | -Install |
10 | | ----- |
11 | 11 | ```bash |
12 | 12 | npm install -g lambda-local |
13 | 13 | ``` |
14 | 14 |
|
| 15 | +## Usage |
| 16 | + |
| 17 | +### As a command line tool |
15 | 18 |
|
16 | | -Usage |
17 | | ------ |
| 19 | +You can use Lambda-local as a command line tool. |
18 | 20 |
|
19 | 21 | ```bash |
20 | 22 | # Usage |
21 | | -lambda-local -l index.js -h handler -e event-samples/s3-put.js |
| 23 | +lambda-local -l index.js -h handler -e event-samples/s3-put.js |
22 | 24 | ``` |
23 | 25 |
|
24 | | -About |
25 | | ------ |
| 26 | +### In another node.js script |
| 27 | + |
| 28 | +You can also use Lambda local directly in a script. For instance, it is interesting in a [mocha][1] test suite in combination with [istanbull][2] in order to get test coverage. |
| 29 | + |
| 30 | +```js |
| 31 | +const lambdaLocal = require('lambda-local'); |
| 32 | + |
| 33 | +var jsonPayload = { |
| 34 | + 'key1': 'value1', |
| 35 | + 'key2': 'value2', |
| 36 | + 'key3': 'value3' |
| 37 | +} |
| 38 | + |
| 39 | +lambdaLocal.execute({ |
| 40 | + event: jsonPayload, |
| 41 | + lambdaPath: path.join(__dirname, 'path/to/index.js'), |
| 42 | + profilePath: '~/.aws/credentials', |
| 43 | + profileName: 'default', |
| 44 | + timeoutMs: 3000, |
| 45 | + callback: function(err, data) { |
| 46 | + if (err) { |
| 47 | + console.log(err); |
| 48 | + } else { |
| 49 | + console.log(data); |
| 50 | + } |
| 51 | + } |
| 52 | +}); |
| 53 | +``` |
| 54 | + |
| 55 | +## About |
| 56 | + |
26 | 57 | ### Command |
27 | | -* -l, --lambdapath [lambda file name] Specify Lambda function file name. |
28 | | -* -e, --eventpath [event data file name] Specify event data file name. |
29 | | -* -h, --handler [lambda-function handler name (optional)] Lambda function handler name. Default is "handler". |
30 | | -* -t, --timeout [timeout seconds (optional)] Seconds until lambda function timeout. Default is 3 seconds. |
31 | | -* -c, --callbackforce (optional) Force the function to stop after having called context.done/succeed/fail. |
32 | | -* -p, --profile [aws file path (optional)] Read the AWS profile to get the credidentials. |
| 58 | +* -l, --lambda-path <lambda index path> (required) Specify Lambda function file name. |
| 59 | +* -e, --event-path <event path> (required) Specify event data file name. |
| 60 | +* -h, --handler <handler name> (optional) Lambda function handler name. Default is "handler". |
| 61 | +* -t, --timeout <timeout> (optional) Seconds until lambda function timeout. Default is 3 seconds. |
| 62 | +* -n, --no-force-callback (optional) Force the function to stop after having called the handler function even if context.done/succeed/fail was not called. |
| 63 | +* -p, --profile <aws profile name> (optional) Read the AWS profile to get the credentials from file name. |
| 64 | +* -p, --profile-path <aws profile name> (optional) Read the specified AWS credentials file. |
33 | 65 |
|
34 | 66 | ### Event data |
35 | | -Event sample data are placed in `event-samples` folder - feel free to use the files in here, or create your own event data. |
36 | | -Event data are just JSON objects exported: |
| 67 | +Event sample data are placed in `event-samples` folder - feel free to use the files in here, or create your own event data. |
| 68 | +Event data are just JSON objects exported: |
37 | 69 |
|
38 | 70 | ```js |
39 | | -# Sample event data |
| 71 | +// Sample event data |
40 | 72 | module.exports = { |
41 | 73 | foo: "bar" |
42 | 74 | }; |
43 | 75 | ``` |
44 | 76 |
|
45 | 77 | ### Context |
46 | | -The `context` object has been directly extracted from the source visible when running an actual Lambda function on AWS. |
47 | | -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. |
| 78 | +The `context` object has been directly extracted from the source visible when running an actual Lambda function on AWS. |
| 79 | +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. |
48 | 80 |
|
49 | 81 | ### AWS-SDK |
50 | 82 | Since the Amazon Lambda can load the AWS-SDK npm without installation, Lambda-local has also packaged AWS-SDK in its dependencies. |
51 | 83 | If you want to use this, please use the "-p" option with the aws credentials file. More infos here: |
52 | 84 | http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-config-files |
53 | 85 |
|
54 | | -License |
55 | | ----------- |
| 86 | +## API |
| 87 | + |
| 88 | +### LambdLocal |
| 89 | + |
| 90 | +#### `execute(options)` |
| 91 | + |
| 92 | +Executes a lambda given the `options` object where keys are: |
| 93 | +- `event` - requested event as a json object, |
| 94 | +- `lambdaPath` - requested path to the lambda function, |
| 95 | +- `profilePath` - optional path to your AWS credentials file |
| 96 | +- `profileName` - optional aws profile name |
| 97 | +- `lambdaHandler` - optional handler name, default to `handler` |
| 98 | +- `region` - optional AWS region, default to `us-east-1` |
| 99 | +- `callbackWaitsForEmptyEventLoop` - optional, default to `true` which forces the function to stop after having called the handler function even if context.done/succeed/fail was not called. |
| 100 | +- `timeoutMs` - optional timeout, default to 3000 ms |
| 101 | +- `mute` - optional, allows to mute console.log calls in the lambda function, default false |
| 102 | +- `callback` - optional lambda third parameter [callback][3] |
| 103 | + |
| 104 | +## License |
| 105 | + |
56 | 106 | This library is released under the MIT license. |
57 | 107 |
|
| 108 | +[1]: https://mochajs.org/ |
| 109 | +[2]: http://gotwarlost.github.io/istanbul/ |
| 110 | +[3]: http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html |
58 | 111 |
|
0 commit comments