Which statement best explains why dynamic blocks in Terraform reduce repetition?
Think about how you can generate repeated blocks from data structures instead of copying code.
Dynamic blocks let you loop over collections to create multiple nested blocks from one template, reducing manual repetition and making configurations cleaner.
Given this Terraform snippet using a dynamic block, how many ingress blocks will be created?
resource "aws_security_group" "example" {
name = "example"
dynamic "ingress" {
for_each = var.rules
content {
from_port = ingress.value.from_port
to_port = ingress.value.to_port
protocol = ingress.value.protocol
cidr_blocks = ingress.value.cidr_blocks
}
}
}Assuming var.rules is a list with 3 elements.
Dynamic blocks create one nested block per item in the collection.
The dynamic block iterates over var.rules which has 3 elements, so it creates 3 ingress blocks.
You are designing a Terraform module to create firewall rules. You want to support any number of rules without repeating code. Which approach best uses dynamic blocks to achieve this?
Think about how to generate multiple nested blocks inside one resource dynamically.
Dynamic blocks with for_each allow the module to generate any number of nested rule blocks based on input, making the module flexible and reusable.
When using dynamic blocks to create multiple nested blocks in a Terraform resource, what is the effect on the deployed infrastructure?
Consider how Terraform translates configuration into real infrastructure changes.
Dynamic blocks generate multiple nested blocks in the resource configuration, so the deployed infrastructure reflects all those nested configurations.
What is a potential security risk when using dynamic blocks in Terraform to generate firewall rules from user input without validation?
Think about what happens if user data controls security settings without checks.
If user input is used directly in dynamic blocks for firewall rules without validation, it can create rules that allow unwanted traffic, risking security breaches.