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
82 changes: 40 additions & 42 deletions bin/node-lambda
Original file line number Diff line number Diff line change
@@ -1,64 +1,62 @@
#!/usr/bin/env node

var
dotenv = require( "dotenv" ),
lambda = require( "../lib/main.js" ),
program = require( "commander" );
packageJson = require( process.cwd() + "/package.json" );

dotenv.load( );

var
AWS_ENVIRONMENT = process.env.AWS_ENVIRONMENT || "development",
AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID || "missing",
AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY || "missing",
AWS_REGION = process.env.AWS_REGION || "us-east-1",
AWS_FUNCTION_NAME = process.env.AWS_FUNCTION_NAME || packageJson.name,
AWS_HANDLER = process.env.AWS_HANDLER || "index.handler",
AWS_MODE = "event",
AWS_ROLE = process.env.AWS_ROLE || "missing",
AWS_MEMORY_SIZE = process.env.AWS_MEMORY_SIZE || 128,
AWS_TIMEOUT = process.env.AWS_TIMEOUT || 3,
AWS_DESCRIPTION = process.env.AWS_DESCRIPTION || "",
AWS_RUNTIME = process.env.AWS_RUNTIME || "nodejs";
var dotenv = require('dotenv');

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice. airbnb style.

var lambda = require('../lib/main.js');
var program = require('commander');
var packageJson = require(process.cwd() + '/package.json');

dotenv.load();

var AWS_ENVIRONMENT = process.env.AWS_ENVIRONMENT || 'development';
var AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID || 'missing';
var AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY || 'missing';
var AWS_REGION = process.env.AWS_REGION || 'us-east-1';
var AWS_FUNCTION_NAME = process.env.AWS_FUNCTION_NAME || packageJson.name;
var AWS_HANDLER = process.env.AWS_HANDLER || 'index.handler';
var AWS_MODE = 'event';
var AWS_ROLE = process.env.AWS_ROLE || 'missing';
var AWS_MEMORY_SIZE = process.env.AWS_MEMORY_SIZE || 128;
var AWS_TIMEOUT = process.env.AWS_TIMEOUT || 3;
var AWS_DESCRIPTION = process.env.AWS_DESCRIPTION || '';
var AWS_RUNTIME = process.env.AWS_RUNTIME || 'nodejs';
var AWS_FUNCTION_VERSION = process.env.AWS_FUNCTION_VERSION || packageJson.version;

program
.version( lambda.version )
.command( "deploy" )
.description( "Deploy your application to Amazon Lambda" )
.option( "-e, --environment [" + AWS_ENVIRONMENT + "]", "Choose environment {development, stating, production}", 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( "-r, --region [" + AWS_REGION + "]", "AWS Region", AWS_REGION )
.option( "-n, --functionName [" + AWS_FUNCTION_NAME + "]", "Lambda FunctionName", AWS_FUNCTION_NAME )
.option( "-h, --handler [" + AWS_HANDLER + "]", "Lambda Handler {index.handler}", AWS_HANDLER )
.option( "-m, --mode [" + AWS_MODE + "]", "Lambda Mode", AWS_MODE )
.option( "-o, --role [" + AWS_ROLE + "]", "Amazon role", AWS_ROLE )
.option( "-m, --memorySize [" + AWS_MEMORY_SIZE + "]", "Lambda Memory Size", AWS_MEMORY_SIZE )
.option( "-t, --timeout [" + AWS_TIMEOUT + "]", "Lambda Timeout", AWS_TIMEOUT )
.option( "-d, --description [" + AWS_DESCRIPTION + "]", "Lambda Description", AWS_DESCRIPTION )
.option( "-u, --runtime [" + AWS_RUNTIME + "]", "Lambda Runtime", AWS_RUNTIME )
.command( 'deploy' )
.description( 'Deploy your application to Amazon Lambda' )
.option( '-e, --environment [' + AWS_ENVIRONMENT + ']', 'Choose environment {development, stating, production}', 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( '-r, --region [' + AWS_REGION + ']', 'AWS Region', AWS_REGION )
.option( '-n, --functionName [' + AWS_FUNCTION_NAME + ']', 'Lambda FunctionName', AWS_FUNCTION_NAME )
.option( '-h, --handler [' + AWS_HANDLER + ']', 'Lambda Handler {index.handler}', AWS_HANDLER )
.option( '-m, --mode [' + AWS_MODE + ']', 'Lambda Mode', AWS_MODE )
.option( '-o, --role [' + AWS_ROLE + ']', 'Amazon role', AWS_ROLE )
.option( '-m, --memorySize [' + AWS_MEMORY_SIZE + ']', 'Lambda Memory Size', AWS_MEMORY_SIZE )
.option( '-t, --timeout [' + AWS_TIMEOUT + ']', 'Lambda Timeout', AWS_TIMEOUT )
.option( '-d, --description [' + AWS_DESCRIPTION + ']', 'Lambda Description', AWS_DESCRIPTION )
.option( '-u, --runtime [' + AWS_RUNTIME + ']', 'Lambda Runtime', AWS_RUNTIME )
.option( '-v, --version [' + AWS_FUNCTION_VERSION + ']', 'Lambda Function Version', AWS_FUNCTION_VERSION )
.action( function( prg ) {
lambda.deploy( prg );
} );

program
.version( lambda.version )
.command( "run" )
.description( "Run your Amazon Lambda application locally" )
.option( "-h, --handler [" + AWS_HANDLER + "]", "Lambda Handler {index.handler}", AWS_HANDLER )
.command( 'run' )
.description( 'Run your Amazon Lambda application locally' )
.option( '-h, --handler [' + AWS_HANDLER + ']', 'Lambda Handler {index.handler}', AWS_HANDLER )
.action( function( prg ) {
lambda.run( prg );
} );

program
.version( lambda.version )
.command( "setup" )
.description( "Sets up the .env file." )
.command( 'setup' )
.description( 'Sets up the .env file.' )
.action( function( prg ) {
lambda.setup( );
} )



program.parse( process.argv );
176 changes: 83 additions & 93 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,72 @@
"use strict";

var
aws = require( "aws-sdk" ),
exec = require( "child_process" ).exec,
fs = require( "fs" ),
os = require( "os" ),
packageJson = require( "./../package.json" ),
path = require( "path" );

var Lambda = function( ) {
var aws = require('aws-sdk');
var exec = require('child_process').exec;
var fs = require('fs');
var os = require('os');
var packageJson = require('./../package.json');
var path = require('path');
var async = require('async');

var Lambda = function() {
this.version = packageJson.version;

return this;
};

Lambda.prototype._createEnvFile = function( ) {
var
envfile = process.cwd() + "/.env",
envexamplefile = __dirname + "/.env.example";

if ( !fs.existsSync( envfile ) ) {
console.log( ".env file successfully created" )
fs.writeFileSync( envfile, fs.readFileSync( envexamplefile) );
Lambda.prototype._createSampleFile = function(file) {
var exampleFile = process.cwd() + '/' + file;
var boilerplateFile = __dirname + '/' + file + '.example';

if (!fs.existsSync(exampleFile)) {
fs.writeFileSync(exampleFile, fs.readFileSync(boilerplateFile));
console.log(exampleFile + ' file successfully created');
}

};

Lambda.prototype._createEventJsonFile = function( ) {
var
eventfile = process.cwd() + "/event.json",
eventexamplefile = __dirname + "/event.json.example";

if ( !fs.existsSync( eventfile ) ) {
console.log( "event.json file successfully created" )
fs.writeFileSync( eventfile, fs.readFileSync( eventexamplefile ) );
}

Lambda.prototype.setup = function() {
console.log('Running setup.');
this._createSampleFile('.env');

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice refactor

this._createSampleFile('event.json');
console.log('Setup done. Edit the .env and event.json files as needed.');
};

Lambda.prototype.setup = function( ) {
console.log( "Running setup." );
this._createEnvFile( );
this._createEventJsonFile( );
console.log( "Setup done. Edit the .env and event.json files as needed." );
};
Lambda.prototype.run = function(program) {
this._createSampleFile('event.json');

Lambda.prototype.run = function( program ) {
this._createEventJsonFile( );
var dir = program.directory;
var splitHandler = program.handler.split('.');
var filename = splitHandler[0] + '.js';
var handlername = splitHandler[1];

var
dir = program.directory,
splitHandler = program.handler.split( "." ),
filename = splitHandler[0] + ".js",
handlername = splitHandler[1];
var handler = require(process.cwd() + '/' + filename)[handlername];
var event = require(process.cwd() + '/event.json');

var
handler = require( process.cwd() + "/" + filename )[handlername],
event = require( process.cwd() + "/event.json" );

this._runHandler( handler, event );
}
this._runHandler(handler, event);
};

Lambda.prototype._runHandler = function( handler, event ) {
Lambda.prototype._runHandler = function(handler, event) {
var context = {
done: function( ) {
done: function() {
process.exit(0);
}
}
};

handler( event, context );
handler(event, context);
};

Lambda.prototype._zipfileTmpPath = function( program ) {
var
ms_since_epoch = +new Date,
filename = program.functionName + "-" + ms_since_epoch + ".zip",
zipfile = path.join( os.tmpDir(), filename );
Lambda.prototype._zipfileTmpPath = function(program) {
var ms_since_epoch = +new Date();
var filename = program.functionName + '-' + ms_since_epoch + '.zip';
var zipfile = path.join(os.tmpDir(), filename);

return zipfile;
};

Lambda.prototype._params = function( program, buffer ) {
Lambda.prototype._params = function(program, buffer) {
var params = {
FunctionName: program.functionName + "-" + program.environment,
FunctionName: program.functionName + '-' + program.environment + (program.version ? '/' + program.version: ''),
FunctionZip: buffer,
Handler: program.handler,
Mode: program.mode,
Expand All @@ -96,46 +80,52 @@ Lambda.prototype._params = function( program, buffer ) {
return params;
};

Lambda.prototype.deploy = function( program ) {
this._createEnvFile( );

aws.config.update({
accessKeyId: program.accessKey,
secretAccessKey: program.secretKey,
region: program.region
});

var
_this = this,
lambda = new aws.Lambda( { apiVersion: "2014-11-11" } ),
zipfile = _this._zipfileTmpPath( program );
Lambda.prototype.deploy = function(program) {
this._createSampleFile('.env');

var _this = this;
var zipfile = _this._zipfileTmpPath(program);
var regions = program.regions.split(',');

// zip up but ignore .git, .dotfiles, .log files, and the event.json file.
console.log( "Generating zip file" );
exec( "zip -roq " + zipfile + " * -x '.git' -x '*/\.*' -x '*/\*.log' -x 'event.json'", function( err, stdout, stderr ) {
if ( err ) {
console.log('Generating zip file');
exec( "zip -roq " + zipfile + " * -x '.git' -x '*/\.*' -x '*/\*.log' -x 'event.json'", function(err, stdout, stderr) {
if (err) {
throw err;
}

console.log( "Reading zip file to memory" );
var
buffer = fs.readFileSync( zipfile ),
params = _this._params( program, buffer );

console.log( "Uploading zip file to AWS Lambda with parameters:" );
console.log( params );

lambda.uploadFunction( params, function( err, data ) {
if ( err ) {
console.log( err );
} else {
console.log( "Zip file done uploading. Results follow:" );
console.log( data );
console.log('Reading zip file to memory');
var buffer = fs.readFileSync(zipfile);
var params = _this._params(program, buffer);

async.each(regions, function(region, cb) {
console.log('Working in region: ' + region);
console.log('Uploading zip file to AWS Lambda with parameters:');
console.log(params);

aws.config.update({
accessKeyId: program.accessKey,
secretAccessKey: program.secretKey,
region: region
});

var lambda = new aws.Lambda({ apiVersion: '2014-11-11' });

lambda.uploadFunction(params, function(err, data) {
if (err) {
cb(err);
}
console.log('Zip file done uploading. Results follow:');
console.log(data);
cb();
});

}, function(err) {
if (err) {
console.log(err);
}
} );

} );
}

module.exports = new Lambda( );
});
});
};

module.exports = new Lambda();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-lambda",
"version": "0.1.5",
"version": "0.2.0",
"description": "Command line tool for locally running and remotely deploying your node.js applications to Amazon Lambda.",
"main": "lib/main.js",
"directories": {
Expand Down Expand Up @@ -34,6 +34,7 @@
"should": ""
},
"dependencies": {
"async": "^0.9.0",
"aws-sdk": "^2.0.28",
"commander": "^2.5.0",
"dotenv": "^0.4.0"
Expand Down
2 changes: 1 addition & 1 deletion test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe( "dotenv", function( ) {
} );

it( "version should be set", function( ) {
result.version.should.eql( "0.1.5" );
result.version.should.eql( "0.2.0" );
} );

describe( "_zipfileTmpPath", function( ) {
Expand Down