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
2 changes: 2 additions & 0 deletions bin/node-lambda
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/.env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
AWS_ENVIRONMENT=development
AWS_ACCESS_KEY_ID=your_key
AWS_SECRET_ACCESS_KEY=your_secret
AWS_PROFILE=NO_NEED_KEY
AWS_SESSION_TOKEN=
AWS_ROLE_ARN=your_amazon_role
AWS_REGION=us-east-1
Expand Down
96 changes: 52 additions & 44 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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));
Expand Down Expand Up @@ -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.");
}

};
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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);
}

Expand All @@ -515,5 +524,4 @@ Lambda.prototype.deploy = function (program) {
});
};


module.exports = new Lambda();
1 change: 1 addition & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var originalProgram = {
environment: 'development',
accessKey: 'key',
secretKey: 'secret',
profile: 'profile',
sessionToken: 'token',
functionName: 'node-lambda',
handler: 'index.handler',
Expand Down