diff --git a/bin/node-lambda b/bin/node-lambda index ae68e04a..e9319971 100755 --- a/bin/node-lambda +++ b/bin/node-lambda @@ -15,6 +15,7 @@ var CONFIG_FILE = process.env.CONFIG_FILE || ''; var EXCLUDE_GLOBS = process.env.EXCLUDE_GLOBS || ''; var AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID; var AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY; +var AWS_PROFILE = process.env.AWS_PROFILE || ''; var AWS_SESSION_TOKEN = process.env.AWS_SESSION_TOKEN || ''; var AWS_REGION = process.env.AWS_REGION || 'us-east-1,us-west-2,eu-west-1'; var AWS_FUNCTION_NAME = process.env.AWS_FUNCTION_NAME || packageJsonName; @@ -43,6 +44,7 @@ program AWS_ENVIRONMENT) .option('-a, --accessKey [' + AWS_ACCESS_KEY_ID + ']', 'AWS Access Key', AWS_ACCESS_KEY_ID) .option('-s, --secretKey [' + AWS_SECRET_ACCESS_KEY + ']', 'AWS Secret Key', AWS_SECRET_ACCESS_KEY) + .option('-p, --profile [' + AWS_PROFILE + ']', 'AWS profile', AWS_PROFILE) .option('-k, --sessionToken [' + AWS_SESSION_TOKEN + ']', 'AWS Session Token', AWS_SESSION_TOKEN) .option('-r, --region [' + AWS_REGION + ']', 'AWS Region', AWS_REGION) .option('-n, --functionName [' + AWS_FUNCTION_NAME + ']', 'Lambda FunctionName', AWS_FUNCTION_NAME) diff --git a/lib/.env.example b/lib/.env.example index 1fd0bcd2..dc9ed6c1 100644 --- a/lib/.env.example +++ b/lib/.env.example @@ -1,6 +1,7 @@ AWS_ENVIRONMENT=development AWS_ACCESS_KEY_ID=your_key AWS_SECRET_ACCESS_KEY=your_secret +AWS_PROFILE= AWS_SESSION_TOKEN= AWS_ROLE_ARN=your_amazon_role AWS_REGION=us-east-1 diff --git a/lib/main.js b/lib/main.js index 010b7f88..5ab94e88 100644 --- a/lib/main.js +++ b/lib/main.js @@ -33,7 +33,8 @@ Lambda.prototype.setup = function (program) { this._createSampleFile(program.eventFile, 'event.json'); this._createSampleFile('deploy.env', 'deploy.env'); this._createSampleFile(program.contextFile, 'context.json'); - console.log('Setup done. Edit the .env, deploy.env, ' + program.contextFile + ' and ' + program.eventFile + ' files as needed.'); + console.log('Setup done. Edit the .env, deploy.env, ' + program.contextFile + ' and ' + program.eventFile + + ' files as needed.'); }; Lambda.prototype.run = function (program) { @@ -60,8 +61,7 @@ Lambda.prototype._runHandler = function (handler, event, runtime, context) { if (err) { console.log('Error: ' + err); process.exit(-1); - } - else { + } else { console.log('Success:'); if (result) { console.log(JSON.stringify(result)); @@ -90,15 +90,15 @@ Lambda.prototype._runHandler = function (handler, event, runtime, context) { callback(); }; - switch(runtime) { - case "nodejs": - handler(event, context); - break; - case "nodejs4.3": - handler(event, context, callback); - break; - default: - console.error("Runtime [" + runtime + "] is not supported."); + switch (runtime) { + case "nodejs": + handler(event, context); + break; + case "nodejs4.3": + handler(event, context, callback); + break; + default: + console.error("Runtime [" + runtime + "] is not supported."); } }; @@ -144,18 +144,18 @@ Lambda.prototype._zipfileTmpPath = function (program) { Lambda.prototype._rsync = function (program, src, dest, excludeNodeModules, callback) { var excludes = ['.git*', '*.swp', '.editorconfig', 'deploy.env', '*.log', 'build/'], - excludeGlobs = []; + excludeGlobs = []; if (program.excludeGlobs) { excludeGlobs = program.excludeGlobs.split(' '); } var excludeArgs = excludeGlobs .concat(excludes) .concat(excludeNodeModules ? ['node_modules'] : []) - .map(function(exclude) { + .map(function (exclude) { return '--exclude=' + exclude; }).join(' '); - exec('mkdir -p ' + dest, function(err) { + exec('mkdir -p ' + dest, function (err) { if (err) { return callback(err); } @@ -184,23 +184,25 @@ Lambda.prototype._npmInstall = function (program, codeDirectory, callback) { Lambda.prototype._postInstallScript = function (codeDirectory, callback) { var script_filename = 'post_install.sh'; - var cmd = './'+script_filename; + var cmd = './' + script_filename; var filePath = [codeDirectory, script_filename].join('/'); - fs.exists(filePath, function(exists) { + fs.exists(filePath, function (exists) { if (exists) { - console.log('=> Running post install script '+script_filename); - exec(cmd, { cwd: codeDirectory,maxBuffer: 50 * 1024 * 1024 }, function(error, stdout, stderr){ + console.log('=> Running post install script ' + script_filename); + exec(cmd, { + cwd: codeDirectory, + maxBuffer: 50 * 1024 * 1024 + }, function (error, stdout, stderr) { - if (error) callback(error +" stdout: " + stdout + "stderr"+stderr); + if (error) callback(error + " stdout: " + stdout + "stderr" + stderr); else { - console.log("\t\t"+stdout); + console.log("\t\t" + stdout); callback(null); } }); - } else { callback(null); } @@ -260,7 +262,7 @@ Lambda.prototype._cleanDirectory = function (codeDirectory, callback) { throw err; } - fs.mkdir(codeDirectory, function(err) { + fs.mkdir(codeDirectory, function (err) { if (err) { throw err; } @@ -281,8 +283,8 @@ Lambda.prototype._setEnvironmentVars = function (program, codeDirectory) { var config = dotenv.parse(configValues); var contentStr = contents.toString(); - if(program.environment){ - prefix += 'process.env["environment"]=' + JSON.stringify(program.environment) + ';\n'; + if (program.environment) { + prefix += 'process.env["environment"]=' + JSON.stringify(program.environment) + ';\n'; } for (var k in config) { @@ -314,17 +316,17 @@ Lambda.prototype._setRunTimeEnvironmentVars = function (program) { continue; } - process.env[k]=config[k]; + process.env[k] = config[k]; } }; -Lambda.prototype._uploadExisting = function(lambda, params, cb) { +Lambda.prototype._uploadExisting = function (lambda, params, cb) { return lambda.updateFunctionCode({ 'FunctionName': params.FunctionName, 'ZipFile': params.Code.ZipFile, 'Publish': params.Publish - }, function(err, data) { - if(err) { + }, function (err, data) { + if (err) { return cb(err, data); } @@ -336,22 +338,22 @@ Lambda.prototype._uploadExisting = function(lambda, params, cb) { 'Role': params.Role, 'Timeout': params.Timeout, 'VpcConfig': params.VpcConfig - }, function(err, data) { + }, function (err, data) { return cb(err, data); }); }); }; -Lambda.prototype._uploadNew = function(lambda, params, cb) { - return lambda.createFunction(params, function(err, data) { +Lambda.prototype._uploadNew = function (lambda, params, cb) { + return lambda.createFunction(params, function (err, data) { return cb(err, data); }); }; Lambda.prototype._archive = function (program, archive_callback) { - return program.prebuiltDirectory - ? this._archivePrebuilt(program, archive_callback) - : this._buildAndArchive(program, archive_callback); + return program.prebuiltDirectory ? + this._archivePrebuilt(program, archive_callback) : + this._buildAndArchive(program, archive_callback); } Lambda.prototype._archivePrebuilt = function (program, archive_callback) { @@ -389,7 +391,7 @@ Lambda.prototype._buildAndArchive = function (program, archive_callback) { var _this = this; var codeDirectory = _this._codeDirectory(program); - _this._cleanDirectory(codeDirectory, function(err) { + _this._cleanDirectory(codeDirectory, function (err) { if (err) { return archive_callback(err); } @@ -437,7 +439,7 @@ Lambda.prototype.package = function (program) { if (!isDir) { throw program.packageDirectory + ' is not a directory!'; } - } catch(err) { + } catch (err) { if (err.code === 'ENOENT') { console.log('=> Creating package directory'); fs.mkdirSync(program.packageDirectory); @@ -455,7 +457,7 @@ Lambda.prototype.package = function (program) { var basename = program.functionName + (program.environment ? '-' + program.environment : ''); var zipfile = path.join(program.packageDirectory, basename + '.zip'); console.log('=> Writing packaged zip'); - fs.writeFile(zipfile, buffer, function(err) { + fs.writeFile(zipfile, buffer, function (err) { if (err) { throw err; } @@ -480,12 +482,19 @@ Lambda.prototype.deploy = function (program) { console.log(params); var aws_security = { - accessKeyId: program.accessKey, - secretAccessKey: program.secretKey, region: region }; - if (program.sessionToken){ + if (program.profile) { + aws.config.credentials = new aws.SharedIniFileCredentials({ + profile: program.profile + }); + } else { + aws_security.accessKeyId = program.accessKey; + aws_security.secretAccessKey = program.secretKey; + } + + if (program.sessionToken) { aws_security.sessionToken = program.sessionToken; } @@ -497,8 +506,8 @@ Lambda.prototype.deploy = function (program) { return lambda.getFunction({ 'FunctionName': params.FunctionName - }, function(err) { - if(err) { + }, function (err) { + if (err) { return _this._uploadNew(lambda, params, cb); } @@ -515,5 +524,4 @@ Lambda.prototype.deploy = function (program) { }); }; - module.exports = new Lambda(); diff --git a/test/main.js b/test/main.js index 6e872040..356215c2 100644 --- a/test/main.js +++ b/test/main.js @@ -14,6 +14,7 @@ var originalProgram = { environment: 'development', accessKey: 'key', secretKey: 'secret', + profile: 'profile', sessionToken: 'token', functionName: 'node-lambda', handler: 'index.handler',