Skip to content

Commit 302fdf7

Browse files
erselDeviaVir
authored andcommitted
allow injection of environment variables via config file at runtime (#136)
1 parent 55edb5d commit 302fdf7

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ $ node-lambda run --help
6868
-h, --help Output usage information
6969
--handler [index.handler] Lambda Handler {index.handler}
7070
-j, --eventFile [event.json] Event JSON File
71+
-f, --configFile [] Path to file holding secret environment variables (e.g. "deploy.env")
7172
-u, --runtime [nodejs4.3] Lambda Runtime {nodejs4.3, nodejs} - "nodejs4.3" is the current standard, "nodejs" is v0.10.36
7273
-x, --contextFile [context.json] Context JSON file
7374
```

bin/node-lambda

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ program
9292
.option('--handler [' + AWS_HANDLER + ']', 'Lambda Handler {index.handler}', AWS_HANDLER)
9393
.option('-j, --eventFile [' + EVENT_FILE + ']', 'Event JSON File', EVENT_FILE)
9494
.option('-u, --runtime [' + AWS_RUNTIME + ']', 'Lambda Runtime', AWS_RUNTIME)
95+
.option('-f, --configFile [' + CONFIG_FILE + ']',
96+
'Path to file holding secret environment variables (e.g. "deploy.env")', CONFIG_FILE)
9597
.option('-x, --contextFile [' + CONTEXT_FILE + ']', 'Context JSON File', CONTEXT_FILE)
9698
.action(function (prg) {
9799
lambda.run(prg);

lib/main.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ Lambda.prototype.run = function (program) {
4646
var event = require(process.cwd() + '/' + program.eventFile);
4747
var context = require(process.cwd() + '/' + program.contextFile);
4848

49+
// Set custom environment variables if program.configFile is defined
50+
if (program.configFile) {
51+
this._setRunTimeEnvironmentVars(program);
52+
}
53+
4954
this._runHandler(handler, event, program.runtime, context);
5055
};
5156

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

308+
Lambda.prototype._setRunTimeEnvironmentVars = function (program) {
309+
var configValues = fs.readFileSync(program.configFile);
310+
var config = dotenv.parse(configValues);
311+
312+
for (var k in config) {
313+
if (!config.hasOwnProperty(k)) {
314+
continue;
315+
}
316+
317+
process.env[k]=config[k];
318+
}
319+
};
320+
303321
Lambda.prototype._uploadExisting = function(lambda, params, cb) {
304322
return lambda.updateFunctionCode({
305323
'FunctionName': params.FunctionName,

test/main.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,29 @@ describe('node-lambda', function () {
276276

277277
});
278278

279+
describe('environment variable injection at runtime', function () {
280+
beforeEach(function () {
281+
// Prep...
282+
fs.writeFileSync('tmp.env', 'FOO=bar\nBAZ=bing\n');
283+
});
284+
285+
afterEach(function () {
286+
fs.unlinkSync('tmp.env');
287+
});
288+
289+
it('should inject environment variables at runtime', function () {
290+
291+
// Run it...
292+
lambda._setRunTimeEnvironmentVars({
293+
configFile: 'tmp.env'
294+
}, process.cwd());
295+
296+
assert.equal(process.env["FOO"], 'bar');
297+
assert.equal(process.env["BAZ"], 'bing');
298+
});
299+
300+
});
301+
279302
describe('environment variable injection - "use strict" allowance', function () {
280303
beforeEach(function () {
281304
// Prep...

0 commit comments

Comments
 (0)