Skip to main content
Conditional expressions in Terraform let you choose one value or another based on a boolean condition — essentially Terraform’s ternary operator. This is useful for selecting regions, SKUs, sizes, or toggling small feature differences without duplicating resources or maintaining separate files per environment. Example — choose region based on environment:
The syntax is: condition ? value_if_true : value_if_false. Use conditional expressions to keep configuration DRY and to decide values dynamically during terraform plan and terraform apply.
Use conditional expressions to keep code clean and let Terraform compute configuration values (regions, SKUs, toggles) at plan/apply time.
This article also covers for expressions and how they interact with conditional expressions and for_each. Example showing provider, variable, and locals with a conditional:
Generating resource-group names with for expressions Two common patterns generate combinations (environments × apps):
  1. Nested for expression that returns a list of lists (nested list)
local.rg_nested becomes a list with inner lists (one per environment), for example: [["rg-dev-api","rg-dev-web","rg-dev-db"], ["rg-prod-api","rg-prod-web","rg-prod-db"]].
  1. Single for expression with two iterators that returns a flat list
local.rg_flat produces a flat list: ["rg-dev-api","rg-dev-web","rg-dev-db","rg-prod-api","rg-prod-web","rg-prod-db"]. Using the generated names as resources Terraform for_each supports maps and sets of strings. If you have a nested list (rg_nested), flatten it before converting to a set. Example using the nested list:
If you produce a flat list (rg_flat), you can use it directly:
Why flatten? Example error when using a nested list directly If you use the nested list without flatten, for_each will fail during terraform plan:
This error occurs because the nested for expression produced a list that contains inner lists. flatten() converts a nested list into a single flat list, which toset() can then convert into a set of strings acceptable to for_each.
If you produce nested lists, call flatten() before toset() when using for_each. Otherwise Terraform will complain that the argument type is not a set of strings.
Plan output and environment overrides With the default environment = "dev", local.location will compute to "westus", so all resource groups are planned for West US. Example plan (after using flatten or rg_flat):
Override the environment at plan time to change locations:
Now local.location evaluates to "eastus" and the plan shows East US locations:
Quick reference Summary
  • Conditional expressions are Terraform’s ternary operator — useful for environment-specific values and small toggles.
  • for expressions build lists. Use a double for to produce a flat list directly, or nest for expressions to create grouped lists.
  • If you have nested lists, use flatten() before toset() so for_each receives a set of strings.
  • Override variables at plan/apply time (e.g., -var='environment=prod') to affect computed locals like local.location.
Links and references

Watch Video

Practice Lab