-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
178 lines (155 loc) · 4.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var gutil = require('gulp-util');
var zip = require('gulp-zip');
var del = require('del');
var install = require('gulp-install');
var runSequence = require('run-sequence');
var AWS = require('aws-sdk');
var rename = require("gulp-rename");
var fs = require('fs');
module.exports = function(gulp, opts) {
if (!gulp) {
gulp = require('gulp');
runSequence.use(gulp);
}
if (!opts){
opts = {};
}
if (!opts.configFile) {
opts.configFile = 'lambda-config.js';
}
var config = {};
try {
config = require(process.cwd() + "/" + opts.configFile);
} catch(err) {}
var packageFile = opts.packageFile || 'package.json';
var environment = config.Environment || process.env.NODE_ENV || opts.environment || ''
gulp.task('clean', function(cb) {
del(['./dist', './dist.zip'], cb);
});
gulp.task('js', function() {
var file = config.Handler ? config.Handler.split('.')[0] : 'index';
return gulp.src(file + '.js')
.pipe(gulp.dest('dist/'));
});
gulp.task('bin', function() {
return gulp.src('./bin/*')
.pipe(gulp.dest('dist/'));
});
gulp.task('env', function() {
var env = '.env';
if (environment) {
env += '.' + environment;
}
return gulp.src('./' + env)
.pipe(rename('.env'))
.pipe(gulp.dest('dist/'));
});
gulp.task('lib', function() {
return gulp.src('./lib/*')
.pipe(gulp.dest('dist/lib/'));
});
gulp.task('node-mods', function() {
return gulp.src('./' + packageFile)
.pipe(rename('package.json'))
.pipe(gulp.dest('dist/'))
.pipe(install({production: true, ignoreScripts: opts.ignoreScripts}));
});
gulp.task('zip', function() {
return gulp.src(['dist/**/*','dist/.env','!dist/' + packageFile])
.pipe(zip('dist.zip'))
.pipe(gulp.dest('./'));
});
gulp.task('lambda-zip', function(callback) {
return runSequence(
['clean'],
['js', 'lib', 'bin', 'node-mods', 'env'],
['zip'],
callback
);
});
gulp.task('lambda-zip-quick', function(callback) {
return runSequence(
['js', 'lib', 'bin', 'env'],
['zip'],
callback
);
});
gulp.task('upload', function(callback) {
try {
var config = require(process.cwd() + "/" + opts.configFile);
} catch(err) {
gutil.log("lambduh-gulp upload requires a ./lambda-config.js file to return a js object");
throw err;
}
AWS.config.region = config.Region;
var lambda = new AWS.Lambda();
var params = {
FunctionName: config.FunctionName,
Description: config.Description,
Handler: config.Handler,
Role: config.Role,
Runtime: config.Runtime,
MemorySize: config.MemorySize,
Timeout: config.Timeout
};
return fs.readFile('./dist.zip', function(err, zip) {
params.Code = { ZipFile: zip };
lambda.createFunction(params, function(err, data) {
if (err && err.statusCode == 409) {//function already exists
console.log("Function already exists, updating....");
lambda.updateFunctionConfiguration({
FunctionName: params.FunctionName,
Description: params.Description,
Handler: params.Handler,
MemorySize: params.MemorySize,
Role: params.Role,
Timeout: params.Timeout
}, function(err, data){
if(err) {
var warning = 'Fail while Updating Function Configuration';
gutil.log(warning);
//TODO: think about trying to update the code even if the config fails
callback(err);
} else {
console.log("Successful function configuration update.");
lambda.updateFunctionCode({
FunctionName: params.FunctionName,
ZipFile: params.Code.ZipFile
}, function(err, data) {
if(err) {
var warning = 'Fail while Updating Function Code';
gutil.log(warning);
callback(err);
} else {
console.log("Successful function upload");
callback();
}
});
}
});
} else if(err) {
var warning = 'Fail while Creating Lambda Function';
gutil.log(warning);
callback(err);
} else {
console.log('Successfully created new lambda function');
callback();
}
});
});
});
gulp.task('zipload-quick', function(callback) {
return runSequence(
['lambda-zip-quick'],
['upload'],
callback
);
});
gulp.task('zipload', function(callback) {
return runSequence(
['lambda-zip'],
['upload'],
callback
);
});
};