Skip to content

Commit

Permalink
fix(stepfunctions): retry block in CustomState is always empty (#28598)
Browse files Browse the repository at this point in the history
Currently it is not possible to set Retry for the Task when using CustomState construct.
There are two ways to add Retry: by adding the `addRetry` method or by rendering the `Retry` property of `stateJson`.
The `addRetry` method was added in this PR because rendering the `Retry` property of `stateJson`, which was not rendered before, could result in an unexpected StateMachine for the user.

Closes #28586

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
sakurai-ryo authored Jan 8, 2024
1 parent 8562c17 commit 0042e53
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 25 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
"StateMachine2E01A3A5": {
"Type": "AWS::StepFunctions::StateMachine",
"Properties": {
"DefinitionString": "{\"StartAt\":\"my custom task\",\"States\":{\"my custom task\":{\"Next\":\"final step\",\"Type\":\"Task\",\"Resource\":\"arn:aws:states:::dynamodb:putItem\",\"Parameters\":{\"TableName\":\"my-cool-table\",\"Item\":{\"id\":{\"S\":\"my-entry\"}}},\"ResultPath\":null,\"Retry\":[{\"ErrorEquals\":[\"States.ALL\"],\"IntervalSeconds\":10,\"MaxAttempts\":5}],\"Catch\":[{\"ErrorEquals\":[\"States.ALL\"],\"Next\":\"failed\"}]},\"final step\":{\"Type\":\"Pass\",\"End\":true},\"failed\":{\"Type\":\"Fail\",\"Error\":\"DidNotWork\",\"Cause\":\"We got stuck\"}},\"TimeoutSeconds\":30}",
"RoleArn": {
"Fn::GetAtt": [
"StateMachineRoleB840431D",
"Arn"
]
},
"DefinitionString": "{\"StartAt\":\"my custom task\",\"States\":{\"my custom task\":{\"Next\":\"final step\",\"Type\":\"Task\",\"Resource\":\"arn:aws:states:::dynamodb:putItem\",\"Parameters\":{\"TableName\":\"my-cool-table\",\"Item\":{\"id\":{\"S\":\"my-entry\"}}},\"ResultPath\":null,\"Catch\":[{\"ErrorEquals\":[\"States.ALL\"],\"Next\":\"failed\"}]},\"final step\":{\"Type\":\"Pass\",\"End\":true},\"failed\":{\"Type\":\"Fail\",\"Error\":\"DidNotWork\",\"Cause\":\"We got stuck\"}},\"TimeoutSeconds\":30}"
}
},
"DependsOn": [
"StateMachineRoleB840431D"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ const custom = new sfn.CustomState(stack, 'my custom task', {
});

custom.addCatch(failure);
custom.addRetry({
errors: [sfn.Errors.ALL],
interval: cdk.Duration.seconds(10),
maxAttempts: 5,
});

const chain = sfn.Chain.start(custom).next(finalStatus);

Expand Down
7 changes: 7 additions & 0 deletions packages/aws-cdk-lib/aws-stepfunctions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,13 @@ const custom = new sfn.CustomState(this, 'my custom task', {
const errorHandler = new sfn.Pass(this, 'handle failure');
custom.addCatch(errorHandler);

// retry the task if something goes wrong
custom.addRetry({
errors: [sfn.Errors.ALL],
interval: Duration.seconds(10),
maxAttempts: 5,
});

const chain = sfn.Chain.start(custom)
.next(finalStatus);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Construct } from 'constructs';
import { State } from './state';
import { Chain } from '..';
import { CatchProps, IChainable, INextable } from '../types';
import { CatchProps, IChainable, INextable, RetryProps } from '../types';

/**
* Properties for defining a custom state definition
Expand Down Expand Up @@ -34,6 +34,17 @@ export class CustomState extends State implements IChainable, INextable {
this.stateJson = props.stateJson;
}

/**
* Add retry configuration for this state
*
* This controls if and how the execution will be retried if a particular
* error occurs.
*/
public addRetry(props: RetryProps = {}): CustomState {
super._addRetry(props);
return this;
}

/**
* Add a recovery handler for this state
*
Expand Down
43 changes: 43 additions & 0 deletions packages/aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,47 @@ describe('Custom State', () => {
},
);
});

test('can add a retry state', () => {
// GIVEN
const custom = new sfn.CustomState(stack, 'Custom', {
stateJson,
});
const chain = sfn.Chain.start(custom);

// WHEN
custom.addRetry({
errors: [sfn.Errors.ALL],
interval: cdk.Duration.seconds(10),
maxAttempts: 5,
});

// THEN
expect(render(stack, chain)).toStrictEqual(
{
StartAt: 'Custom',
States: {
Custom: {
Type: 'Task',
Resource: 'arn:aws:states:::dynamodb:putItem',
Parameters: {
TableName: 'MyTable',
Item: {
id: {
S: 'MyEntry',
},
},
},
ResultPath: null,
Retry: [{
ErrorEquals: ['States.ALL'],
IntervalSeconds: 10,
MaxAttempts: 5,
}],
End: true,
},
},
},
);
});
});

0 comments on commit 0042e53

Please sign in to comment.