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(stepfunctions): adding custom state name prop #27306

Merged
merged 8 commits into from
Oct 11, 2023
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
2 changes: 2 additions & 0 deletions packages/aws-cdk-lib/aws-stepfunctions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ becomes new the new Data object. This behavior can be modified by supplying valu

These properties impact how each individual step interacts with the state machine data:

* `stateName`: the name of the state in the state machine definition. If not supplied, defaults to the construct id.
* `inputPath`: the part of the data object that gets passed to the step (`itemsPath` for `Map` states)
* `resultSelector`: the part of the step result that should be added to the state machine data
* `resultPath`: where in the state machine data the step result should be inserted
Expand Down Expand Up @@ -285,6 +286,7 @@ and also injects a field called `otherData`.

```ts
const pass = new sfn.Pass(this, 'Filter input and inject data', {
stateName: 'my-pass-state', // the custom state name for the Pass state, defaults to 'Filter input and inject data' as the state name
parameters: { // input to the pass state
input: sfn.JsonPath.stringAt('$.input.greeting'),
otherData: 'some-extra-stuff',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export interface StateMachineProps {
readonly definitionBody?: DefinitionBody;

/**
* substitutions for the definition body aas a key-value map
* substitutions for the definition body as a key-value map
*/
readonly definitionSubstitutions?: { [key: string]: string };

Expand Down
7 changes: 7 additions & 0 deletions packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import { IChainable, INextable } from '../types';
* Properties for defining a Choice state
*/
export interface ChoiceProps {
/**
* Optional name for this state
*
* @default - The construct ID will be used as state name
*/
readonly stateName?: string;

/**
* An optional description for this state
*
Expand Down
7 changes: 7 additions & 0 deletions packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import { INextable } from '../types';
* Properties for defining a Fail state
*/
export interface FailProps {
/**
* Optional name for this state
*
* @default - The construct ID will be used as state name
*/
readonly stateName?: string;

/**
* An optional description for this state
*
Expand Down
7 changes: 7 additions & 0 deletions packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import { CatchProps, IChainable, INextable, RetryProps } from '../types';
* Properties for defining a Map state
*/
export interface MapProps {
/**
* Optional name for this state
*
* @default - The construct ID will be used as state name
*/
readonly stateName?: string;

/**
* An optional description for this state
*
Expand Down
7 changes: 7 additions & 0 deletions packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import { CatchProps, IChainable, INextable, RetryProps } from '../types';
* Properties for defining a Parallel state
*/
export interface ParallelProps {
/**
* Optional name for this state
*
* @default - The construct ID will be used as state name
*/
readonly stateName?: string;

/**
* An optional description for this state
*
Expand Down
7 changes: 7 additions & 0 deletions packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ export class Result {
* Properties for defining a Pass state
*/
export interface PassProps {
/**
* Optional name for this state
*
* @default - The construct ID will be used as state name
*/
readonly stateName?: string;

/**
* An optional description for this state
*
Expand Down
11 changes: 10 additions & 1 deletion packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import { CatchProps, Errors, IChainable, INextable, RetryProps } from '../types'
* Properties shared by all states
*/
export interface StateProps {
/**
* Optional name for this state
*
* @default - The construct ID will be used as state name
*/
readonly stateName?: string;

/**
* A comment describing this state
*
Expand Down Expand Up @@ -155,6 +162,7 @@ export abstract class State extends Construct implements IChainable {
// features are shared by a couple of states, and it becomes cumbersome to
// slice it out across all states. This is not great design, but it is
// pragmatic!
protected readonly stateName?: string;
protected readonly comment?: string;
protected readonly inputPath?: string;
protected readonly parameters?: object;
Expand Down Expand Up @@ -194,6 +202,7 @@ export abstract class State extends Construct implements IChainable {

this.startState = this;

this.stateName = props.stateName;
this.comment = props.comment;
this.inputPath = props.inputPath;
this.parameters = props.parameters;
Expand All @@ -219,7 +228,7 @@ export abstract class State extends Construct implements IChainable {
* Tokenized string that evaluates to the state's ID
*/
public get stateId(): string {
return this.prefixes.concat(this.id).join('');
return this.prefixes.concat(this.stateName? this.stateName: this.id).join('');
}

/**
Expand Down
7 changes: 7 additions & 0 deletions packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import { INextable } from '../types';
* Properties for defining a Succeed state
*/
export interface SucceedProps {
/**
* Optional name for this state
*
* @default - The construct ID will be used as state name
*/
readonly stateName?: string;

/**
* An optional description for this state
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import { CatchProps, IChainable, INextable, RetryProps } from '../types';
* Props that are common to all tasks
*/
export interface TaskStateBaseProps {
/**
* Optional name for this state
*
* @default - The construct ID will be used as state name
*/
readonly stateName?: string;

/**
* An optional description for this state
*
Expand Down
7 changes: 7 additions & 0 deletions packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ export interface TaskProps {
*/
readonly task: IStepFunctionsTask;

/**
* Optional name for this state
*
* @default - The construct ID will be used as state name
*/
readonly stateName?: string;

/**
* An optional description for this state
*
Expand Down
7 changes: 7 additions & 0 deletions packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ export class WaitTime {
* Properties for defining a Wait state
*/
export interface WaitProps {
/**
* Optional name for this state
*
* @default - The construct ID will be used as state name
*/
readonly stateName?: string;

/**
* An optional description for this state
*
Expand Down
10 changes: 6 additions & 4 deletions packages/aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ describe('Custom State', () => {
// WHEN
const definition = new sfn.CustomState(stack, 'Custom', {
stateJson,
}).next(new sfn.Pass(stack, 'MyPass'));
}).next(new sfn.Pass(stack, 'MyPass', {
stateName: 'my-pass-state',
}));

// THEN
expect(render(stack, definition)).toStrictEqual(
{
StartAt: 'Custom',
States: {
Custom: {
Next: 'MyPass',
'Custom': {
Next: 'my-pass-state',
Type: 'Task',
Resource: 'arn:aws:states:::dynamodb:putItem',
Parameters: {
Expand All @@ -62,7 +64,7 @@ describe('Custom State', () => {
},
ResultPath: null,
},
MyPass: {
'my-pass-state': {
Type: 'Pass',
End: true,
},
Expand Down
61 changes: 59 additions & 2 deletions packages/aws-cdk-lib/aws-stepfunctions/test/fail.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,67 @@
import { render } from './private/render-util';
import * as cdk from '../../core';
import * as stepfunctions from '../lib';

describe('Fail State', () => {
test('Props are optional', () => {
const stack = new cdk.Stack();
let stack: cdk.Stack;
let stateJson: any;

beforeEach(() => {
// GIVEN
stack = new cdk.Stack();
stateJson = {
Type: 'Task',
Resource: 'arn:aws:states:::dynamodb:putItem',
Parameters: {
TableName: 'MyTable',
Item: {
id: {
S: 'MyEntry',
},
},
},
ResultPath: null,
};
});

test('Props are optional', () => {
new stepfunctions.Fail(stack, 'Fail');
});

test('can add a fail state to the chain with custom state name', () => {
// WHEN
const definition = new stepfunctions.CustomState(stack, 'Custom1', {
stateJson,
}).next(new stepfunctions.Pass(stack, 'MyPass'))
.next(new stepfunctions.Fail(stack, 'Fail', {
stateName: 'my-fail-state',
comment: 'failing state',
errorPath: stepfunctions.JsonPath.stringAt('$.error'),
causePath: stepfunctions.JsonPath.stringAt('$.cause'),
}));

// THEN
expect(render(stack, definition)).toStrictEqual(
{
StartAt: 'Custom1',
States: {
'Custom1': {
Next: 'MyPass',
Type: 'Task',
...stateJson,
},
'MyPass': {
Type: 'Pass',
Next: 'my-fail-state',
},
'my-fail-state': {
Comment: 'failing state',
Type: 'Fail',
CausePath: '$.cause',
ErrorPath: '$.error',
},
},
},
);
});
});
5 changes: 3 additions & 2 deletions packages/aws-cdk-lib/aws-stepfunctions/test/map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('Map State', () => {

// WHEN
const map = new stepfunctions.Map(stack, 'Map State', {
stateName: 'My-Map-State',
maxConcurrency: 1,
itemsPath: stepfunctions.JsonPath.stringAt('$.inputForMap'),
parameters: {
Expand All @@ -19,9 +20,9 @@ describe('Map State', () => {

// THEN
expect(render(map)).toStrictEqual({
StartAt: 'Map State',
StartAt: 'My-Map-State',
States: {
'Map State': {
'My-Map-State': {
Type: 'Map',
End: true,
Parameters: {
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk-lib/aws-stepfunctions/test/parallel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Parallel State', () => {

// WHEN
const parallel = new stepfunctions.Parallel(stack, 'Parallel State');
parallel.branch(new stepfunctions.Pass(stack, 'Branch 1'));
parallel.branch(new stepfunctions.Pass(stack, 'Branch 1', { stateName: 'first-pass-state' }));
parallel.branch(new stepfunctions.Pass(stack, 'Branch 2'));

// THEN
Expand All @@ -19,7 +19,7 @@ describe('Parallel State', () => {
Type: 'Parallel',
End: true,
Branches: [
{ StartAt: 'Branch 1', States: { 'Branch 1': { Type: 'Pass', End: true } } },
{ StartAt: 'first-pass-state', States: { 'first-pass-state': { Type: 'Pass', End: true } } },
{ StartAt: 'Branch 2', States: { 'Branch 2': { Type: 'Pass', End: true } } },
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ describe('State Machine Resources', () => {
// GIVEN
const stack = new cdk.Stack();
const task = new stepfunctions.Pass(stack, 'Pass', {
stateName: 'my-pass-state',
inputPath: '$',
outputPath: '$.state',
parameters: {
Expand Down
21 changes: 21 additions & 0 deletions packages/aws-cdk-lib/aws-stepfunctions/test/wait.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,25 @@ describe('Wait State', () => {
});
});

test('supports adding a custom state name', () => {
// GIVEN
const stack = new cdk.Stack();
const waitTime = new Wait(stack, 'myWaitState', {
stateName: 'wait-state-custom-name',
time: WaitTime.duration(cdk.Duration.seconds(30)),
});

// THEN
expect(render(stack, waitTime)).toEqual({
StartAt: 'wait-state-custom-name',
States: {
'wait-state-custom-name': {
Seconds: 30,
Type: 'Wait',
End: true,
},
},
});
});

});