Challenge - 5 Problems
Nested Dynamic Blocks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Configuration
intermediate2:00remaining
Output of nested dynamic blocks in Terraform
Given the following Terraform configuration snippet using nested dynamic blocks, what will be the value of the resource's
tags attribute after deployment?Terraform
resource "aws_instance" "example" { ami = "ami-123456" instance_type = "t2.micro" dynamic "tags" { for_each = { env = "prod", team = "devops" } content { key = tags.key value = tags.value dynamic "subtag" { for_each = { owner = "alice", project = "cloud" } content { key = subtag.key value = subtag.value } } } } }
Attempts:
2 left
💡 Hint
Remember that nested dynamic blocks create nested lists or maps in the resource attributes.
✗ Incorrect
The outer dynamic block iterates over two tags, each producing a map with key and value. The inner dynamic block adds a list of subtag maps inside each tag. This results in a list of tag objects, each containing a list of subtag objects.
❓ service_behavior
intermediate2:00remaining
Behavior of nested dynamic blocks with empty inner for_each
In Terraform, if the inner nested dynamic block's
for_each is an empty map, what will be the effect on the resource attribute generated by the nested dynamic blocks?Terraform
resource "aws_security_group" "example" { name = "example" dynamic "ingress" { for_each = { http = 80, https = 443 } content { from_port = ingress.value to_port = ingress.value protocol = "tcp" dynamic "cidr_blocks" { for_each = {} content { cidr_block = cidr_blocks.value } } } } }
Attempts:
2 left
💡 Hint
Empty for_each in a dynamic block results in no blocks generated for that level.
✗ Incorrect
When the inner dynamic block's for_each is empty, no cidr_blocks blocks are created inside each ingress block. The ingress blocks still exist but without cidr_blocks attribute.
❓ Architecture
advanced2:30remaining
Designing nested dynamic blocks for multi-tier infrastructure
You want to use Terraform nested dynamic blocks to define a multi-tier AWS security group where each tier has multiple ingress rules and each ingress rule can have multiple CIDR blocks. Which structure best represents this nested dynamic block usage?
Attempts:
2 left
💡 Hint
Think about the hierarchy: tiers contain ingress rules, which contain CIDR blocks.
✗ Incorrect
The correct nesting order is tiers > ingress rules > CIDR blocks, so nested dynamic blocks should follow this order to represent the hierarchy properly.
❓ security
advanced2:30remaining
Security implications of nested dynamic blocks in Terraform
What is a potential security risk when using nested dynamic blocks to generate AWS IAM policies dynamically from user input variables?
Attempts:
2 left
💡 Hint
Consider what happens if user input is not controlled or sanitized.
✗ Incorrect
If user input variables are used directly in nested dynamic blocks to build IAM policies, malicious or incorrect input can create overly permissive policies, risking privilege escalation.
✅ Best Practice
expert3:00remaining
Optimizing nested dynamic blocks for maintainability and performance
Which practice is best to optimize Terraform configurations that use deeply nested dynamic blocks to manage complex resources?
Attempts:
2 left
💡 Hint
Think about code reuse and readability in large Terraform projects.
✗ Incorrect
Breaking complex nested dynamic blocks into smaller reusable modules and using locals to simplify expressions improves maintainability and reduces errors.