Posted On: Jul 26, 2023
Amazon CloudFormation announces looping capabilities with Fn::ForEach intrinsic function. With Fn::ForEach, you can replicate parts of your templates with minimal lines of code. You can use Fn::ForEach to simplify your template layout and make it easier and faster for you and your peers to review your code. Fn::ForEach helps reduce human errors such as updating wrong properties or missing out on updating multiple target properties in your template.
To use Fn::ForEach you have to declare CloudFormation Language Extensions transform. The language extensions transform expands the functionality of the base CloudFormation JSON/YAML template language. With this launch, you can use Fn::ForEach in your Resources, Resource properties, Conditions, and Outputs sections of your templates. For example, you can declare five instances of Amazon DynamoDB with unique identifier names using Fn::ForEach instead of replicating the same lines of code. Additionally, you can use nested Fn::ForEach loops, and combine Fn::ForEach with other intrinsic functions such as Fn::Join to generate advanced templates. For example, you can combine resources such as VPCs, Subnets and Transit Gateways as nested loops to create a networking stack. Refer to user guide for examples with sample templates.
Fn::ForEach intrinsic function is available in the Amazon Web Services China (Beijing) Region, operated by Sinnet, and the Amazon Web Services China (Ningxia) Region, operated by NWCD.
Fn::ForEach is the result of open discussions with the CloudFormation community via our Language Discussion Github repository. We thank the community for sharing their feedback. Visit language discussions repository to request language features, and leave feedback on Request for Comments (RFC) proposals for new language features.