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

fix(stepfunctions): the Retry field in the statesJson in CustomState is always overwrited #28793

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view

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,7 +20,7 @@
"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}",
"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.Timeout\"],\"IntervalSeconds\":10,\"MaxAttempts\":5},{\"ErrorEquals\":[\"States.Permissions\"],\"IntervalSeconds\":20,\"MaxAttempts\":2}],\"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",
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.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const stateJson = {
},
},
ResultPath: null,
Retry: [{
ErrorEquals: [sfn.Errors.PERMISSIONS],
IntervalSeconds: 20,
MaxAttempts: 2,
}],
};

const failure = new sfn.Fail(stack, 'failed', {
Expand All @@ -36,7 +41,7 @@ const custom = new sfn.CustomState(stack, 'my custom task', {

custom.addCatch(failure);
custom.addRetry({
errors: [sfn.Errors.ALL],
errors: [sfn.Errors.TIMEOUT],
interval: cdk.Duration.seconds(10),
maxAttempts: 5,
});
Expand Down
4 changes: 4 additions & 0 deletions packages/aws-cdk-lib/aws-stepfunctions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,10 @@ Custom states can be chained together with any of the other states to create you
definition. You will also need to provide any permissions that are required to the `role` that
the State Machine uses.

The Retry and Catch fields are available for error handling.
You can configure the Retry field by defining it in the JSON object or by adding it using the `addRetry` method.
However, the Catch field cannot be configured by defining it in the JSON object, so it must be added using the `addCatch` method.
Copy link
Contributor

Choose a reason for hiding this comment

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

why not to do the same for the Catch field?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@moelasmar
Thanks for clarifying.
As mentioned in the comments (#25798 (comment)), the method of defining the Catch directly in JSON for the CustomState did not render well and forced us to call _addCatch in the base State class.
The addCatch method was added to solve this in this PR (#28422), but now it overrides the Catch and Retry fields in the JSON.
So it will take some investigation and changes to be able to define the Catch directly in JSON.

Copy link
Contributor

Choose a reason for hiding this comment

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

thanks @sakurai-ryo for the clarifying.


The following example uses the `DynamoDB` service integration to insert data into a DynamoDB table.

```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,17 @@ export class CustomState extends State implements IChainable, INextable {
* Returns the Amazon States Language object for this state
*/
public toStateJson(): object {
return {
const state = {
...this.renderNextEnd(),
...this.stateJson,
...this.renderRetryCatch(),
};

// merge the Retry filed defined in the stateJson into the state
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The Retry is configured in two ways(the addRetry method and defining it in the stateJson), so we need to merge them.

if (Array.isArray(this.stateJson.Retry)) {
state.Retry = [...state.Retry, ...this.stateJson.Retry];
}

return state;
}
}
79 changes: 79 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 @@ -163,4 +163,83 @@ describe('Custom State', () => {
},
);
});

test('respect the Retry field in the stateJson', () => {
// GIVEN
const custom = new sfn.CustomState(stack, 'Custom', {
stateJson: {
Type: 'Task',
Resource: 'arn:aws:states:::dynamodb:putItem',
Parameters: {
TableName: 'MyTable',
Item: {
id: {
S: 'MyEntry',
},
},
},
ResultPath: null,
Retry: [
{
ErrorEquals: [sfn.Errors.TIMEOUT],
IntervalSeconds: 20,
MaxAttempts: 2,
},
{
ErrorEquals: [sfn.Errors.RESULT_PATH_MATCH_FAILURE],
IntervalSeconds: 20,
MaxAttempts: 2,
},
],
},
});
const chain = sfn.Chain.start(custom);

// WHEN
custom.addRetry({
errors: [sfn.Errors.PERMISSIONS],
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.Permissions'],
IntervalSeconds: 10,
MaxAttempts: 5,
},
{
ErrorEquals: ['States.Timeout'],
IntervalSeconds: 20,
MaxAttempts: 2,
},
{
ErrorEquals: ['States.ResultPathMatchFailure'],
IntervalSeconds: 20,
MaxAttempts: 2,
},
],
End: true,
},
},
},
);
});
});