forked from ThoughtWorksStudios/node-aws-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
150 lines (136 loc) · 4.24 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
var fs = require('fs');
var AWS = require('aws-sdk');
var extend = require('util')._extend;
var async = require('async');
exports.deploy = function(codePackage, config, callback, logger, lambda) {
if (!logger) {
logger = console.log;
}
if(!lambda) {
if("profile" in config) {
var credentials = new AWS.SharedIniFileCredentials({profile: config.profile});
AWS.config.credentials = credentials;
}
if (process.env.HTTPS_PROXY) {
if (!AWS.config.httpOptions) {
AWS.config.httpOptions = {};
}
var HttpsProxyAgent = require('https-proxy-agent');
AWS.config.httpOptions.agent = new HttpsProxyAgent(process.env.HTTPS_PROXY);
}
lambda = new AWS.Lambda({
region: config.region,
accessKeyId: "accessKeyId" in config ? config.accessKeyId : "",
secretAccessKey: "secretAccessKey" in config ? config.secretAccessKey : ""
});
}
var params = {
FunctionName: config.functionName,
Description: config.description,
Handler: config.handler,
Role: config.role,
Timeout: config.timeout,
MemorySize: config.memorySize
};
var updateEventSource = function(callback) {
if(!config.eventSource) {
callback();
return;
}
var params = extend({
FunctionName: config.functionName
}, config.eventSource);
lambda.listEventSourceMappings({
FunctionName: params.FunctionName,
EventSourceArn: params.EventSourceArn
}, function(err, data) {
if(err) {
logger("List event source mapping failed, please make sure you have permission");
callback(err);
} else {
if (data.EventSourceMappings.length === 0) {
lambda.createEventSourceMapping(params, function(err, data) {
if(err) {
logger("Failed to create event source mapping!");
callback(err);
} else {
callback();
}
});
} else {
async.eachSeries(data.EventSourceMappings, function(mapping, iteratorCallback) {
lambda.updateEventSourceMapping({
UUID: mapping.UUID,
BatchSize: params.BatchSize
}, iteratorCallback);
}, function(err) {
if(err) {
logger("Update event source mapping failed");
callback(err);
} else {
callback();
}
});
}
}
});
};
var updateFunction = function(callback) {
fs.readFile(codePackage, function(err, data) {
if(err) {
return callback('Error reading specified package "'+ codePackage + '"');
}
lambda.updateFunctionCode({FunctionName: params.FunctionName, ZipFile: data}, function(err, data) {
if (err) {
var warning = 'Package upload failed. '
warning += 'Check your iam:PassRole permissions.'
gutil.log(warning);
callback(err)
} else {
lambda.updateFunctionConfiguration(params, function(err, data) {
if (err) {
var warning = 'Update function configuration failed. '
logger(warning);
callback(err);
} else {
updateEventSource(callback);
}
});
}
});
});
};
var createFunction = function(callback) {
fs.readFile(codePackage, function(err, data) {
if(err) {
return callback('Error reading specified package "'+ codePackage + '"');
}
params['Code'] = { ZipFile: data };
params['Runtime'] = "nodejs";
lambda.createFunction(params, function(err, data) {
if (err) {
var warning = 'Create function failed. '
warning += 'Check your iam:PassRole permissions.'
logger(warning);
callback(err)
} else {
updateEventSource(callback);
}
});
});
};
lambda.getFunction({FunctionName: params.FunctionName}, function(err, data) {
if (err) {
if (err.statusCode === 404) {
createFunction(callback);
} else {
var warning = 'AWS API request failed. '
warning += 'Check your AWS credentials and permissions.'
logger(warning);
callback(err);
}
} else {
updateFunction(callback);
}
});
};