From ff3e38552e27dcb8c13b652e68f76ed1d1667be1 Mon Sep 17 00:00:00 2001 From: Ersel Aker Date: Sat, 13 Aug 2016 20:25:31 +0100 Subject: [PATCH] allow injection of environment variables via config file at runtime --- README.md | 1 + bin/node-lambda | 2 ++ lib/main.js | 18 ++++++++++++++++++ test/main.js | 23 +++++++++++++++++++++++ 4 files changed, 44 insertions(+) diff --git a/README.md b/README.md index 81ef4a47..c898425f 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/bin/node-lambda b/bin/node-lambda index dcb3ca2c..ae68e04a 100755 --- a/bin/node-lambda +++ b/bin/node-lambda @@ -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); diff --git a/lib/main.js b/lib/main.js index 3ecc43b1..d1b5b8e9 100644 --- a/lib/main.js +++ b/lib/main.js @@ -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); }; @@ -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, diff --git a/test/main.js b/test/main.js index 99238ef2..2bef2e68 100644 --- a/test/main.js +++ b/test/main.js @@ -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...