A for expression builds a new list or map from an existing one: it goes through each item and keeps a value for it, and it can skip items with an if clause. This guide turns a to-do list into three different results.
Syntax
[for <ITEM> in <COLLECTION> : <RESULT> if <CONDITION>]
{for <ITEM> in <COLLECTION> : <KEY> => <VALUE> if <CONDITION>}
- Square brackets
[ ]build a list (a tuple); curly braces{ }build a map (an object). <ITEM>: a name for the current item, used in the rest of the expression.<INDEX>, <ITEM>also gives the position (or the key, for a map).<COLLECTION>: a list, set, tuple, map or object.<RESULT>, or<KEY> => <VALUE>in braces: what to keep for each item.if <CONDITION>: optional; keeps only the items for which it’s true.
The full rules are in for Expressions (opens in a new tab) in the OpenTofu® docs.
Prerequisites
- OpenTofu installed. Installing OpenTofu (opens in a new tab) in the OpenTofu docs covers every system.
Step 1: Write the Expressions
In an empty folder, create main.tf with a list of tasks and three outputs built from it:
variable "tasks" {
description = "To-do items and whether each one is done."
type = list(object({
name = string
done = bool
}))
default = [
{ name = "Write the plan", done = true },
{ name = "Review the plan", done = false },
{ name = "Apply the plan", done = false },
]
}
output "task_names" {
value = [for task in var.tasks : task.name]
}
output "finished_tasks" {
value = [for task in var.tasks : task.name if task.done]
}
output "task_status" {
value = { for task in var.tasks : task.name => task.done }
}
task_nameskeeps the name of every task.finished_taskskeeps a name onlyif task.done.task_statususes braces and=>to make a map from each name to itsdonevalue.
Step 2: Apply It
This configuration uses no providers, so there’s nothing for tofu init to download: tofu apply works straight away. Run it and type yes:
tofu apply
Outputs:
finished_tasks = [
"Write the plan",
]
task_names = [
"Write the plan",
"Review the plan",
"Apply the plan",
]
task_status = {
"Apply the plan" = false
"Review the plan" = false
"Write the plan" = true
}
The map lists its keys in alphabetical order: the docs say a map or object is always sorted by key.
Clean Up
This example created nothing but a state file. Delete the folder when you’re done.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using = instead of => in braces | OpenTofu stops with a syntax error. | Write <KEY> => <VALUE>. |
| Two items giving the same key in braces | OpenTofu stops: map keys must be unique. | Pick a unique key, or add ... after the value to group them, as the docs show. |
FAQ
Can I use a for expression to make blocks?
No. It builds values. To repeat nested blocks, the docs point to dynamic blocks.
When should I use a splat expression instead?
When you only take one attribute from every item of a list, var.tasks[*].name is the shorter form of the first output.
Conclusion
A for expression turns one collection into another: square brackets for a list, braces and => for a map, and if to keep only some items.
Related
- OpenTofu Tutorial — Part 5: Locals, Expressions and tofu console
- How to Use Splat Expressions in OpenTofu
- How to Use Conditional Expressions in OpenTofu
Additional Resources
OpenTofu® is a registered trademark of LF Projects, LLC. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by the OpenTofu project.
0 comments