forked from nordcloud/lambda-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
82 lines (67 loc) · 1.98 KB
/
index.js
File metadata and controls
82 lines (67 loc) · 1.98 KB
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
// Wrapper class for AWS Lambda
function Wrapped(mod) {
this.lambdaModule = mod;
}
Wrapped.prototype.run = function(event, callback, customContext) {
var lambdacontext = Object.assign(customContext || {}, {
succeed: function(success) {
return callback(null, success);
},
fail: function(error) {
return callback(error, null);
},
done: function(error, success) {
return callback(error, success);
}
});
try {
if (this.lambdaModule.handler) {
this.lambdaModule.handler(event, lambdacontext, callback);
} else {
var AWS = require('aws-sdk');
if (this.lambdaModule.region) {
AWS.config.update({
region: this.lambdaModule.region
});
}
var lambda = new AWS.Lambda();
var params = {
FunctionName: this.lambdaModule.lambdaFunction,
InvocationType: 'RequestResponse',
LogType: 'None',
Payload: JSON.stringify(event),
};
lambda.invoke(params, function(err, data) {
if (err) {
return callback(err);
}
callback(null, JSON.parse(data.Payload));
});
}
} catch (ex) {
throw(ex);
}
};
// Wrapper factory
function wrap(mod) {
var wrapped = new Wrapped(mod);
return wrapped;
}
// Static variables (for backwards compatibility)
var latest;
// Public interface for the module
module.exports = exports = {
// reusable wrap method
wrap: wrap,
// static init/run interface for backwards compatibility
init: function(mod) {
latest = wrap(mod);
},
run: function(event, callback) {
if (typeof latest === typeof undefined) {
return callback('Module not initialized', null);
} else {
latest.run(event, callback);
}
}
};