Skip to main content

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

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.

Back to the full Terraform roadmap