From 7a25665a22db84bd3032a717a10a66bac56d7928 Mon Sep 17 00:00:00 2001 From: Wenqian Wang Date: Thu, 12 Dec 2019 14:04:09 -0800 Subject: [PATCH] feat(aws-stepfunctions): support StateMachineType Closes #5397 --- .../aws-stepfunctions/lib/state-machine.ts | 35 ++++++++++ .../test/test.state-machine.ts | 67 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 packages/@aws-cdk/aws-stepfunctions/test/test.state-machine.ts diff --git a/packages/@aws-cdk/aws-stepfunctions/lib/state-machine.ts b/packages/@aws-cdk/aws-stepfunctions/lib/state-machine.ts index 0fd3204a101a6..980fd1266d22f 100644 --- a/packages/@aws-cdk/aws-stepfunctions/lib/state-machine.ts +++ b/packages/@aws-cdk/aws-stepfunctions/lib/state-machine.ts @@ -5,6 +5,25 @@ import { StateGraph } from './state-graph'; import { CfnStateMachine } from './stepfunctions.generated'; import { IChainable } from './types'; +/** + * Two types of state machines are available in AWS Step Functions: EXPRESS AND STANDARD. + * + * @see https://docs.aws.amazon.com/step-functions/latest/dg/concepts-standard-vs-express.html + * + * @default STANDARD + */ +export enum StateMachineType { + /** + * Express Workflows are ideal for high-volume, event processing workloads. + */ + EXPRESS = 'EXPRESS', + + /** + * Standard Workflows are ideal for long-running, durable, and auditable workflows. + */ + STANDARD = 'STANDARD' +} + /** * Properties for defining a State Machine */ @@ -34,6 +53,13 @@ export interface StateMachineProps { * @default No timeout */ readonly timeout?: Duration; + + /** + * Type of the state machine + * + * @default StateMachineType.STANDARD + */ + readonly stateMachineType?: StateMachineType; } /** @@ -86,6 +112,12 @@ export class StateMachine extends StateMachineBase { */ public readonly stateMachineArn: string; + /** + * Type of the state machine + * @attribute + */ + public readonly stateMachineType: StateMachineType; + constructor(scope: Construct, id: string, props: StateMachineProps) { super(scope, id, { physicalName: props.stateMachineName, @@ -98,8 +130,11 @@ export class StateMachine extends StateMachineBase { const graph = new StateGraph(props.definition.startState, `State Machine ${id} definition`); graph.timeout = props.timeout; + this.stateMachineType = props.stateMachineType ? props.stateMachineType : StateMachineType.STANDARD; + const resource = new CfnStateMachine(this, 'Resource', { stateMachineName: this.physicalName, + stateMachineType: props.stateMachineType ? props.stateMachineType : undefined, roleArn: this.role.roleArn, definitionString: Stack.of(this).toJsonString(graph.toGraphJson()), }); diff --git a/packages/@aws-cdk/aws-stepfunctions/test/test.state-machine.ts b/packages/@aws-cdk/aws-stepfunctions/test/test.state-machine.ts new file mode 100644 index 0000000000000..2aab5ea1070d8 --- /dev/null +++ b/packages/@aws-cdk/aws-stepfunctions/test/test.state-machine.ts @@ -0,0 +1,67 @@ +import { expect, haveResource } from '@aws-cdk/assert'; +import * as cdk from '@aws-cdk/core'; +import { Test } from 'nodeunit'; +import * as stepfunctions from '../lib'; + +export = { + 'Instantiate Default State Machine'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + + // WHEN + new stepfunctions.StateMachine(stack, 'MyStateMachine', { + stateMachineName: "MyStateMachine", + definition: stepfunctions.Chain.start(new stepfunctions.Pass(stack, 'Pass')) + }); + + // THEN + expect(stack).to(haveResource('AWS::StepFunctions::StateMachine', { + StateMachineName: "MyStateMachine", + DefinitionString: "{\"StartAt\":\"Pass\",\"States\":{\"Pass\":{\"Type\":\"Pass\",\"End\":true}}}" + })); + + test.done(); + }, + + 'Instantiate Standard State Machine'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + + // WHEN + new stepfunctions.StateMachine(stack, 'MyStateMachine', { + stateMachineName: "MyStateMachine", + definition: stepfunctions.Chain.start(new stepfunctions.Pass(stack, 'Pass')), + stateMachineType: stepfunctions.StateMachineType.STANDARD + }); + + // THEN + expect(stack).to(haveResource('AWS::StepFunctions::StateMachine', { + StateMachineName: "MyStateMachine", + StateMachineType: "STANDARD", + DefinitionString: "{\"StartAt\":\"Pass\",\"States\":{\"Pass\":{\"Type\":\"Pass\",\"End\":true}}}" + })); + + test.done(); + }, + + 'Instantiate Express State Machine'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + + // WHEN + new stepfunctions.StateMachine(stack, 'MyStateMachine', { + stateMachineName: "MyStateMachine", + definition: stepfunctions.Chain.start(new stepfunctions.Pass(stack, 'Pass')), + stateMachineType: stepfunctions.StateMachineType.EXPRESS + }); + + // THEN + expect(stack).to(haveResource('AWS::StepFunctions::StateMachine', { + StateMachineName: "MyStateMachine", + StateMachineType: "EXPRESS", + DefinitionString: "{\"StartAt\":\"Pass\",\"States\":{\"Pass\":{\"Type\":\"Pass\",\"End\":true}}}" + })); + + test.done(); + } +};