Dynamic Blocks and String Templates
Dynamic blocks generate repeated nested blocks, and string templates build text from values. This lesson covers both, with guidance on when to use them.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how dynamic blocks generate nested blocks and their limits.
- Interpret string interpolation, directives and heredocs.
Dynamic blocks
Some resource types configure repeated settings as nested blocks rather than arguments, such as ingress blocks in an AWS security group. Arguments can be set with expressions, but nested blocks cannot, so Terraform provides the dynamic block to generate them from a collection.
variable "ingress_ports" {
type = list(number)
default = [80, 443]
}
resource "aws_security_group" "web" {
name = "web"
vpc_id = var.vpc_id
dynamic "ingress" {
for_each = var.ingress_ports
content {
from_port = ingress.value
to_port = ingress.value
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
}The label of the dynamic block, ingress, names the nested block to generate. for_each gives the collection, and content holds the body of each generated block. Inside content, the iterator is named after the block by default, so ingress.key and ingress.value refer to the current element; the iterator argument chooses another name. Dynamic blocks can be nested.
The following diagram shows a dynamic block and the nested blocks it generates.

Dynamic blocks
Limits and good practice
Dynamic blocks generate nested blocks inside a resource, data source, provider or provisioner; they cannot generate meta-argument blocks such as lifecycle or provisioner, and they cannot create whole resources, which is the job of for_each on the resource itself. HashiCorp advises using them sparingly, because configuration full of dynamic blocks is hard to read; they are most useful in reusable modules that must accept a variable number of nested settings.
Nested blocks, not resources
To create several resources, put for_each or count on the resource. A dynamic block only repeats nested blocks inside one resource.
String templates and heredocs
Strings support interpolation with ${ ... }, which inserts the result of an expression, as in "${var.project}-logs". They also support directives with %{ ... } for conditionals and loops.
locals {
greeting = "Hello, %{ if var.name != "" }${var.name}%{ else }guest%{ endif }!"
hosts_file = <<-EOT
%{ for ip in var.server_ips ~}
${ip} server
%{ endfor ~}
EOT
}Heredoc strings hold multi-line text between <<EOT and EOT; the indented form <<-EOT removes leading indentation. A tilde in a directive, ~}, strips the adjacent whitespace and newline. For longer templates, keep the text in a separate file and render it with templatefile("${path.module}/init.tftpl", { port = 8080 }). To write a literal ${, escape it as $${.
Template syntax
A variable number of rules
A shared module creates a security group. Some callers need one ingress rule, others ten, and each rule has a port and a list of CIDR blocks. How should the module accept and create the rules?
The decisive fact is that ingress rules are nested blocks of one resource.
The module declares variable "rules" { type = list(object({ port = number, cidrs = list(string) })) } and uses a dynamic "ingress" block with for_each = var.rules and content that reads ingress.value.port and ingress.value.cidrs. Each caller then passes as many rules as it needs. An alternative with the AWS provider is one aws_vpc_security_group_ingress_rule resource per rule, created with for_each, which avoids nested blocks entirely.
Inside dynamic "setting" with no iterator argument, how do you refer to the current element's value?
Show answer
setting.value, because the iterator is named after the dynamic block's label by default.
Exam Tips
Remember for the exam
- dynamic generates nested blocks with for_each and content.
- The iterator defaults to the block label: ingress.value.
- dynamic cannot generate lifecycle or whole resources.
- ${ } interpolates; %{ } directives loop and branch; templatefile renders files.