diff --git a/README.md b/README.md index a4bc09f1..a331df9d 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,7 @@ $ node-lambda run --help -h, --help output usage information -h, --handler [index.handler] Lambda Handler {index.handler} -j, --eventFile [event.json] Event JSON File + -u, --runtime [nodejs4.3] Lambda Runtime {nodejs4.3, nodejs} - "nodejs4.3" is the current standard, "nodejs" is v0.10.36 ``` #### package @@ -112,7 +113,7 @@ $ node-lambda deploy --help -m, --memorySize [128] Lambda Memory Size -t, --timeout [3] Lambda Timeout -d, --description [missing] Lambda Description - -u, --runtime [nodejs] Lambda Runtime + -u, --runtime [nodejs4.3] Lambda Runtime {nodejs4.3, nodejs} - "nodejs4.3" is the current standard, "nodejs" is v0.10.36 -p, --publish [false] This boolean parameter can be used to request AWS Lambda to create the Lambda function and publish a version as an atomic operation -v, --version [custom-version] Lambda Version -f, --configFile [] Path to file holding secret environment variables (e.g. "deploy.env")` @@ -124,6 +125,15 @@ $ node-lambda deploy --help AWS Lambda doesn't let you set environment variables for your function, but in many cases you will need to configure your function with secure values that you don't want to check into version control, for example a DB connection string or encryption key. Use the sample `deploy.env` file in combination with the `--configFile` flag to set values which will be prepended to your compiled Lambda function as `process.env` environment variables before it gets uploaded to S3. +## Node.js Runtime Configuration + +AWS Lambda now supports Node.js v4.3.2, and there have been some [API changes](http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-using-old-runtime.html) for the new version. Most notably, +`context.done()`, `context.succeed()`, and `context.fail()` are deprecated in favor of the Node convention of passing in +a callback function. These will still work for now for backward compatibility, but are no longer recommended. + +v0.10.36 is still supported, and can be targeted by changing the `AWS_RUNTIME` value to `nodejs` in the `.env` file. + + ## Other AWS Lambda Tools Projects + [lambdaws](https://github.com/mentum/lambdaws) diff --git a/bin/node-lambda b/bin/node-lambda index b846586f..072c6c16 100755 --- a/bin/node-lambda +++ b/bin/node-lambda @@ -19,7 +19,7 @@ var AWS_ROLE = process.env.AWS_ROLE_ARN || process.env.AWS_ROLE || 'missing'; var AWS_MEMORY_SIZE = process.env.AWS_MEMORY_SIZE || 128; var AWS_TIMEOUT = process.env.AWS_TIMEOUT || 60; var AWS_DESCRIPTION = process.env.AWS_DESCRIPTION || ''; -var AWS_RUNTIME = process.env.AWS_RUNTIME || 'nodejs'; +var AWS_RUNTIME = process.env.AWS_RUNTIME || 'nodejs4.3'; var AWS_PUBLISH = process.env.AWS_PUBLIS || false; var AWS_FUNCTION_VERSION = process.env.AWS_FUNCTION_VERSION || ''; var AWS_VPC_SUBNETS = process.env.AWS_VPC_SUBNETS || ''; @@ -76,6 +76,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('-u, --runtime [' + AWS_RUNTIME + ']', 'Lambda Runtime', AWS_RUNTIME) .action(function (prg) { lambda.run(prg); }); diff --git a/lib/main.js b/lib/main.js index b74720a3..be685850 100644 --- a/lib/main.js +++ b/lib/main.js @@ -45,25 +45,59 @@ Lambda.prototype.run = function (program) { var handler = require(process.cwd() + '/' + filename)[handlername]; var event = require(process.cwd() + '/' + program.eventFile); - this._runHandler(handler, event); + this._runHandler(handler, event, program.runtime); }; -Lambda.prototype._runHandler = function (handler, event) { +Lambda.prototype._runHandler = function (handler, event, runtime) { + + var callback = function (err, result) { + if (err) { + console.log('Error: ' + error); + process.exit(-1); + } + else { + console.log('Success:'); + if (result) { + console.log(JSON.stringify(result)); + } + process.exit(0); + } + }; + var context = { + isNode43: runtime === "nodejs4.3", succeed: function (result) { - console.log('succeed: ' + JSON.stringify(result)); - process.exit(0); + if (isNode43) { + console.log('context.succeed() is deprecated with Node.js 4.3 runtime'); + } + callback(null, result); }, fail: function (error) { - console.log('fail: ' + error); - process.exit(-1); + if (isNode43) { + console.log('context.fail() is deprecated with Node.js 4.3 runtime'); + } + callback(error); }, done: function () { - process.exit(0); + if (isNode43) { + console.log('context.done() is deprecated with Node.js 4.3 runtime'); + } + callback(); } }; - handler(event, context); + switch(runtime) { + case "nodejs": + handler(event, context); + break; + case "nodejs4.3": + handler(event, context, callback); + break; + default: + console.error("Runtime [" + runtime + "] is not supported."); + } + + }; Lambda.prototype._params = function (program, buffer) {