Sub topic 4
Controlling flow with Choice, Wait, Pass, Succeed, and Fail
This chapter assumes you can read Amazon States Language (ASL) at the level of the previous chapters. That means you know that a state machine is a JSON document with StartAt and States, and that Task states do the work. The five states here do no work against other AWS services. They decide where execution goes next, pause it, reshape data, or end it.
All examples use the default JSONPath query language. In a state machine that sets "QueryLanguage": "JSONata", a Choice rule uses a Condition expression instead of Variable plus a comparison operator. The routing ideas are the same.
The five states at a glance
| State | Purpose | Has `Next` or `End`? | Can `Retry` or `Catch`? |
|---|---|---|---|
| Choice | Branch on the data | No, each rule has its own `Next` | No |
| Wait | Pause for a duration or until a time | Yes | No |
| Pass | Pass input to output, optionally injecting or reshaping data | Yes | No |
| Succeed | End the execution successfully | No, it is terminal | No |
| Fail | End the execution with an error and cause | No, it is terminal | No |
Because Choice, Wait, and Pass do not call anything, they cannot fail from a service error. They can still raise runtime errors, such as a bad path.
Choice: branching on data
A Choice state holds an ordered list of rules in Choices. Each rule has a Variable (a path into the state input), exactly one comparison operator, and a Next state. Step Functions evaluates the rules from top to bottom and follows the first one that matches. Order therefore matters when rules overlap.
The optional Default field names the state to use when no rule matches. Always set it. Without it, a non-matching input stops the execution with a States.NoChoiceMatched error.
Comparison operators
Operators are named by type, then comparison. Each type also has a ...Path form that compares against another path in the input, such as NumericLessThanPath.
| Family | Examples |
|---|---|
| String | `StringEquals`, `StringLessThan`, `StringGreaterThanEquals`, `StringMatches` |
| Numeric | `NumericEquals`, `NumericGreaterThan`, `NumericLessThanEquals` |
| Boolean | `BooleanEquals` |
| Timestamp | `TimestampEquals`, `TimestampLessThan`, `TimestampGreaterThanEquals` |
| Type and presence checks | `IsPresent`, `IsNull`, `IsString`, `IsNumeric`, `IsBoolean`, `IsTimestamp` |
StringMatches accepts * as a wildcard, so "StringMatches": "logs/*.json" matches any JSON file under logs/.
Combining rules
Three operators combine other rules: And and Or take an array of rules, and Not takes one rule. Nested rules have no Next. Only the top-level rule has one.
Missing fields
If a rule's Variable points at a field that does not exist, the state raises a States.Runtime error. The comparison does not evaluate to false. Guard optional fields with IsPresent. Put it first inside an And, because And stops at the first false rule and never evaluates the risky comparison.
Wait: pausing without compute
A Wait state pauses the execution. You set exactly one of four fields.
Seconds: a fixed number of seconds.Timestamp: an absolute time in RFC 3339 format, such as2026-12-01T09:00:00Z.SecondsPath: a path to a number of seconds in the input.TimestampPath: a path to a timestamp in the input.
Waiting uses no compute, which makes it a cheap replacement for a function that sleeps. In a Standard workflow a state machine can wait for up to a year, which is the limit for the whole execution. In an Express workflow the whole execution is capped at five minutes, so long waits do not fit.
Wait pairs with Choice to form a polling loop: a Task checks status, a Choice tests it, and a Wait delays before the Choice routes back to the Task. Each loop iteration adds events to the execution history. A Standard execution can hold at most 25,000 history events, so a tight loop with a long run can hit that limit. Use a longer interval, or use the .sync integration pattern from a later chapter, which polls on your behalf.
Pass: reshaping and stubbing
A Pass state copies its input to its output. It supports InputPath, Parameters, Result, ResultPath, and OutputPath. Three uses are common:
- Inject constant data with
Result, merged into the input byResultPath. - Restructure JSON with
Parametersand the.$suffix, for example renaming fields before a Task. - Stand in for a Task while you design a workflow, so you can test routing before the real services exist.
Result is a static value. Use Parameters when you need values taken from the input.
Succeed and Fail: ending an execution
Succeed ends the execution with status SUCCEEDED. The output is the state's input, adjusted by InputPath and OutputPath if you set them. Many workflows do not need an explicit Succeed, because a final state can simply set "End": true. Succeed is useful as a named destination for a Choice rule, because it makes the success path visible in the console graph.
Fail ends the execution with status FAILED. It takes an Error (a short name you can filter on) and a Cause (a human-readable explanation). It has no output. If you need these values to come from the input, use ErrorPath and CausePath, which accept a reference path or an intrinsic function such as States.Format. A static field and its Path variant cannot be set together.
Neither state can have a Catch. A Fail inside a Map iteration or a Parallel branch fails that state, and a Catch on the Map or Parallel state can then handle it. A Succeed inside a branch ends only that branch or iteration, not the whole execution.
A worked example
This state machine routes orders. It rejects invalid orders, delays scheduled ones until their time, holds large orders briefly, and ships everything else immediately.
{
"Comment": "Route an order by validity, schedule, and size",
"StartAt": "AddMetadata",
"States": {
"AddMetadata": {
"Type": "Pass",
"Result": { "source": "order-api" },
"ResultPath": "$.meta",
"Next": "RouteOrder"
},
"RouteOrder": {
"Type": "Choice",
"Choices": [
{
"Or": [
{ "Variable": "$.amount", "NumericLessThanEquals": 0 },
{ "Variable": "$.customerId", "IsPresent": false }
],
"Next": "InvalidOrder"
},
{
"And": [
{ "Variable": "$.scheduledFor", "IsPresent": true },
{ "Variable": "$.scheduledFor", "IsTimestamp": true }
],
"Next": "WaitUntilScheduled"
},
{
"Variable": "$.amount",
"NumericGreaterThanEquals": 1000,
"Next": "CoolingOff"
}
],
"Default": "ShipNow"
},
"WaitUntilScheduled": {
"Type": "Wait",
"TimestampPath": "$.scheduledFor",
"Next": "ShipNow"
},
"CoolingOff": {
"Type": "Wait",
"Seconds": 60,
"Next": "ShipNow"
},
"ShipNow": {
"Type": "Pass",
"Result": { "shipped": true },
"ResultPath": "$.shipment",
"Next": "OrderDone"
},
"OrderDone": {
"Type": "Succeed"
},
"InvalidOrder": {
"Type": "Fail",
"Error": "InvalidOrder",
"Cause": "Amount must be positive and customerId is required"
}
}
}Trace this input through the machine:
{ "orderId": "1001", "customerId": "c-7", "amount": 2500 }AddMetadata adds meta. In RouteOrder, the first rule is false because the amount is positive and customerId is present. The second rule is false because scheduledFor is absent, and And stops there without evaluating IsTimestamp. The third rule is true, so execution goes to CoolingOff, waits 60 seconds, then reaches ShipNow and OrderDone. The final output is the original fields plus meta and shipment.
Common errors
- No
Defaulton a Choice. A single unexpected input ends the execution withStates.NoChoiceMatched. - Unguarded optional fields. Comparing a missing field raises
States.Runtime. Guard it withIsPresent. - Wrong rule order. The first match wins. Put the specific rules (invalid input, special cases) before the general ones.
- Setting two Wait fields.
SecondsandTimestampare mutually exclusive, and so are all four options. The definition is rejected at creation time. - Past timestamps. A Wait whose
Timestampis already in the past continues immediately. It does not fail. - Expecting
Nexton terminal states. Succeed and Fail rejectNextandEnd. - Using
Resultwhen you need input values.Resultis static, so useParameterswith.$to read the input.
Exercise
In the worked example, what happens to the input {"orderId": "1002", "customerId": "c-9", "amount": 50, "scheduledFor": "2020-01-01T00:00:00Z"}, and why?
Show answer
The first rule is false (the amount is positive and customerId is present). The second rule is true, because scheduledFor is present and is a valid timestamp, so execution goes to WaitUntilScheduled. The timestamp is in the past, so the Wait finishes immediately. The order then goes to ShipNow and OrderDone, and the execution succeeds. The amount rule is never checked because the second rule matched first.
Exercise
A Choice state tests "$.discount" with NumericGreaterThan 0, but some inputs have no discount field. Rewrite the rule so those inputs go to the Default state instead of raising an error.
Show answer
Wrap the comparison in an And with IsPresent first: {"And": [{"Variable": "$.discount", "IsPresent": true}, {"Variable": "$.discount", "NumericGreaterThan": 0}], "Next": "ApplyDiscount"}. And stops at the first false rule, so the numeric comparison is never evaluated when the field is missing, and the Choice falls through to Default.