diff --git a/README.md b/README.md index 6a388757..17c0eace 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ $ node-lambda deploy --help ## Custom Environment Variables -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. +AWS Lambda will let you set environment variables for your function. Use the sample `deploy.env` file in combination with the `--configFile` flag to set values which will be added to the lambda configuration upon deploy. Environment variables will also be set when running locally using the same flag ## Node.js Runtime Configuration diff --git a/lib/main.js b/lib/main.js index 72f5211c..689652c4 100644 --- a/lib/main.js +++ b/lib/main.js @@ -122,7 +122,10 @@ Lambda.prototype._params = function (program, buffer) { MemorySize: program.memorySize, Timeout: program.timeout, Publish: program.publish, - VpcConfig: {} + VpcConfig: {}, + Environment: { + Variables: {} + } }; if (program.lambdaVersion) { params.FunctionName += ('-' + program.lambdaVersion); @@ -133,7 +136,14 @@ Lambda.prototype._params = function (program, buffer) { 'SecurityGroupIds': program.vpcSecurityGroups.split(',') }; } - + if (program.configFile) { + var configValues = fs.readFileSync(program.configFile); + var config = dotenv.parse(configValues); + params.Environment = { + Variables: config + } + } + return params; }; @@ -276,41 +286,6 @@ Lambda.prototype._cleanDirectory = function (codeDirectory, callback) { }); }; -Lambda.prototype._setEnvironmentVars = function (program, codeDirectory) { - console.log('=> Setting "environment variables" for Lambda from %s', program.configFile); - // Which file is the handler? - var handlerFileName = codeDirectory + '/' + program.handler.split('.').shift() + '.js'; - var contents = fs.readFileSync(handlerFileName); - - var configValues = fs.readFileSync(program.configFile); - var prefix = '////////////////////////////////////\n// "Environment Variables"\n'; - var config = dotenv.parse(configValues); - var contentStr = contents.toString(); - - if(program.environment) { - prefix += 'process.env["environment"]=' + JSON.stringify(program.environment) + ';\n'; - } - - for (var k in config) { - if (!config.hasOwnProperty(k)) { - continue; - } - - // Use JSON.stringify to ensure that it's valid code. - prefix += 'process.env["' + k + '"]=' + JSON.stringify(config[k]) + ';\n'; - } - prefix += '////////////////////////////////////\n\n'; - - // If the first line of the file is 'use strict', append after - if (contentStr.trim().indexOf('use strict') === 1) { - contentStr = contentStr.replace(/([`'"]use strict[`'"][;]?)/, '$1\n' + prefix); - } else { - contentStr = prefix + contentStr; - } - - fs.writeFileSync(handlerFileName, contentStr); -}; - Lambda.prototype._setRunTimeEnvironmentVars = function (program) { var configValues = fs.readFileSync(program.configFile); var config = dotenv.parse(configValues); @@ -341,7 +316,8 @@ Lambda.prototype._uploadExisting = function (lambda, params, cb) { 'MemorySize': params.MemorySize, 'Role': params.Role, 'Timeout': params.Timeout, - 'VpcConfig': params.VpcConfig + 'VpcConfig': params.VpcConfig, + 'Environment': params.Environment }, function (err, data) { return cb(err, data); }); @@ -368,10 +344,6 @@ Lambda.prototype._archivePrebuilt = function (program, archive_callback) { return archive_callback(err); } - // Add custom environment variables if program.configFile is defined - if (program.configFile) { - _this._setEnvironmentVars(program, codeDirectory); - } console.log('=> Zipping deployment package'); var archive = process.platform !== 'win32' ? _this._nativeZip : _this._zip; archive = archive.bind(_this); @@ -415,10 +387,6 @@ Lambda.prototype._buildAndArchive = function (program, archive_callback) { return archive_callback(err); } - // Add custom environment variables if program.configFile is defined - if (program.configFile) { - _this._setEnvironmentVars(program, codeDirectory); - } console.log('=> Zipping deployment package'); var archive = process.platform !== 'win32' ? _this._nativeZip : _this._zip; diff --git a/test/main.js b/test/main.js index 0be10b46..31bc0ff4 100644 --- a/test/main.js +++ b/test/main.js @@ -73,6 +73,29 @@ describe('node-lambda', function () { var params = lambda._params(program); assert.equal(Object.keys(params.VpcConfig).length, 0); }); + + describe('configFile', function () { + beforeEach(function () { + // Prep... + fs.writeFileSync('tmp.env', 'FOO=bar\nBAZ=bing\n'); + }); + + afterEach(function () { + fs.unlinkSync('tmp.env'); + }); + + it('adds variables when configFile param is set', function () { + program.configFile = 'tmp.env'; + var params = lambda._params(program); + assert.equal(params.Environment.Variables['FOO'], "bar"); + assert.equal(params.Environment.Variables['BAZ'], "bing"); + }); + + it('does not add when configFile param is not set', function () { + var params = lambda._params(program); + assert.equal(Object.keys(params.Environment.Variables).length, 0); + }); + }); }); describe('_zipfileTmpPath', function () { @@ -329,33 +352,6 @@ describe('node-lambda', function () { }); }); - describe('environment variable injection', function () { - beforeEach(function () { - // Prep... - fs.writeFileSync('tmp.env', 'FOO=bar\nBAZ=bing\n'); - fs.writeFileSync('test.js', ''); - }); - - afterEach(function () { - fs.unlinkSync('tmp.env'); - fs.unlinkSync('test.js'); - }); - - it('should inject environment variables at the top of the entry point file', function () { - - // Run it... - lambda._setEnvironmentVars({ - configFile: 'tmp.env', - handler: 'test.handler', - }, process.cwd()); - - assert.equal(fs.readFileSync('test.js').toString(), '////////////////////////////////////\n' + - '// "Environment Variables"\nprocess.env["FOO"]="bar";\n' + - 'process.env["BAZ"]="bing";\n////////////////////////////////////\n\n'); - }); - - }); - describe('environment variable injection at runtime', function () { beforeEach(function () { // Prep... @@ -379,33 +375,6 @@ describe('node-lambda', function () { }); - describe('environment variable injection - "use strict" allowance', function () { - beforeEach(function () { - // Prep... - fs.writeFileSync('tmp.env', 'FOO=bar\nBAZ=bing\n'); - fs.writeFileSync('test.js', '\'use strict\';'); - }); - - afterEach(function () { - fs.unlinkSync('tmp.env'); - fs.unlinkSync('test.js'); - }); - - it('should inject environment variables at the top of the entry point file', function () { - - // Run it... - lambda._setEnvironmentVars({ - configFile: 'tmp.env', - handler: 'test.handler', - }, process.cwd()); - - assert.equal(fs.readFileSync('test.js').toString(), '\'use strict\';\n////////////////////////////////////\n' + - '// "Environment Variables"\nprocess.env["FOO"]="bar";\n' + - 'process.env["BAZ"]="bing";\n////////////////////////////////////\n\n'); - }); - - }); - describe('create sample files', function () { afterEach(function () {