Sub topic 9
Parallel branches and Map iteration
This chapter assumes you can read Amazon States Language (ASL), you know how InputPath, Parameters, ResultSelector, ResultPath and OutputPath shape data, and you understand Retry and Catch on a Task state. It covers the two states that run work concurrently: Parallel and Map.
Two kinds of fan-out
Both states start several pieces of work at once and wait for all of them. They differ in what is repeated.
| Aspect | Parallel | Map |
|---|---|---|
| What runs | A fixed set of different branches defined in the definition | One identical sub-workflow per array element |
| Number of concurrent units | Known when you write the definition | Known only at run time, from the input |
| Input to each unit | The same state input, copied to every branch | One array item, optionally reshaped by `ItemSelector` |
| Output | An array with one entry per branch, in branch order | An array with one entry per item, in input order |
| Typical use | Call a customer service and an inventory service at the same time | Process each line item of an order |
The Parallel state
A Parallel state has a Branches array. Each branch is a complete mini state machine with its own StartAt and States. Every branch receives a copy of the state's effective input, which is the input after InputPath and Parameters are applied.
{
"Comment": "Look up a customer and stock level at the same time",
"StartAt": "Gather",
"States": {
"Gather": {
"Type": "Parallel",
"Branches": [
{
"StartAt": "GetCustomer",
"States": {
"GetCustomer": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "arn:aws:lambda:us-east-1:123456789012:function:GetCustomer:$LATEST",
"Payload.$": "$"
},
"OutputPath": "$.Payload",
"End": true
}
}
},
{
"StartAt": "GetInventory",
"States": {
"GetInventory": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "arn:aws:lambda:us-east-1:123456789012:function:GetInventory:$LATEST",
"Payload.$": "$"
},
"OutputPath": "$.Payload",
"End": true
}
}
}
],
"ResultPath": "$.lookups",
"End": true
}
}
}With "ResultPath": "$.lookups", the original input is preserved and $.lookups becomes a two-element array. Element 0 is the GetCustomer result and element 1 is the GetInventory result. The order follows the Branches array, not the order in which branches finish. Later states can read $.lookups[0] and $.lookups[1]. If you omit ResultPath, the array replaces the whole state input.
Rules that catch people out
- A branch cannot transition to a state outside itself, and states outside cannot jump into a branch. Each branch must end with a terminal state (
End: true,Succeed, orFail). - State names must be unique within a branch. Across the whole state machine they only need to be unique at their own level, but unique names make execution history easier to read.
- If any branch fails and the failure is not handled inside that branch, the whole
Parallelstate fails. Step Functions stops the other running branches. You handle this withRetryandCatchon theParallelstate itself. - A retry on the
Parallelstate restarts every branch, including ones that already succeeded. If a branch has side effects, put theRetryon the Task inside the branch instead.
The Map state
Map runs an ItemProcessor once per element of an array. The field names have changed over time. Older definitions use Iterator and Parameters. Current definitions use ItemProcessor and ItemSelector. Prefer the current names in new work.
The key fields are:
ItemsPathis a JSONPath that must resolve to an array in the state input. If you omit it, the whole input must be an array.ItemProcessorholds the sub-workflow (StartAtandStates) and aProcessorConfigwithMode.INLINEruns iterations inside the parent execution.DISTRIBUTEDis covered in a later chapter.MaxConcurrencylimits how many iterations run at once. A value of0or omitting it means no limit set by you. In inline mode, the service still caps real concurrency at 40.ItemSelectorbuilds each iteration's input from the item, its index, and data from the parent input.ResultPath,ResultSelector,RetryandCatchwork as on other states.
The first example runs an inline Map with at most five iterations at once. Each iteration receives the raw array element as its input.
{
"StartAt": "ProcessItems",
"States": {
"ProcessItems": {
"Type": "Map",
"ItemsPath": "$.items",
"MaxConcurrency": 5,
"ItemProcessor": {
"ProcessorConfig": {
"Mode": "INLINE"
},
"StartAt": "MarkProcessed",
"States": {
"MarkProcessed": {
"Type": "Pass",
"Result": {
"processed": true
},
"End": true
}
}
},
"End": true
}
}
}If $.items has three elements, the state output is [{"processed": true}, {"processed": true}, {"processed": true}]. The Pass state ignores its input, so the output shows nothing about which item produced which result. Real iterations usually carry the item forward.
Shaping iteration input with ItemSelector
An iteration normally sees only its own item. It cannot see the rest of the parent input unless you copy it in. ItemSelector does this, using the context object for item data.
{
"StartAt": "ShipLines",
"States": {
"ShipLines": {
"Type": "Map",
"ItemsPath": "$.lines",
"MaxConcurrency": 10,
"ItemSelector": {
"orderId.$": "$.orderId",
"line.$": "$$.Map.Item.Value",
"position.$": "$$.Map.Item.Index"
},
"ItemProcessor": {
"ProcessorConfig": {
"Mode": "INLINE"
},
"StartAt": "ShipLine",
"States": {
"ShipLine": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "arn:aws:lambda:us-east-1:123456789012:function:ShipLine:$LATEST",
"Payload.$": "$"
},
"OutputPath": "$.Payload",
"Retry": [
{
"ErrorEquals": ["Lambda.ServiceException", "Lambda.TooManyRequestsException"],
"IntervalSeconds": 2,
"MaxAttempts": 3,
"BackoffRate": 2.0
}
],
"End": true
}
}
},
"ResultPath": "$.shipments",
"End": true
}
}
}Here $.orderId is read from the Map state's input, because ItemSelector is evaluated against it. $$.Map.Item.Value is the current element and $$.Map.Item.Index is its zero-based position. Each iteration's Lambda payload looks like {"orderId": "1001", "line": {...}, "position": 0}. The results land in $.shipments, in the same order as $.lines, regardless of completion order.
Failure behavior
By default, one failed iteration fails the whole Map state, and the remaining iterations are stopped. Three tools control this:
- Put
Retryon Tasks inside theItemProcessorso transient errors are retried per item, not per Map. - Put
Catchinside theItemProcessorto turn a failure into a normal result, for example{"status": "FAILED", "line": ...}. The Map then succeeds, and a later Choice state can inspect the array for failures. - Put
Catchon the Map state to route the whole fan-out to a cleanup path.
Catching inside the iteration is the usual way to get partial success from an inline Map. Tolerated-failure settings exist for Distributed Map and are covered in that chapter.
Limits and sizing
- State input and output are limited to 256 KiB. A Parallel or Map output array that collects many results can hit this limit. Return small summaries or write large data to Amazon S3 and pass references.
- In Standard workflows, every state transition inside every iteration is recorded in the execution history, which is capped at 25,000 events. A Map over thousands of items with several states each can exhaust it. This is the main reason to use Distributed Map for large arrays.
MaxConcurrencyprotects downstream services. If each iteration calls a Lambda function with limited reserved concurrency, setMaxConcurrencyat or below that limit. Otherwise you will seeLambda.TooManyRequestsExceptionand rely on retries.
Common errors
States.Runtimeerror becauseItemsPathdid not resolve to an array. Check that the path exists and is not an object ornull.- Missing parent data inside iterations, because the item was passed without
ItemSelector. - Results in the wrong place. Without
ResultPath, the output array replaces the state input. - Assuming order of execution. Results are ordered, but side effects occur in no guaranteed order.
Exercise
A Parallel state has three branches: A, B and C. Branch B fails with an error that no Retry or Catch inside B handles. The Parallel state has no Retry or Catch. What happens to the execution, and where would you add a Retry to avoid re-running branches A and C?
Show answer
The Parallel state fails, the other branches are stopped, and the execution fails with B's error. To avoid re-running A and C, add the Retry to the failing Task inside branch B. A Retry on the Parallel state would restart all three branches.
Exercise
A Map state iterates over $.lines, and each iteration's Lambda needs both the line and the top-level orderId. Which field supplies orderId, and which expression gives the line's position in the array?
Show answer
Use ItemSelector with "orderId.$": "$.orderId" and "position.$": "$$.Map.Item.Index". The line itself is "line.$": "$$.Map.Item.Value". Without ItemSelector, each iteration receives only the array element.