From 5d7d0fa38ddd3fd59306056bf03b9925a3045543 Mon Sep 17 00:00:00 2001 From: Brian Donahue Date: Mon, 11 Apr 2016 11:58:45 -0400 Subject: [PATCH 1/8] Update to pass callback for nodejs4.3 runtime * Also output deprecation warning for older runtime --- bin/node-lambda | 1 + lib/main.js | 44 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/bin/node-lambda b/bin/node-lambda index b846586f..a76e59c3 100755 --- a/bin/node-lambda +++ b/bin/node-lambda @@ -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..27db3cca 100644 --- a/lib/main.js +++ b/lib/main.js @@ -45,25 +45,53 @@ 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) { From cd224dfc960377efcf6ef3dabe324fe3b2288dce Mon Sep 17 00:00:00 2001 From: Brian Donahue Date: Mon, 11 Apr 2016 13:35:51 -0400 Subject: [PATCH 2/8] Update runtime options --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a4bc09f1..2b65f56b 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 [nodejs] Lambda Runtime {nodejs, nodejs4.3} ``` #### 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 [nodejs] Lambda Runtime {nodejs, nodejs4.3} -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")` From 01f2b5ea26f2d88b0ee9fb28cf42245dfede6926 Mon Sep 17 00:00:00 2001 From: Brian Donahue Date: Mon, 11 Apr 2016 13:59:04 -0400 Subject: [PATCH 3/8] style correction --- lib/main.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 27db3cca..be685850 100644 --- a/lib/main.js +++ b/lib/main.js @@ -67,15 +67,21 @@ Lambda.prototype._runHandler = function (handler, event, runtime) { var context = { isNode43: runtime === "nodejs4.3", succeed: function (result) { - if(isNode43) console.log('context.succeed() is deprecated with Node.js 4.3 runtime'); + if (isNode43) { + console.log('context.succeed() is deprecated with Node.js 4.3 runtime'); + } callback(null, result); }, fail: function (error) { - if(isNode43) console.log('context.fail() is deprecated with Node.js 4.3 runtime'); + if (isNode43) { + console.log('context.fail() is deprecated with Node.js 4.3 runtime'); + } callback(error); }, done: function () { - if(isNode43) console.log('context.done() is deprecated with Node.js 4.3 runtime'); + if (isNode43) { + console.log('context.done() is deprecated with Node.js 4.3 runtime'); + } callback(); } }; From d1046cfe7b417f0d522da249fb805d90d95a6968 Mon Sep 17 00:00:00 2001 From: Brian Donahue Date: Mon, 11 Apr 2016 14:04:31 -0400 Subject: [PATCH 4/8] Default to recommended runtime --- bin/node-lambda | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/node-lambda b/bin/node-lambda index a76e59c3..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 || ''; From 22373dfb3a6c302526df4c8d551aaafa1ec8a3ab Mon Sep 17 00:00:00 2001 From: Brian Donahue Date: Mon, 11 Apr 2016 14:04:45 -0400 Subject: [PATCH 5/8] Update runtime option with more detail --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2b65f56b..4223fe70 100644 --- a/README.md +++ b/README.md @@ -68,7 +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 [nodejs] Lambda Runtime {nodejs, nodejs4.3} + -u, --runtime [nodejs4.3] Lambda Runtime {nodejs4.3, nodejs} - `nodejs4.3` is the current standard, `nodejs` is v0.10.36 ``` #### package @@ -113,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 {nodejs, nodejs4.3} + -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")` From 364557b5a80e5f2be1a8e7e469d0dbacba40eafe Mon Sep 17 00:00:00 2001 From: Brian Donahue Date: Mon, 11 Apr 2016 14:15:38 -0400 Subject: [PATCH 6/8] removed backticks which were already inside pre-formatted blocks :) --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4223fe70..dc85e06c 100644 --- a/README.md +++ b/README.md @@ -68,7 +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 + -u, --runtime [nodejs4.3] Lambda Runtime {nodejs4.3, nodejs} - "nodejs4.3" is the current standard, "nodejs" is v0.10.36 ``` #### package @@ -113,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 [nodejs4.3] Lambda Runtime {nodejs4.3, nodejs} - `nodejs4.3` is the current standard, `nodejs` is v0.10.36 + -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")` From 280941c5324ea6a94cb99f2782f7bb5d7f192b05 Mon Sep 17 00:00:00 2001 From: Brian Donahue Date: Mon, 11 Apr 2016 15:18:40 -0400 Subject: [PATCH 7/8] Add note about runtimes --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index dc85e06c..a885c4d8 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,13 @@ $ 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. + + ## Other AWS Lambda Tools Projects + [lambdaws](https://github.com/mentum/lambdaws) From 1ec3d15d920b350bb6c683cb14e43a4c67d19877 Mon Sep 17 00:00:00 2001 From: Brian Donahue Date: Mon, 11 Apr 2016 15:22:16 -0400 Subject: [PATCH 8/8] Added note on how to target v0.10.36 --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index a885c4d8..a331df9d 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,8 @@ AWS Lambda now supports Node.js v4.3.2, and there have been some [API changes](h `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