Skill 4.5.2

Dynamic blocks and string templates

Key Concepts

Some resources configure repeated settings as nested blocks, such as ingress rules in a security group. A dynamic "ingress" block with for_each and a content block generates one nested block per element. Inside content, the iterator is named after the block by default, as in ingress.value.port, or by the iterator argument. Dynamic blocks cannot generate meta-argument blocks such as lifecycle, and overusing them makes configuration hard to read.

String templates use ${ } interpolation, such as "web-${var.env}", and %{ } directives for conditionals and loops, such as %{ for ip in var.ips }${ip}\n%{ endfor }. Heredoc strings (<<EOT, or <<-EOT to strip indentation) hold multi-line text, and templatefile() renders a template stored in a file.