Skip to content
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ $ node-lambda run --help
-h, --help Output usage information
--handler [index.handler] Lambda Handler {index.handler}
-j, --eventFile [event.json] Event JSON File
-f, --configFile [] Path to file holding secret environment variables (e.g. "deploy.env")
-u, --runtime [nodejs4.3] Lambda Runtime {nodejs4.3, nodejs} - "nodejs4.3" is the current standard, "nodejs" is v0.10.36
-x, --contextFile [context.json] Context JSON file
```
Expand Down
2 changes: 2 additions & 0 deletions bin/node-lambda
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ program
.option('--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)
.option('-f, --configFile [' + CONFIG_FILE + ']',
'Path to file holding secret environment variables (e.g. "deploy.env")', CONFIG_FILE)
.option('-x, --contextFile [' + CONTEXT_FILE + ']', 'Context JSON File', CONTEXT_FILE)
.action(function (prg) {
lambda.run(prg);
Expand Down
18 changes: 18 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ Lambda.prototype.run = function (program) {
var event = require(process.cwd() + '/' + program.eventFile);
var context = require(process.cwd() + '/' + program.contextFile);

// Set custom environment variables if program.configFile is defined
if (program.configFile) {
this._setRunTimeEnvironmentVars(program);
}

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

Expand Down Expand Up @@ -300,6 +305,19 @@ Lambda.prototype._setEnvironmentVars = function (program, codeDirectory) {
fs.writeFileSync(handlerFileName, contentStr);
};

Lambda.prototype._setRunTimeEnvironmentVars = function (program) {
var configValues = fs.readFileSync(program.configFile);
var config = dotenv.parse(configValues);

for (var k in config) {
if (!config.hasOwnProperty(k)) {
continue;
}

process.env[k]=config[k];
}
};

Lambda.prototype._uploadExisting = function(lambda, params, cb) {
return lambda.updateFunctionCode({
'FunctionName': params.FunctionName,
Expand Down
23 changes: 23 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,29 @@ describe('node-lambda', function () {

});

describe('environment variable injection at runtime', function () {
beforeEach(function () {
// Prep...
fs.writeFileSync('tmp.env', 'FOO=bar\nBAZ=bing\n');
});

afterEach(function () {
fs.unlinkSync('tmp.env');
});

it('should inject environment variables at runtime', function () {

// Run it...
lambda._setRunTimeEnvironmentVars({
configFile: 'tmp.env'
}, process.cwd());

assert.equal(process.env["FOO"], 'bar');
assert.equal(process.env["BAZ"], 'bing');
});

});

describe('environment variable injection - "use strict" allowance', function () {
beforeEach(function () {
// Prep...
Expand Down