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 catch field in CustomState is not rendered #29654

Merged
merged 7 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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\":\"my custom task with inline Retriers\",\"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\"}]},\"my custom task with inline Retriers\":{\"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.Permissions\"],\"IntervalSeconds\":20,\"MaxAttempts\":2}]},\"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\":\"my custom task with inline Retriers\",\"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}],\"Catch\":[{\"ErrorEquals\":[\"States.ALL\"],\"Next\":\"failed\"}]},\"my custom task with inline Retriers\":{\"Next\":\"my custom task with inline Catchers\",\"Type\":\"Task\",\"Resource\":\"arn:aws:states:::dynamodb:putItem\",\"Parameters\":{\"TableName\":\"my-cool-table\",\"Item\":{\"id\":{\"S\":\"my-entry\"}}},\"ResultPath\":null,\"Retry\":[{\"ErrorEquals\":[\"States.Permissions\"],\"IntervalSeconds\":20,\"MaxAttempts\":2}]},\"my custom task with inline Catchers\":{\"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.Permissions\"],\"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
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ const stateJson = {
},
},
ResultPath: null,
Retry: [{
ErrorEquals: [sfn.Errors.PERMISSIONS],
IntervalSeconds: 20,
MaxAttempts: 2,
}],
};

const failure = new sfn.Fail(stack, 'failed', {
Expand All @@ -39,18 +34,35 @@ const custom = new sfn.CustomState(stack, 'my custom task', {
stateJson,
});

const customWithInlineRetry = new sfn.CustomState(stack, 'my custom task with inline Retriers', {
stateJson,
});

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

const chain = sfn.Chain.start(custom).next(customWithInlineRetry).next(finalStatus);
const customWithInlineRetry = new sfn.CustomState(stack, 'my custom task with inline Retriers', {
stateJson: {
...stateJson,
Retry: [{
ErrorEquals: [sfn.Errors.PERMISSIONS],
IntervalSeconds: 20,
MaxAttempts: 2,
}],
},
});

const customWithInlineCatch = new sfn.CustomState(stack, 'my custom task with inline Catchers', {
stateJson: {
...stateJson,
Catch: [{
ErrorEquals: [sfn.Errors.PERMISSIONS],
Next: 'failed',
}],
},
});

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

const sm = new sfn.StateMachine(stack, 'StateMachine', {
definition: chain,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ export class CustomState extends State implements IChainable, INextable {
...this.renderRetryCatch(),
};

// merge the Retry field defined in the stateJson into the state
// Retriers and Catchers can be specified directly in the stateJson or indirectly to the construct with addRetry() and addCatch().
// renderRetryCatch() only renders the indirectly supplied Retriers and Catchers, so we need to manually merge in those directly in the stateJson
if (Array.isArray(this.stateJson.Retry)) {
state.Retry = Array.isArray(state.Retry) ? [...state.Retry, ...this.stateJson.Retry] : [...this.stateJson.Retry];
}

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

aaythapa marked this conversation as resolved.
Show resolved Hide resolved
return state;
}
}
142 changes: 142 additions & 0 deletions packages/aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { render } from './private/render-util';
import * as cdk from '../../core';
import * as sfn from '../lib';
import { Errors } from '../lib/types';

describe('Custom State', () => {
let stack: cdk.Stack;
Expand Down Expand Up @@ -309,4 +310,145 @@ describe('Custom State', () => {
},
);
});

test('expect retry to merge when specifying strategy inline and through construct', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

(non-blocking) this is already covered as part of this test:

test('respect the Retry field in the stateJson', () => {

// GIVEN
const custom = new sfn.CustomState(stack, 'Custom', {
stateJson: {
...stateJson,
Retry: [{
ErrorEquals: ['States.TaskFailed'],
}],
},
}).addRetry({ errors: [Errors.TIMEOUT] });
const chain = sfn.Chain.start(custom);

// 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.Timeout'],
},
{
ErrorEquals: ['States.TaskFailed'],
},
],
End: true,
},
},
},
);
});

test('expect catch to not fail when specifying strategy inline', () => {
// GIVEN
const custom = new sfn.CustomState(stack, 'Custom', {
stateJson: {
...stateJson,
Catch: [{
ErrorEquals: ['States.TaskFailed'],
Next: 'Failed',
}],
},
});
const chain = sfn.Chain.start(custom);

// 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,
Catch: [{
ErrorEquals: ['States.TaskFailed'],
Next: 'Failed',
}],
End: true,
},
},
},
);
});

test('expect catch to merge when specifying strategy inline and through construct', () => {
// GIVEN
const failure = new sfn.Fail(stack, 'Failed', {
error: 'DidNotWork',
cause: 'We got stuck',
});

const custom = new sfn.CustomState(stack, 'Custom', {
stateJson: {
...stateJson,
Catch: [{
ErrorEquals: ['States.TaskFailed'],
Next: 'Failed',
}],
},
}).addCatch(failure, { errors: [Errors.TIMEOUT] });
const chain = sfn.Chain.start(custom);

// 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,
Catch: [
{
ErrorEquals: ['States.Timeout'],
Next: 'Failed',
}, {
ErrorEquals: ['States.TaskFailed'],
Next: 'Failed',
},
],
End: true,
},
Failed: {
Type: 'Fail',
Error: 'DidNotWork',
Cause: 'We got stuck',
},
},
},
);
});
});