-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda-sandbox-run.js
More file actions
76 lines (57 loc) · 1.81 KB
/
lambda-sandbox-run.js
File metadata and controls
76 lines (57 loc) · 1.81 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
var awsEmulator = {
succeed: function(response){
console.log('AWS Lambda Function was executed without issues.');
console.log('Output: ', response);
},
fail: function(error){
console.log('AWS Lambda Function failed, reason:', error);
}
};
exports.aws = awsEmulator;
exports.run = function(lambdaName, handlerName, eventPayLoad){
var lambdaFunction, handler;
if( !lambdaName){
console.error('Lambda Function Name is required.');
console.error('Example to call mylambda.js:');
console.error('npm lambda-runner.js mylambda');
return null;
}
handlerName = handlerName || 'handler';
if( eventPayLoad ) {
eventPayLoad = parseEventPayLoad(eventPayLoad);
}else{
eventPayLoad = {};
}
try{
lambdaFunction = require(process.cwd() + '/' + lambdaName + '.js');
}catch(exception){
throw new Error('Invalid module: ' + lambdaName);
}
handler = lambdaFunction[handlerName];
if( !handler ){
console.error('Handler not found: ' + handlerName + ' on ' + lambdaName);
return null;
}
try{
handler(eventPayLoad, awsEmulator);
}catch(exception){
console.log(exception);
throw new Error('Uncaught exception running the lambdaFunction. ' + lambdaName + '.' + handlerName);
}
};
function parseEventPayLoad(eventPayLoad){
console.log(eventPayLoad);
try{
eventPayLoad = JSON.parse(eventPayLoad);
}catch(e){
var filePath = eventPayLoad;
if( filePath.indexOf('file://') === 0 ){
filePath = filePath.substring(7);
}
if( filePath.indexOf('/') !== 0 ){
filePath = process.cwd() + '/' + filePath;
}
eventPayLoad = require(filePath);
}
return eventPayLoad;
}