Sub topic 9
Parallel branches and Map iteration
This chapter assumes you can read Amazon States Language (ASL), know how ResultPath and Catch work, and have written a Task state that calls Lambda. Here you will use two states that run work concurrently: Parallel runs different branches on the same input, and Map runs the same workflow once for each item in an array.
Parallel: different work, same input
A Parallel state takes a list of Branches. Each branch is a complete mini state machine with its own StartAt and States. Every branch receives the same input. The state finishes when all branches finish.
The output is an array with one element per branch, in the order the branches are declared. It is not in completion order.
{
"StartAt": "EnrichOrder",
"States": {
"EnrichOrder": {
"Type": "Parallel",
"Branches": [
{
"StartAt": "CheckInventory",
"States": {
"CheckInventory": {
"Type": "Pass",
"Result": { "inStock": true },
"End": true
}
}
},
{
"StartAt": "CheckFraud",
"States": {
"CheckFraud": {
"Type": "Pass",
"Result": { "risk": "low" },
"End": true
}
}
}
],
"ResultPath": "$.enrichment",
"End": true
}
}
}After this state runs, $.enrichment[0] holds the inventory result and $.enrichment[1] holds the fraud result. The original input is kept because of ResultPath.
A state inside a branch cannot transition to a state in another branch or outside its own branch. Branches only share the input they started with and the array they return.
Map: same work, many items
A Map state iterates over an array. ItemsPath points at the array. ItemProcessor defines the workflow that runs for each element. MaxConcurrency caps how many iterations run at once. A value of 0 means no limit.
By default, each iteration receives one array element as its input. Use ItemSelector to build a custom input. In it you can read the current element and its position from the context object.
{
"StartAt": "ProcessItems",
"States": {
"ProcessItems": {
"Type": "Map",
"ItemsPath": "$.items",
"MaxConcurrency": 5,
"ItemSelector": {
"orderId.$": "$.orderId",
"item.$": "$$.Map.Item.Value",
"index.$": "$$.Map.Item.Index"
},
"ItemProcessor": {
"ProcessorConfig": { "Mode": "INLINE" },
"StartAt": "MarkProcessed",
"States": {
"MarkProcessed": {
"Type": "Pass",
"Result": { "processed": true },
"End": true
}
}
},
"ResultPath": "$.processed",
"End": true
}
}
}Inside ItemSelector, $ refers to the Map state's own input, so $.orderId reaches a field outside the array. The result is an array with one output per item, in the same order as the input array.
Failure behavior
Both states fail as a unit by default. If one branch or iteration fails, Step Functions stops the others and the state fails with that error. Branches that were running are cancelled.
Put Retry and Catch on the Parallel or Map state to handle that failure. A retry restarts the entire state, including branches that had already succeeded. If you want finer control, put Retry and Catch on the Task states inside the branch or iterator. Then a transient error is retried in one place and the other iterations are unaffected.
Common mistakes
- Assuming the output order reflects completion order. It reflects declaration order for Parallel and array order for Map.
- Leaving
MaxConcurrencyunset when each iteration calls a rate-limited service. Large arrays can then cause throttling errors. - Omitting
ResultPath. The result array replaces the whole state input, and later states lose the original fields. - Using Map for very large datasets. An inline Map is bounded by the execution history quota. The Distributed Map chapter covers that case.
Exercise
A Map state has "ItemsPath": "$.items" and no ResultPath. The input is {"orderId": "1001", "items": ["a", "b"]}. What does the next state receive, and how would you keep orderId available?
Show answer
The next state receives only the array of per-item results, because the default ResultPath of $ replaces the whole input. Add "ResultPath": "$.processed" to the Map state so the results are stored under that key and orderId stays in the state input.