forked from mavi888/stepfunction-triggers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
76 lines (58 loc) · 1.95 KB
/
Copy pathhandler.js
File metadata and controls
76 lines (58 loc) · 1.95 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
'use strict';
const AWS = require('aws-sdk');
const stepfunctions = new AWS.StepFunctions();
module.exports.executeStepFunction = (event, context, callback) => {
console.log('executeStepFunction');
const number = event.queryStringParameters.number;
console.log(number);
callStepFunction(number).then(result => {
let message = 'Step function is executing';
if (!result) {
message = 'Step function is not executing';
}
const response = {
statusCode: 200,
body: JSON.stringify({ message })
};
callback(null, response);
});
};
module.exports.calculateRandomNumber = (event, context, callback) => {
console.log('calculateRandomNumber was called');
let result = event.number;
console.log(result);
callback(null, { result });
};
module.exports.moreCalculations = (event, context, callback) => {
console.log('moreCalculations was called');
console.log(event);
callback(null, null);
};
function callStepFunction(number) {
console.log('callStepFunction');
const stateMachineName = 'TestingStateMachine'; // The name of the step function we defined in the serverless.yml
console.log('Fetching the list of available workflows');
return stepfunctions
.listStateMachines({})
.promise()
.then(listStateMachines => {
console.log('Searching for the step function', listStateMachines);
for (var i = 0; i < listStateMachines.stateMachines.length; i++) {
const item = listStateMachines.stateMachines[i];
if (item.name.indexOf(stateMachineName) >= 0) {
console.log('Found the step function', item);
var params = {
stateMachineArn: item.stateMachineArn,
input: JSON.stringify({ number: number })
};
console.log('Start execution');
return stepfunctions.startExecution(params).promise().then(() => {
return true;
});
}
}
})
.catch(error => {
return false;
});
}