-
Notifications
You must be signed in to change notification settings - Fork 203
/
gulpfile.js
164 lines (143 loc) · 3.96 KB
/
gulpfile.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
var AWS = require('aws-sdk');
var gulp = require('gulp');
var zip = require('gulp-zip');
var install = require('gulp-install');
var runSequence = require('run-sequence');
var fs = require('fs');
var packageJson = require('./package.json');
//constants
var region = 'eu-west-1';
var functionName = 'LambdaTest';
var outputName = 'LambdaTest.zip';
var IAMRole = 'arn:aws:iam::655240720487:role/lambda_basic_execution';
var filesToPack = ['./lambda-testing/functions/LambdaTest.js'];
/**
* Adds the project files to the archive folder.
*/
gulp.task('js', function () {
return gulp.src(filesToPack, {base: './lambda-testing/functions'})
.pipe(gulp.dest('dist/'));
});
/**
* This task will copy all the required dependencies to
* the dist folder.
*/
gulp.task('node-mods', function () {
return gulp.src('./package.json')
.pipe(gulp.dest('dist/'))
.pipe(install({production: true}));
});
/**
* Create an archive based on the dest folder.
*/
gulp.task('zip', function () {
return gulp.src(['dist/**', '!dist/package.json'])
.pipe(zip(outputName))
.pipe(gulp.dest('./'));
});
/**
* Upload deployment package to S3 (lambda function file + dependencies)
*/
gulp.task('upload-to-s3', function () {
var s3 = new AWS.S3();
var zipFilePath = './' + outputName;
getZipFile(function (data) {
var params = {
Bucket: 'lambda-function-container',
Key: zipFilePath,
Body: data
};
s3.putObject(params, function(err, data) {
if (err) console.log('Object upload unsuccessful!');
else console.log('Object ' + outputName + ' was uploaded!');
});
});
function getZipFile (next) {
fs.readFile(zipFilePath, function (err, data) {
if (err) console.log(err);
else {
next(data);
}
});
}
});
/**
* update or create the lambda functon
*/
gulp.task('upload', function() {
AWS.config.region = region;
var lambda = new AWS.Lambda();
var s3 = new AWS.S3();
var zipFile = './' + outputName;
var bucketName = 'lambda-function-container';
lambda.getFunction({ FunctionName: functionName }, function(err, data) {
if (err) checkObject(createFunction);
else checkObject(updateFunction);
});
function checkObject (fn) {
var params = {
Bucket: bucketName,
Key: zipFile
};
s3.getObject(params, function (err, data) {
if (err) console.log('BUCKET ERROR', err);
else fn();
});
}
function createFunction () {
var params = {
Code: {
S3Bucket: bucketName,
S3Key: zipFile
},
FunctionName: functionName,
Handler: 'LambdaTest.handler',
Role: IAMRole,
Runtime: 'nodejs'
};
lambda.createFunction (params, function (err, data) {
if (err) console.error("CREATE ERROR", err);
else console.log('Function ' + functionName + ' has been created.');
});
}
function updateFunction () {
var params = {
FunctionName: functionName,
S3Bucket: bucketName,
S3Key: zipFile
};
lambda.updateFunctionCode(params, function(err, data) {
if (err) console.error(err);
else console.log('Function ' + functionName + ' has been updated.');
});
}
});
gulp.task('test-invoke', function() {
var lambda = new AWS.Lambda();
var params = {
FunctionName: functionName,
InvocationType: 'RequestResponse',
LogType: 'Tail',
Payload: '{ "key1" : "name" }'
};
lambda.getFunction({ FunctionName: functionName }, function(err, data) {
if (err) console.log("FUNCTION NOT FOUND", err);
else invokeFunction();
});
function invokeFunction() {
lambda.invoke(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
})
}
})
gulp.task('deploy', function (callback) {
return runSequence(
['js', 'node-mods'],
['zip'],
['upload-to-s3'],
['upload'],
['test-invoke']
callback
);
});