Last active
June 12, 2021 09:11
-
-
Save tpschmidt/3d5d3a2178e5978198d573ec3ecbf67c to your computer and use it in GitHub Desktop.
Step Functions: Starting a State Machine via AWS SDK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import AWS, { StepFunctions } from 'aws-sdk' | |
| import { v4 } from 'uuid' | |
| import { StartExecutionInput, StartExecutionOutput } from 'aws-sdk/clients/stepfunctions' | |
| interface StateMachineSubmission { | |
| customerId: string | |
| traceId: string | |
| } | |
| export class StateMachineService { | |
| private client: StepFunctions | |
| constructor() { | |
| this.client = new AWS.StepFunctions() | |
| } | |
| private async startExecution(customerId: string, traceId: string) { | |
| const payload: StateMachineSubmission = { customerId, traceId } | |
| const params: StartExecutionInput = { | |
| stateMachineArn: process.env.STATE_MACHINE_ARN, | |
| input: JSON.stringify(payload), | |
| name: `${customerId}_${v4().toString()}`, | |
| } | |
| const response = await this.client.startExecution(params).promise() | |
| .catch((err) => { | |
| console.err(`State machine could not be started [err=${err}, customerId=${customerId}]`) | |
| })) as StartExecutionOutput | |
| if (response) { | |
| console.log(`StateMachine execution has been started [date=${response.startDate}, arn=${response.executionArn}`) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment