This topic describes foreach steps and related examples.
Overview
A foreach step traverses parameters of an array type in the input, and executes the serial steps for each element in the array in parallel. Foreach steps are similar to foreach
in programming languages. The difference is that iterations of foreach steps are executed in parallel.
Each iteration of a foreach step corresponds to a local variable. In a foreach step, serial steps of each element in the input parameters are executed in parallel. These serial steps change the local variables corresponding to their iterations. After all iterations are executed, output mappings can be used to convert the local variable arrays of iterations into the output of the foreach step.
A foreach step contains the following attributes:
- type: Required. The step type. The value foreach indicates that the step is a foreach step.
- name: Required. The step name.
- iterationMapping: Required. The iterative mapping.
- collection: Required. The input parameter that serves as a collection for a foreach step.
- item: Required. The name of the current element that is incorporated into the iteration input.
- index: Optional. The name of the current position that is incorporated into the iteration input.
- steps: Required. The serial steps.
- end: Optional. Specifies whether to proceed with the subsequent steps after the current step ends.
- inputMappings: Optional. The input mappings.
- outputMappings: Optional. The output mappings. In this step, the
$local
is an array. Each element in the array is a JSON object that records the result of each iteration.Note If no output mappings are specified, this step has no output by default.
Examples
The following sample flow defines a foreach 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 following information is the flow input. No input mapping is specified for the
myforeach
step. Therefore, its input is the same as the flow input.{ "names": ["a", "b", "c"] }
- No input mapping is defined for
toUpperCase
. Therefore, its input is the same as the input of the parent step. According toiterationMapping
, the system inputs the current elements (a
,b
, andc
in sequence) as values and thename
as the key upon each iteration{ "name": "a", "names":["a","b","c"] } { "name": "b", "names":["a","b","c"] } { "name": "c", "names":["a","b","c"] }
toUpperCase
is executed three times, with the following outputs in sequence:{ "name": "A" } { "name": "B" } { "name": "C" }
- The local variable of
myforeach
is an array, with the following values:[ { "name": "A" }, { "name": "B" }, { "name": "C" } ]
- The following information is the output of
myforeach
. No output or result mappings are defined for the flow. Therefore, the output is the final flow output.{ "names": ["A", "B", "C"] }