Sub topic 4
Controlling flow with Choice, Wait, Pass, Succeed, and Fail
This chapter assumes you can read a basic state machine in Amazon States Language (ASL) and know what a Task state does. Here you will use the five states that shape the path of an execution without calling a service: Choice, Wait, Pass, Succeed, and Fail.
The five flow states
| State | Purpose | Has `Next`? | Ends the execution? |
|---|---|---|---|
| Choice | Branch on the input data | No, each rule has its own `Next` | No |
| Wait | Pause for a time or until a date | Yes, or `End` | Only if `End` is true |
| Pass | Pass input to output, optionally reshaping it | Yes, or `End` | Only if `End` is true |
| Succeed | Stop with success | No | Yes |
| Fail | Stop with an error | No | Yes |
Choice: branching on data
A Choice state holds a list of rules in Choices. Each rule compares a Variable (a JSON path into the state input) with a value and names a Next state. Step Functions checks the rules in order and follows the first one that matches. If none match, it follows Default. Without a Default, an unmatched input fails the execution with States.NoChoiceMatched, so always set one.
Comparison operators are typed. Examples are StringEquals, NumericGreaterThanEquals, BooleanEquals, and TimestampLessThan. You can combine rules with And, Or, and Not.
A Choice state does not change data. It passes its input unchanged to the next state.
Wait, Pass, Succeed, and Fail
- Wait pauses for a fixed
Secondsor until an absoluteTimestamp. UseSecondsPathorTimestampPathto read the value from the input instead. - Pass copies its input to its output. Use
Parametersto build a new JSON shape, orResultto inject fixed data. It is useful for setting defaults and for stubbing out a state during development. - Succeed ends the execution successfully.
- Fail ends it with an
Errorname and aCausedescription. Both are fixed strings. Callers and alarms can match on them.
Putting them together
This state machine normalizes the input, routes by amount, delays large orders, and rejects invalid ones.
{
"Comment": "Route a payment by amount",
"StartAt": "SetDefaults",
"States": {
"SetDefaults": {
"Type": "Pass",
"Parameters": {
"orderId.$": "$.orderId",
"amount.$": "$.amount",
"currency": "USD"
},
"Next": "CheckAmount"
},
"CheckAmount": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.amount",
"NumericLessThanEquals": 0,
"Next": "InvalidAmount"
},
{
"Variable": "$.amount",
"NumericGreaterThanEquals": 1000,
"Next": "CoolingOff"
}
],
"Default": "AutoApprove"
},
"CoolingOff": {
"Type": "Wait",
"Seconds": 300,
"Next": "AutoApprove"
},
"AutoApprove": {
"Type": "Succeed"
},
"InvalidAmount": {
"Type": "Fail",
"Error": "InvalidAmount",
"Cause": "Amount must be greater than zero"
}
}
}Common errors
- Missing field in
Variable. If the path does not exist in the input, the Choice state raisesStates.Runtimeand the execution fails. Guard the rule withIsPresentinside anAnd, placed before the comparison. - Wrong type.
"amount": "50"is a string, soNumericGreaterThanEqualsdoes not match it. Use the operator that matches the JSON type. - Rule order. Because the first match wins, put the most specific rules first. In the example, the
<= 0rule comes before the>= 1000rule. - Long waits in Express workflows. An Express workflow runs for at most five minutes, so a long Wait cannot fit. Standard workflows can run for up to a year.
Exercise
A Choice state has rules for $.tier equal to "gold" and "silver" but no Default. An execution arrives with $.tier set to "bronze". What happens, and how do you fix it?
Show answer
No rule matches and there is no Default, so the execution fails with States.NoChoiceMatched. Add a Default field that names the state to use for all other values, such as a standard-tier state or a Fail state.