Challenge - 5 Problems
Dynamic Blocks Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Configuration
intermediate2:00remaining
Identify the output of this Terraform security group with dynamic blocks
Given the following Terraform code snippet, what will be the number of ingress rules created in the security group?
Terraform
variable "ports" { type = list(number) default = [22, 80, 443] } resource "aws_security_group" "example" { name = "example-sg" dynamic "ingress" { for_each = var.ports content { from_port = ingress.value to_port = ingress.value protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } } }
Attempts:
2 left
💡 Hint
Think about how dynamic blocks iterate over lists to create multiple nested blocks.
✗ Incorrect
The dynamic block iterates over the list of ports and creates one ingress block per port. Each block has from_port and to_port set to the current port value, resulting in 3 separate ingress rules.
❓ service_behavior
intermediate2:00remaining
What happens if the dynamic block's for_each is an empty list?
Consider the same Terraform security group code but with variable ports set to an empty list. What will be the behavior after applying?
Terraform
variable "ports" { type = list(number) default = [] } resource "aws_security_group" "example" { name = "example-sg" dynamic "ingress" { for_each = var.ports content { from_port = ingress.value to_port = ingress.value protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } } }
Attempts:
2 left
💡 Hint
What does an empty list mean for dynamic blocks in Terraform?
✗ Incorrect
When for_each is an empty list, the dynamic block creates zero nested blocks. Thus, no ingress rules are created.
❓ Architecture
advanced2:30remaining
Choose the correct Terraform code to create multiple egress rules dynamically
You want to create an AWS security group with multiple egress rules for ports 53 and 123 using dynamic blocks. Which Terraform snippet correctly achieves this?
Attempts:
2 left
💡 Hint
Remember that for_each over a list uses .value for the current item, and protocol must match the port's protocol.
✗ Incorrect
Option A correctly uses for_each over a list and sets from_port and to_port to egress.value. Protocol is udp, matching ports 53 and 123. Option A incorrectly uses egress.key which is invalid for list iteration. Option A uses a map but correctly assigns protocol from value. Option A uses tcp protocol which is incorrect for these ports.
❓ security
advanced2:00remaining
What security risk arises if dynamic blocks in security groups use cidr_blocks = ["0.0.0.0/0"] indiscriminately?
If a Terraform security group uses dynamic blocks to create multiple ingress rules all allowing traffic from 0.0.0.0/0, what is the main security concern?
Attempts:
2 left
💡 Hint
Think about what 0.0.0.0/0 means in network terms.
✗ Incorrect
CIDR block 0.0.0.0/0 means all IPv4 addresses. Allowing ingress from this range means anyone on the internet can access the ports, which is a security risk.
✅ Best Practice
expert3:00remaining
Which Terraform pattern best avoids duplication when creating multiple similar ingress rules with dynamic blocks?
You need to create multiple ingress rules with different ports and CIDR blocks. Which approach follows best practice to keep Terraform code clean and maintainable?
Attempts:
2 left
💡 Hint
Think about how to combine related data to simplify iteration.
✗ Incorrect
Option B uses a list of objects combining port and CIDR block, allowing a single dynamic block to create all ingress rules cleanly. Option B duplicates code and is hard to maintain. Option B risks mismatched iterations. Option B is inflexible and does not support multiple rules.