TGViewer
AWS Notes AWS Notes @aws_notes · 5.83K subscribers
Post #2517 2.13K
​​Loops in CloudFormation: !ForEach 💪

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-foreach.html

AWSTemplateFormatVersion: 2010-09-09
Transform: 'AWS::LanguageExtensions'
Resources:
'Fn::ForEach::Tables':
- TableName
- [Points, Score, Name, Leaderboard]
- 'DynamoDB${TableName}':
Type: 'AWS::DynamoDB::Table'
Properties:
TableName: !Ref TableName
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: '5'
WriteCapacityUnits: '5'

Fn::ForEach can be used in the sections:

▫️ Resource
▪️ Resource properties
▫️ Conditions
▫️ Outputs

Fn::ForEach parameters

Identifier (String) → Identifier is used to refer to the current element we’re iterating over within the Collection (Array of Strings).

Collection (Array of Strings) → Array of values that the Identifier can take.

OutputKey (String) → The key of the resulting key-value pair for the given element in the collection that will be merged to the parent object.

OutputValue (Any) → The value of the resulting key-value pair for the given element in the collection that will be merged to the parent object.

Note: the syntax of Fn::ForEach declaration has a suffix where the UniqueLoopName is used to identify the loop. This allows multiple Fn::ForEach function references to be declared on a given level.

#CloudFormation
  • 🔥 10
  • ❤ 4
  • 😱 4
  • 😁 3
  • 👍 1
More from @aws_notes
  1. Oct 9, 2026Сколько зарабатывают вайбкодеры (обновлённая версия): Junior: -20$/month Middle: -100$/mon…
  2. Oct 7, 2026People jan! 🥳 We’ve got something exciting: Ready to put your AI skills to the test and c…
  3. Oct 7, 2026Agentic Football Cup | AWS Game Day Friday, Oct 23 · 1:00 PM to 4:00 PM CEST Online Безкош…
  4. Oct 6, 2026AWS Innovation Day 2026: от AI-пилотов к реальным решениям 22 октября в Astana Hub, Астана…
  5. Oct 5, 2026EKS + Kubernetes 1.37 🎉 С официального релиза 1.37 прошло чуть больше месяца и теперь на…
  6. Oct 5, 2026Приглашаем на встречу “Сжечь 50 млрд токенов и собрать кроссплатформенного агента для AI-ф…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →