How Terraform expressions compute values and when dynamic blocks help generate repeated nested configuration.
Terraform expressions compute or refer to values inside configuration. They include references, conditionals, for expressions, and built-in functions. HashiCorp’s expression reference is the authoritative overview.
1locals {
2 normalized_regions = toset([for region in var.regions : lower(region)])
3}
This transforms an input list into a set of lowercase region names; it shapes a declared value rather than adding imperative control flow. Use terraform console to test a transformation before embedding it in a complex configuration.
A dynamic block generates repeated nested blocks from a collection. It cannot generate arbitrary Terraform syntax or meta-arguments.
1dynamic "setting" {
2 for_each = var.settings
3 content {
4 name = setting.key
5 value = setting.value
6 }
7}
Dynamic blocks can simplify a reusable module’s narrow interface, but literal nested blocks are clearer when their number and shape are stable.