Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aws-stepfunctions): support StateMachineType #5398

Merged
merged 1 commit into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the default in CloudFormation perhaps STANDARD? I think so, no? If so, I'd rather we default to undefined here, so that the churn on the existing CloudFormation templates is minimal.

The @default annotation on the stateMachineType property should STILL indicate that the default behavior is STANDARD. @default annotations concern themselves with behavior of the stack as a whole, not with the details of what is rendered to a CFN temlate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your feedback.

In my latest commit, the member variable stateMachineType is set to STANDARD by default (line 133), while the parameter stateMachineType in the cloudformation resource keeps undefined if users do not specify this optional field.

This solution helps avoid breaking the existing integration tests. :)


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 = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also added unit tests to cover different scenarios regarding stateMachineType.

'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();
}
};