Expressions for Terraforms
Terraform expressions are how HCL computes values and makes configuration dynamic. They include references, conditionals, for expressions, built-in functions and templates. Meta-arguments such as count and for_each create several resources from one block, and dynamic blocks generate nested settings, so a single configuration can adapt to inputs without copy and paste.
Stage: Terraform Basics · Part of the Terraform roadmap · Last reviewed 19 November 2026
What to learn in Expressions
- Use count and for_each to create multiple similar resources
- Choose between count and for_each and explain the effect on resource addresses
- Transform a list or map with a for expression
- Use functions such as merge, lookup and cidrsubnet
- Generate repeated nested blocks with a dynamic block
Expressions interview questions
What is the difference between count and for_each?
count creates resources indexed by number, so removing an item from the middle of a list shifts later indexes and may cause replacement. for_each keys resources by map keys or set values, so each has a stable address. Prefer for_each when items have natural identifiers.
How do you write a conditional in Terraform?
Use the ternary expression condition ? true_value : false_value. It is commonly combined with count, for example count = var.enable_logging ? 1 : 0, to create a resource only when a flag is set. Conditionals choose values, they are not full if statements.
Where Expressions fits in the Terraform roadmap
Expressions belongs to the Terraform Basics stage (beginner level), which takes about 17 hours. Declare infrastructure.
Other units in this stage
Back to the full Terraform roadmap