unit block for each is repetitive and brittle. Add one more bucket to the list and you have to add another block. for_each over a list solves this: ubx creates one resource per element, making the list the authoritative source of what gets provisioned. The current element is always available as each.value, and the resource label becomes the iteration key in the generated Pulumi state. The result is a concise declaration that scales from two resources to twenty without touching the block definition.
What you’ll learn
- How
for_eachover a list creates one resource per element - How to use
each.valueto reference the current element - How Pulumi keys the generated resources in state
Why this matters
With
for_each, your list is the single source of truth. Adding or removing an element automatically provisions or destroys the corresponding resource on the next ubx apply — no block duplication, no manual bookkeeping.The source code
How it works
The for_each list is resolved at compile time
The list
["assets", "logs", "uploads", "backups"] is Resolved<T> — it’s a literal known at compile time. ubx expands it into four IRUnit nodes in the IR pass, one per element.each.value is substituted per iteration
In each expanded node,
each.value is replaced with the corresponding list element: "assets", "logs", "uploads", "backups". The bucket name and tag values are computed for each iteration.What ubx generates
Common mistakes
Run it
What you learned
for_each over a list creates one resource per element, with each.value as the current itemThe list is the authoritative source — add or remove elements to provision or destroy resources
Renaming a list element triggers destroy-and-recreate; use
moved for safe renamesNext steps
for_each over an object
Use each.key and each.value with a map
count meta-argument
Create N copies of a resource by number
Full runnable example: github.com/ubiquex/ubx-examples/06-for-each-list

