/
execution.js
42 lines (38 loc) · 1.55 KB
/
execution.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
const AWS = require('aws-sdk');
const { pollExecutionHistory } = require('./history');
// AWS SDK - StepFunctions
const stepfunctions = new AWS.StepFunctions();
// Environment Variable - ARN of the Step Functions State Machine
const STATE_MACHINE_ARN = process.env.STATE_MACHINE_ARN || 'ARN';
/**
* Starts an execution of an AWS Step Functions State Machine. Passes the
* inputObj parameter to the execution.
* @param {*} inputObj Input to the state machine execution.
*/
const startExecution = async (inputObj = {}) => {
console.log(`startExecution`);
const input = JSON.stringify(inputObj);
const params = {
stateMachineArn: STATE_MACHINE_ARN,
input
};
const execution = await stepfunctions.startExecution(params).promise();
console.log(`execution:\n${JSON.stringify(execution, null, 2)}`);
return execution;
};
exports.startExecution = startExecution;
/**
* Starts an execution of an AWS Step Functions State Machine. Passes the
* inputObj parameter to the execution. Polls the event history of the
* execution for completion. Returns the output from the final event in
* the execution.
* @param {*} inputObj Input to the state machine execution.
* @returns Output from the state machine execution as JSON.
*/
const startExecutionAndWait = async (inputObj = {}) => {
console.log(`startExecutionAndWait`);
const execution = await startExecution(inputObj);
const event = await pollExecutionHistory(execution.executionArn);
return JSON.parse(event.stateExitedEventDetails.output);
};
exports.startExecutionAndWait = startExecutionAndWait;