Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ node-lambda deploy

#### setup

Initializes the `event.json`, `.env` files, and `deploy.env` files. `event.json` is where you mock your event. `.env.` is where you place your deployment configuration. `deploy.env` has the same format as `.env`, but is used for holding any environment/config variables that you need to be deployed with your code to Lambda but you don't want in version control (e.g. DB connection info).
Initializes the `event.json`, `context.json`, `.env` files, and `deploy.env` files. `event.json` is where you mock your event. `context.json` is where you can add additional mock data to the context passed to your lambda function. .env.` is where you place your deployment configuration. `deploy.env` has the same format as `.env`, but is used for holding any environment/config variables that you need to be deployed with your code to Lambda but you don't want in version control (e.g. DB connection info).

```
$ node-lambda setup --help
Expand Down Expand Up @@ -64,9 +64,10 @@ $ node-lambda run --help

Options:

-h, --help output usage information
-h, --handler [index.handler] Lambda Handler {index.handler}
-j, --eventFile [event.json] Event JSON File
-h, --help output usage information
-h, --handler [index.handler] Lambda Handler {index.handler}
-j, --eventFile [event.json] Event JSON File
-x, --contextFile [context.json] Context JSON file
```

#### deploy
Expand Down
2 changes: 2 additions & 0 deletions bin/node-lambda
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var AWS_DESCRIPTION = process.env.AWS_DESCRIPTION || '';
var AWS_RUNTIME = process.env.AWS_RUNTIME || 'nodejs';
var AWS_FUNCTION_VERSION = process.env.AWS_FUNCTION_VERSION || '';
var EVENT_FILE = process.env.EVENT_FILE || 'event.json';
var CONTEXT_FILE = process.env.CONTEXT_FILE || 'context.json';

program
.version(lambda.version)
Expand Down Expand Up @@ -55,6 +56,7 @@ program
.description('Run your Amazon Lambda application locally')
.option('-h, --handler [' + AWS_HANDLER + ']', 'Lambda Handler {index.handler}', AWS_HANDLER)
.option('-j, --eventFile [' + EVENT_FILE + ']', 'Event JSON File', EVENT_FILE)
.option('-x, --contextFile [' + CONTEXT_FILE + ']', 'Context JSON File', CONTEXT_FILE)
.action(function (prg) {
lambda.run(prg);
});
Expand Down
1 change: 1 addition & 0 deletions lib/context.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
21 changes: 11 additions & 10 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,32 @@ Lambda.prototype.setup = function () {

Lambda.prototype.run = function (program) {
this._createSampleFile('event.json');

this._createSampleFile('context.json');
var splitHandler = program.handler.split('.');
var filename = splitHandler[0] + '.js';
var handlername = splitHandler[1];

var handler = require(process.cwd() + '/' + filename)[handlername];
var event = require(process.cwd() + '/' + program.eventFile);
var context = require(process.cwd() + '/' + program.contextFile);

this._runHandler(handler, event);
this._runHandler(handler, event, context);
};

Lambda.prototype._runHandler = function (handler, event) {
var context = {
succeed: function (result) {
Lambda.prototype._runHandler = function (handler, event, context) {
context.succeed = function (result) {
console.log('succeed: ' + JSON.stringify(result));
process.exit(0);
},
fail: function (error) {
}

context.fail = function (error) {
console.log('fail: ' + error);
process.exit(-1);
},
done: function () {
}

context.done = function () {
process.exit(0);
}
};

handler(event, context);
};
Expand Down