Skip to main content
0 reactions

How to Use for Expressions in OpenTofu

Card titled for Expressions, showing a for expression over var.tasks that keeps task.name if task.done, and the result finished_tasks holding Write the plan.
Turn one list into another, and keep only the items you want with if

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

SyntaxHCL
[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

Step 1: Write the Expressions

In an empty folder, create main.tf with a list of tasks and three outputs built from it:

main.tfHCL
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_names keeps the name of every task.
  • finished_tasks keeps a name only if task.done.
  • task_status uses braces and => to make a map from each name to its done value.

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:

TerminalBash
tofu apply
Output (the lines that matter)Plain text
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

MistakeWhat happensDo this instead
Using = instead of => in bracesOpenTofu stops with a syntax error.Write <KEY> => <VALUE>.
Two items giving the same key in bracesOpenTofu 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.

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