1 | "use strict";
|
2 |
|
3 | var
|
4 | aws = require( "aws-sdk" ),
|
5 | exec = require( "child_process" ).exec,
|
6 | fs = require( "fs" ),
|
7 | os = require( "os" ),
|
8 | packageJson = require( "./../package.json" ),
|
9 | path = require( "path" );
|
10 |
|
11 | var Lambda = function( ) {
|
12 | this.version = packageJson.version;
|
13 |
|
14 | return this;
|
15 | };
|
16 |
|
17 | Lambda.prototype._createEnvFile = function( ) {
|
18 | var
|
19 | envfile = process.cwd() + "/.env",
|
20 | envexamplefile = __dirname + "/.env.example";
|
21 |
|
22 | if ( !fs.existsSync( envfile ) ) {
|
23 | console.log( ".env file successfully created" )
|
24 | fs.writeFileSync( envfile, fs.readFileSync( envexamplefile) );
|
25 | }
|
26 |
|
27 | };
|
28 |
|
29 | Lambda.prototype._createEventJsonFile = function( ) {
|
30 | var
|
31 | eventfile = process.cwd() + "/event.json",
|
32 | eventexamplefile = __dirname + "/event.json.example";
|
33 |
|
34 | if ( !fs.existsSync( eventfile ) ) {
|
35 | console.log( "event.json file successfully created" )
|
36 | fs.writeFileSync( eventfile, fs.readFileSync( eventexamplefile ) );
|
37 | }
|
38 |
|
39 | };
|
40 |
|
41 | Lambda.prototype.setup = function( ) {
|
42 | console.log( "Running setup." );
|
43 | this._createEnvFile( );
|
44 | this._createEventJsonFile( );
|
45 | console.log( "Setup done. Edit the .env and event.json files as needed." );
|
46 | };
|
47 |
|
48 | Lambda.prototype.run = function( program ) {
|
49 | this._createEventJsonFile( );
|
50 |
|
51 | var
|
52 | dir = program.directory,
|
53 | splitHandler = program.handler.split( "." ),
|
54 | filename = splitHandler[0] + ".js",
|
55 | handlername = splitHandler[1];
|
56 |
|
57 | var
|
58 | handler = require( process.cwd() + "/" + filename )[handlername],
|
59 | event = require( process.cwd() + "/event.json" );
|
60 |
|
61 | this._runHandler( handler, event );
|
62 | }
|
63 |
|
64 | Lambda.prototype._runHandler = function( handler, event ) {
|
65 | var context = {
|
66 | done: function( ) {
|
67 | process.exit(0);
|
68 | }
|
69 | }
|
70 |
|
71 | handler( event, context );
|
72 | };
|
73 |
|
74 | Lambda.prototype._zipfileTmpPath = function( program ) {
|
75 | var
|
76 | ms_since_epoch = +new Date,
|
77 | filename = program.functionName + "-" + ms_since_epoch + ".zip",
|
78 | zipfile = path.join( os.tmpDir(), filename );
|
79 |
|
80 | return zipfile;
|
81 | };
|
82 |
|
83 | Lambda.prototype._params = function( program, buffer ) {
|
84 | var params = {
|
85 | FunctionName: program.functionName + "-" + program.environment,
|
86 | FunctionZip: buffer,
|
87 | Handler: program.handler,
|
88 | Mode: program.mode,
|
89 | Role: program.role,
|
90 | Runtime: program.runtime,
|
91 | Description: program.description,
|
92 | MemorySize: program.memorySize,
|
93 | Timeout: program.timeout
|
94 | };
|
95 |
|
96 | return params;
|
97 | };
|
98 |
|
99 | Lambda.prototype.deploy = function( program ) {
|
100 | this._createEnvFile( );
|
101 |
|
102 | aws.config.update({
|
103 | accessKeyId: program.accessKey,
|
104 | secretAccessKey: program.secretKey,
|
105 | region: program.region
|
106 | });
|
107 |
|
108 | var
|
109 | _this = this,
|
110 | lambda = new aws.Lambda( { apiVersion: "2014-11-11" } ),
|
111 | zipfile = _this._zipfileTmpPath( program );
|
112 |
|
113 |
|
114 | console.log( "Generating zip file" );
|
115 | exec( "zip -roq " + zipfile + " * -x '.git' -x '*/\.*' -x '*/\*.log' -x 'event.json'", function( err, stdout, stderr ) {
|
116 | if ( err ) {
|
117 | throw err;
|
118 | }
|
119 |
|
120 | console.log( "Reading zip file to memory" );
|
121 | var
|
122 | buffer = fs.readFileSync( zipfile ),
|
123 | params = _this._params( program, buffer );
|
124 |
|
125 | console.log( "Uploading zip file to AWS Lambda with parameters:" );
|
126 | console.log( params );
|
127 |
|
128 | lambda.uploadFunction( params, function( err, data ) {
|
129 | if ( err ) {
|
130 | console.log( err );
|
131 | } else {
|
132 | console.log( "Zip file done uploading. Results follow:" );
|
133 | console.log( data );
|
134 | }
|
135 | } );
|
136 |
|
137 | } );
|
138 | }
|
139 |
|
140 | module.exports = new Lambda( );
|
141 |
|