Posted On: Jul 26, 2023

AWS CloudFormation announces looping capability 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 AWS::LanguageExtensions 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 AWS::DynamoDB::Table 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 AWS Regions where CloudFormation is available. Refer to the AWS Region table to see AWS Regions where CloudFormation is available.

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.