Fn::ForEach - AWS CloudFormation (original) (raw)
The Fn::ForEach
intrinsic function takes a collection and a fragment, and applies the items in the collection to the identifier in the provided fragment.Fn::ForEach
can contain other intrinsic functions, includingFn::ForEach
itself, and be used within the Conditions, Outputs, Resources (including the resource properties) sections. It cannot be used in the Format version, Description,Metadata, Transform, Parameters, Mappings, Rules, orHooks
sections. For examples, see Examples.
Important
Using the Fn::ForEach
intrinsic function does not change the quotas, which apply to the resultant template. Quotas include the maximum size of a template and the maximum number of resources in a template. For more information, see Understand CloudFormation quotas.
Declaration
JSON
"Fn::ForEach::UniqueLoopName": [
"Identifier",
["Value1","Value2"], // Collection
{"OutputKey": {OutputValue}}
]
YAML
'Fn::ForEach::UniqueLoopName':
- Identifier
- - Value1 # Collection
- Value2
- 'OutputKey':
OutputValue
Parameters
UniqueLoopName
A name for this loop. The name must be unique within the template and cannot conflict with any logical ID values in the CloudFormation template Resources syntax section of the template. This name is not in the transformed output.
Identifier
The identifier you want to replace in the`OutputKey`
and `OutputValue`
parameters that represent the template fragment that is replicated. All instances of ${Identifier}
, or&{Identifier}
, in the`OutputKey`
and `OutputValue`
parameters are replaced with the values from the`Collection`
parameter.
Collection
The collection of values to iterate over. This can be an array in this parameter, or it can be a [Ref](./intrinsic-function-reference-ref.html)
to aCommaDelimitedList
. When using the &{Identifier}
, non-alphanumeric characters can be passed in the Collection
.
OutputKey
The key in the transformed template. ${Identifier}
or&{Identifier}
must be included within the`OutputKey`
parameter. For example, if Fn::ForEach
is used in the Resources section of the template, this is the logical Id of each resource.
The &{}
syntax allows non-alphanumeric characters in the Collection
to be used in `OutputKey`
parameter. For an example of this, see Passing non-alphanumeric characters within the Collection for Fn::ForEach.
OutputValue
The value that is replicated in the transformed template for each item in the`Collection`
parameter. For example, ifFn::ForEach
is used in the Resources section of the template, this is the template fragment that is repeated to configure each resource.
Return value
An expanded object that contains the object fragment repeated once for each item in the collection, where the identifier in the fragment is replaced with the item from the collection.
Supported functions
You can use the following functions within Fn::ForEach
.
- Condition functions:
[Fn::And](./intrinsic-function-reference-conditions.html#intrinsic-function-reference-conditions-and)
[Fn::Equals](./intrinsic-function-reference-conditions.html#intrinsic-function-reference-conditions-equals)
[Fn::If](./intrinsic-function-reference-conditions.html#intrinsic-function-reference-conditions-if)
[Fn::Not](./intrinsic-function-reference-conditions.html#intrinsic-function-reference-conditions-not)
[Fn::Or](./intrinsic-function-reference-conditions.html#intrinsic-function-reference-conditions-or)
[Fn::Base64](./intrinsic-function-reference-base64.html)
[Fn::FindInMap](./intrinsic-function-reference-findinmap.html)
[Fn::GetAtt](./intrinsic-function-reference-getatt.html)
[Fn::GetAZs](./intrinsic-function-reference-getavailabilityzones.html)
[Fn::ImportValue](./intrinsic-function-reference-importvalue.html)
[Fn::Join](./intrinsic-function-reference-join.html)
[Fn::Length](./intrinsic-function-reference-length.html)
[Fn::Transform](./intrinsic-function-reference-transform.html)
[Fn::Select](./intrinsic-function-reference-select.html)
[Fn::Sub](./intrinsic-function-reference-sub.html)
[Fn::ToJsonString](./intrinsic-function-reference-ToJsonString.html)
[Ref](./intrinsic-function-reference-ref.html)
Examples
For examples, see Examples.