terraform plan?variable "ports" { type = list(number) default = [22, 80, 443] } resource "aws_security_group" "example" { name = "example-sg" dynamic "ingress" { for_each = var.ports content { from_port = ingress.value to_port = ingress.value protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } } }
The dynamic block iterates over the list var.ports and creates one ingress block per port. Since the list has three ports, three ingress rules are created.
cidr_blocks expects a list of strings.Option C correctly uses a dynamic block to create one ingress rule per CIDR block by wrapping ingress.value in a list. Option C is incorrect because cidr_blocks expects a list, not a string.
resource "aws_security_group" "bad_example" { name = "bad-example" dynamic "ingress" { for_each = var.ports content { from_port = ingress.value to_port = ingress.value protocol = "tcp" cidr_blocks = ingress.value } } }
The cidr_blocks attribute expects a list of strings. Here, ingress.value is a number (port), so Terraform raises a type error.
Updating the variable, then running terraform plan to preview changes before applying is the safest and recommended workflow.
Dynamic blocks let you generate multiple nested blocks from a list or map variable, reducing repetition and making configurations flexible.
