Challenge - 5 Problems
Dynamic Block Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Configuration
intermediate2:00remaining
Understanding dynamic block output count
Given the following Terraform snippet using a dynamic block, how many
ingress blocks will be created in the resulting resource?Terraform
resource "aws_security_group" "example" { name = "example" dynamic "ingress" { for_each = ["80", "443"] content { from_port = tonumber(ingress.value) to_port = tonumber(ingress.value) protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } } }
Attempts:
2 left
💡 Hint
Count the number of items in the for_each list.
✗ Incorrect
The dynamic block iterates over the list ["80", "443"], creating one ingress block per item, so 2 blocks total.
❓ service_behavior
intermediate2:00remaining
Effect of empty for_each in dynamic block
What happens when a dynamic block's
for_each expression evaluates to an empty list in Terraform?Terraform
resource "aws_security_group" "example" { name = "example" dynamic "egress" { for_each = [] content { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } } }
Attempts:
2 left
💡 Hint
Think about what an empty list means for iteration.
✗ Incorrect
An empty for_each means no iterations, so no blocks are created inside the resource.
❓ Architecture
advanced3:00remaining
Dynamic block usage for multiple nested blocks
You want to create an AWS security group with multiple
ingress rules, each having multiple cidr_blocks. Which dynamic block structure correctly nests the cidr_blocks inside each ingress rule?Attempts:
2 left
💡 Hint
Remember that cidr_blocks is a list attribute, not a nested block.
✗ Incorrect
The
cidr_blocks attribute accepts a list of strings directly. Using a dynamic block inside ingress for cidr_blocks is invalid syntax.❓ security
advanced2:30remaining
Security risk of incorrect dynamic block usage
If a dynamic block for
ingress rules in a security group mistakenly uses a static cidr_blocks value instead of iterating over a variable list, what is the likely security impact?Attempts:
2 left
💡 Hint
Consider what happens if the dynamic block does not iterate over all intended CIDRs.
✗ Incorrect
Using a static CIDR means only that IP range is allowed, which may block legitimate traffic if multiple CIDRs were intended.
✅ Best Practice
expert3:00remaining
Optimizing dynamic blocks for maintainability
Which approach best improves maintainability when using dynamic blocks to create multiple similar nested blocks in Terraform?
Attempts:
2 left
💡 Hint
Think about how to keep code DRY and easy to update.
✗ Incorrect
Using a single dynamic block with a structured variable map allows easy updates and reduces code duplication.