Skip to content

Commit 883aa60

Browse files
briandonahueDeviaVir
authored andcommitted
Update to pass callback for nodejs4.3 runtime (#74)
* Update to pass callback for nodejs4.3 runtime * Default to recommended runtime * Add note about runtimes * Added note on how to target v0.10.36
1 parent e9db472 commit 883aa60

3 files changed

Lines changed: 55 additions & 10 deletions

File tree

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ $ node-lambda run --help
6868
-h, --help output usage information
6969
-h, --handler [index.handler] Lambda Handler {index.handler}
7070
-j, --eventFile [event.json] Event JSON File
71+
-u, --runtime [nodejs4.3] Lambda Runtime {nodejs4.3, nodejs} - "nodejs4.3" is the current standard, "nodejs" is v0.10.36
7172
```
7273

7374
#### package
@@ -111,7 +112,7 @@ $ node-lambda deploy --help
111112
-m, --memorySize [128] Lambda Memory Size
112113
-t, --timeout [3] Lambda Timeout
113114
-d, --description [missing] Lambda Description
114-
-u, --runtime [nodejs] Lambda Runtime
115+
-u, --runtime [nodejs4.3] Lambda Runtime {nodejs4.3, nodejs} - "nodejs4.3" is the current standard, "nodejs" is v0.10.36
115116
-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
116117
-v, --version [custom-version] Lambda Version
117118
-f, --configFile [] Path to file holding secret environment variables (e.g. "deploy.env")`
@@ -123,6 +124,15 @@ $ node-lambda deploy --help
123124

124125
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.
125126

127+
## Node.js Runtime Configuration
128+
129+
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,
130+
`context.done()`, `context.succeed()`, and `context.fail()` are deprecated in favor of the Node convention of passing in
131+
a callback function. These will still work for now for backward compatibility, but are no longer recommended.
132+
133+
v0.10.36 is still supported, and can be targeted by changing the `AWS_RUNTIME` value to `nodejs` in the `.env` file.
134+
135+
126136
## Other AWS Lambda Tools Projects
127137

128138
+ [lambdaws](https://github.com/mentum/lambdaws)

bin/node-lambda

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var AWS_ROLE = process.env.AWS_ROLE_ARN || process.env.AWS_ROLE || 'missing';
1919
var AWS_MEMORY_SIZE = process.env.AWS_MEMORY_SIZE || 128;
2020
var AWS_TIMEOUT = process.env.AWS_TIMEOUT || 60;
2121
var AWS_DESCRIPTION = process.env.AWS_DESCRIPTION || '';
22-
var AWS_RUNTIME = process.env.AWS_RUNTIME || 'nodejs';
22+
var AWS_RUNTIME = process.env.AWS_RUNTIME || 'nodejs4.3';
2323
var AWS_PUBLISH = process.env.AWS_PUBLIS || false;
2424
var AWS_FUNCTION_VERSION = process.env.AWS_FUNCTION_VERSION || '';
2525
var AWS_VPC_SUBNETS = process.env.AWS_VPC_SUBNETS || '';
@@ -76,6 +76,7 @@ program
7676
.description('Run your Amazon Lambda application locally')
7777
.option('-h, --handler [' + AWS_HANDLER + ']', 'Lambda Handler {index.handler}', AWS_HANDLER)
7878
.option('-j, --eventFile [' + EVENT_FILE + ']', 'Event JSON File', EVENT_FILE)
79+
.option('-u, --runtime [' + AWS_RUNTIME + ']', 'Lambda Runtime', AWS_RUNTIME)
7980
.action(function (prg) {
8081
lambda.run(prg);
8182
});

lib/main.js

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,59 @@ Lambda.prototype.run = function (program) {
4545
var handler = require(process.cwd() + '/' + filename)[handlername];
4646
var event = require(process.cwd() + '/' + program.eventFile);
4747

48-
this._runHandler(handler, event);
48+
this._runHandler(handler, event, program.runtime);
4949
};
5050

51-
Lambda.prototype._runHandler = function (handler, event) {
51+
Lambda.prototype._runHandler = function (handler, event, runtime) {
52+
53+
var callback = function (err, result) {
54+
if (err) {
55+
console.log('Error: ' + error);
56+
process.exit(-1);
57+
}
58+
else {
59+
console.log('Success:');
60+
if (result) {
61+
console.log(JSON.stringify(result));
62+
}
63+
process.exit(0);
64+
}
65+
};
66+
5267
var context = {
68+
isNode43: runtime === "nodejs4.3",
5369
succeed: function (result) {
54-
console.log('succeed: ' + JSON.stringify(result));
55-
process.exit(0);
70+
if (isNode43) {
71+
console.log('context.succeed() is deprecated with Node.js 4.3 runtime');
72+
}
73+
callback(null, result);
5674
},
5775
fail: function (error) {
58-
console.log('fail: ' + error);
59-
process.exit(-1);
76+
if (isNode43) {
77+
console.log('context.fail() is deprecated with Node.js 4.3 runtime');
78+
}
79+
callback(error);
6080
},
6181
done: function () {
62-
process.exit(0);
82+
if (isNode43) {
83+
console.log('context.done() is deprecated with Node.js 4.3 runtime');
84+
}
85+
callback();
6386
}
6487
};
6588

66-
handler(event, context);
89+
switch(runtime) {
90+
case "nodejs":
91+
handler(event, context);
92+
break;
93+
case "nodejs4.3":
94+
handler(event, context, callback);
95+
break;
96+
default:
97+
console.error("Runtime [" + runtime + "] is not supported.");
98+
}
99+
100+
67101
};
68102

69103
Lambda.prototype._params = function (program, buffer) {

0 commit comments

Comments
 (0)