Skip to content

Commit

Permalink
feat(aws-stepfunctions): support StateMachineType
Browse files Browse the repository at this point in the history
Closes #5397
  • Loading branch information
wqzoww committed Dec 19, 2019
1 parent 93c2ddb commit 7a25665
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
35 changes: 35 additions & 0 deletions packages/@aws-cdk/aws-stepfunctions/lib/state-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -34,6 +53,13 @@ export interface StateMachineProps {
* @default No timeout
*/
readonly timeout?: Duration;

/**
* Type of the state machine
*
* @default StateMachineType.STANDARD
*/
readonly stateMachineType?: StateMachineType;
}

/**
Expand Down Expand Up @@ -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,
Expand All @@ -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()),
});
Expand Down
67 changes: 67 additions & 0 deletions packages/@aws-cdk/aws-stepfunctions/test/test.state-machine.ts
Original file line number Diff line number Diff line change
@@ -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();
}
};

0 comments on commit 7a25665

Please sign in to comment.