在AWS Step Function中,选择条件的每个分支都必须以某种状态结束。默认情况下,选择条件的每个分支都会返回'Succeed”或'Fail”状态。有时,我们需要在特定情况下设置自定义的结束状态。
要实现这一点,我们需要在选择条件中添加一个JSON对象。在这个JSON对象中,我们可以为每个条件分支设置自定义状态。例如,下面的代码将条件分支的结束状态设置为'CustomEndState”。
{
"StartAt": "MyStateMachine",
"States": {
"MyStateMachine": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-west-2:123456789012:function:MyFunction",
"Next": "CheckResult"
},
"CheckResult": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.result",
"NumericGreaterThan": 10,
"Next": "LargeValue",
"End": true,
"EndState": "CustomEndState"
},
{
"Variable": "$.result",
"NumericGreaterThan": 5,
"Next": "MediumValue",
"End": true,
"EndState": "CustomEndState"
},
{
"Variable": "$.result",
"NumericGreaterThan": 0,
"Next": "SmallValue",
"End": true,
"EndState": "CustomEndState"
}
]
},
"LargeValue": {
"Type": "Pass",
"Result": "Large",
"End": true
},
"MediumValue": {
"Type": "Pass",
"Result": "Medium",
"End": true
},
"SmallValue": {
"Type": "Pass",
"Result": "Small",
"End": true
},
"CustomEndState": {
"Type": "Pass",
"Result": "Custom End State",
"End": true
}
}
}
在选择条件的每个分支中,我们都添加了'End”和'EndState”键。如果'End”键设置为true,则该分支结束。如果'EndState”键设置为自定义状态的名称,则该分支将以自定义状态结束。
在本例中,选择条件的每个分支都以'CustomEndState”状态结束。这个状态在Step Function的状态机定义中定义为一个'Pass”状态。然