Lambda-local lets you test Amazon Lambda functions on your local machine, by providing a simplistic API and command-line tool.
The context
of the Lambda function is already loaded so you do not have to worry about it.
The calls are fully customizable, as you can pass any event
(JSON) object to any handler
function.
npm install -g lambda-local
You can use Lambda-local as a command line tool.
# Simple usage
lambda-local -l index.js -h handler -e examples/s3-put.js
# Input environment variables
lambda-local -l index.js -h handler -e examples/s3-put.js -E '{"key":"value","key2":"value2"}'
You can also use Lambda local directly in a script. For instance, it is interesting in a MochaJS test suite in order to get test coverage.
See API for more infos
Event sample data are placed in examples
folder - feel free to use the files in here, or create your own event data.
Event data are just JSON objects exported:
// Sample event data
module.exports = {
foo: "bar"
};
The context
object has been sampled from what's visible when running an actual Lambda function on AWS, and the available documentation
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.
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
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
-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)
API accessible with:
const lambdaLocal = require("lambda-local");
Executes a lambda given the options
object, which is a dictionary where the keys may be:
event
- requested event as a json objectlambdaPath
- requested path to the lambda functionlambdaFunc
- pass the lambda function. You cannot use it at the same time as lambdaPathprofilePath
- optional, path to your AWS credentials fileprofileName
- optional, aws profile name. Must be used withlambdaHandler
- optional handler name, default tohandler
region
- optional, AWS region, default tous-east-1
callbackWaitsForEmptyEventLoop
- optional, default tofalse
. Setting it to True will wait for an empty loop before returning.timeoutMs
- optional, timeout, default to 3000 msenvironment
- optional, extra environment variables for the lambdaenvfile
- optional, load an environment file before bootingenvdestroy
- optional, destroy added environment on closing, default to falseverboseLevel
- optional, default 3. Level 2 dismiss handler() text, level 1 dismiss lambda-local text and level 0 dismiss also the result.callback
- optional, lambda third parameter callback. When left out a Promise is returnedclientContext
- optional, used to populated clientContext property of lambda second parameter (context)
Those functions allow to access the winston logger used by lambda-local.
A lot of examples, especially used among Mocha, may be found in the test files over: here
const lambdaLocal = require('lambda-local');
var jsonPayload = {
'key': 1,
'another_key': "Some text"
}
lambdaLocal.execute({
event: jsonPayload,
lambdaPath: path.join(__dirname, 'path_to_index.js'),
profilePath: '~/.aws/credentials',
profileName: 'default',
timeoutMs: 3000
}).then(function(done) {
console.log(done);
}).catch(function(err) {
console.log(err);
});
const lambdaLocal = require('lambda-local');
var jsonPayload = {
'key': 1,
'another_key': "Some text"
}
lambdaLocal.execute({
event: jsonPayload,
lambdaPath: path.join(__dirname, 'path_to_index.js'),
profilePath: '~/.aws/credentials',
profileName: 'default',
timeoutMs: 3000,
callback: function(err, data) {
if (err) {
console.log(err);
} else {
console.log(data);
}
},
clientContext: JSON.stringify({clientId: 'xxxx'})
});
- If you are willing to test an app based on the ASK-SDK, have a look at https://github.com/taimos/ask-sdk-test
- Run
make
to install npm modules. (Required to develop & test lambda-local) - Run
make test
to execute the mocha test. - Run
make clean
to reset the repository.
This library is released under the MIT license.