Skip to content

Instantly share code, notes, and snippets.

@tpschmidt
Last active June 12, 2021 09:11
Show Gist options
  • Select an option

  • Save tpschmidt/3d5d3a2178e5978198d573ec3ecbf67c to your computer and use it in GitHub Desktop.

Select an option

Save tpschmidt/3d5d3a2178e5978198d573ec3ecbf67c to your computer and use it in GitHub Desktop.
Step Functions: Starting a State Machine via AWS SDK
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