Interview Prep/Terraform/Loops, Conditionals, Dynamic Blocks and Functions

What is a `dynamic` block?

IntermediateConceptLoops, Conditionals, Dynamic Blocks and Functions
Answer

It generates repeated nested blocks from a collection.

resource "aws_security_group" "web" {
  name = "web"
  dynamic "ingress" {
    for_each = var.ingress_ports
    content {
      from_port   = ingress.value
      to_port     = ingress.value
      protocol    = "tcp"
      cidr_blocks = ["10.0.0.0/8"]
    }
  }
}

Only nested blocks can be dynamic, not top-level resources or plain attributes.

All Terraform questions Roadmap: Terraform

More on Loops, Conditionals, Dynamic Blocks and Functions