This topic describes the foreach step and provides usage examples.
Introduction
A foreach step traverses an input array parameter. It executes a sequence of steps in parallel for each element in the array. The foreach step is similar to the foreach loop in programming languages. The main difference is that the iterations are executed in parallel.
Each iteration of a foreach step corresponds to a local variable. When a foreach step runs, it concurrently executes a sequence of steps for each element in the input parameter. These steps modify the local variable for their corresponding iteration. After all branches are complete, you can use output mapping to transform the array of local variables into the output of the foreach step.
A parallel step has the following properties:
- (Required) type: A value of foreach specifies a foreach step.
- (Required) name: The name of the step.
- (Required) iterationMapping: The iteration mapping.
- (Required) collection: Defines the input parameter to use as the collection for the loop.
- (Required) item: Defines the key name for the current element when it is merged into the iteration input.
- (Optional) index: Defines the key name for the current position (index) when it is merged into the iteration input.
- (Required) steps: Defines the sequence of steps to execute.
- (Optional) end: Specifies whether to continue executing subsequent steps after this step completes.
- (Optional) inputMappings: The input mapping.
- (Optional) outputMappings: The output mapping. The
$localvariable for this step is an array. Each element in the array is a JSON object that records the result of an iteration.Note If you do not specify an output mapping, the step's output is empty by default.
Example
The following example shows a flow with a foreach step, which is a parallel step that contains a task step.
version: v1
type: flow
steps:
- type: foreach
name: myforeach
iterationMapping:
collection: $.names
item: name
steps:
- type: task
name: toUpperCase
resourceArn: acs:fc:{region}:{account}:services/fnf_test/functions/toUpperCase
outputMappings:
- target: names
source: $local[*].name - The input for the flow is shown below. Because the
myforeachstep does not specify an input mapping, its input is the same as the flow's input.{ "names": ["a", "b", "c"] } toUpperCasedoes not define an input mapping, so it inherits the input from its parent step. Based on theiterationMapping, the system merges the current element (sequentiallya,b, andc) into the input with the keynamefor each iteration.{ "name": "a", "names":["a","b","c"] } { "name": "b", "names":["a","b","c"] } { "name": "c", "names":["a","b","c"] }toUpperCaseis executed three times. The sequential outputs are shown below.{ "name": "A" } { "name": "B" } { "name": "C" }myforeachstep is an array. Its value is shown below.[ { "name": "A" }, { "name": "B" }, { "name": "C" } ]myforeachstep is shown below. Because the flow does not define an output mapping, this output is also the final output of the flow execution.{ "names": ["A", "B", "C"] }