0
0
Terraformcloud~5 mins

Dynamic block syntax in Terraform - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a dynamic block in Terraform?
A dynamic block lets you generate multiple nested blocks inside a resource or module based on a list or map, making configuration flexible and reusable.
Click to reveal answer
beginner
Which keyword starts a dynamic block in Terraform?
The keyword dynamic starts a dynamic block, followed by the block type in quotes, like dynamic "ingress".
Click to reveal answer
intermediate
What are the two main parts inside a dynamic block?
Inside a dynamic block, you have for_each to loop over a collection, and content which defines the block structure to repeat.
Click to reveal answer
intermediate
Why use dynamic blocks instead of static blocks?
Dynamic blocks reduce repetition by generating multiple blocks from data, making configurations easier to maintain and adapt to changing inputs.
Click to reveal answer
advanced
Show a simple example of a dynamic block creating multiple ingress rules.
Example:<br>
dynamic "ingress" {
  for_each = var.ingress_rules
  content {
    from_port   = ingress.value.from_port
    to_port     = ingress.value.to_port
    protocol    = ingress.value.protocol
    cidr_blocks = ingress.value.cidr_blocks
  }
}
Click to reveal answer
What does the for_each attribute inside a dynamic block do?
ALoops over a collection to create multiple nested blocks
BDefines the type of resource to create
CSpecifies the provider for the resource
DSets the block's name
Which part of a dynamic block defines the repeated block's structure?
Ablock_type
Bfor_each
Cvariable
Dcontent
True or False: Dynamic blocks can only be used inside resource blocks.
ATrue
BFalse
COnly in modules
DOnly in variables
What happens if the for_each collection is empty in a dynamic block?
AOne empty block is created
BTerraform throws an error
CNo nested blocks are created
DTerraform ignores the resource
Which of these is a benefit of using dynamic blocks?
AAvoids repeating similar blocks manually
BIncreases Terraform plan time significantly
CRequires writing more code
DLimits resource scalability
Explain how a dynamic block works in Terraform and when you would use it.
Think about how to create multiple similar blocks from a list or map.
You got /4 concepts.
    Describe the structure of a dynamic block with an example of creating multiple ingress rules.
    Imagine you want to add many firewall rules without writing each one manually.
    You got /4 concepts.