Challenge - 5 Problems
Dynamic Blocks Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of Terraform plan with dynamic ingress blocks
Given the Terraform snippet below, what will be the number of ingress rules created in the security group after running
terraform plan?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 var.ports and creates one ingress block per port. Since the list has three ports, three ingress rules are created.
❓ Configuration
intermediate2:00remaining
Correct dynamic block syntax for multiple CIDR blocks
You want to create ingress rules for port 8080 but allow access from multiple CIDR blocks dynamically. Which Terraform code snippet correctly uses a dynamic block to achieve this?
Attempts:
2 left
💡 Hint
Remember that
cidr_blocks expects a list of strings.✗ Incorrect
Option C correctly uses a dynamic block to create one ingress rule per CIDR block by wrapping ingress.value in a list. Option C is incorrect because cidr_blocks expects a list, not a string.
❓ Troubleshoot
advanced2:00remaining
Error caused by incorrect dynamic block usage in ingress
What error will Terraform produce when applying the following resource configuration?
Terraform
resource "aws_security_group" "bad_example" { name = "bad-example" dynamic "ingress" { for_each = var.ports content { from_port = ingress.value to_port = ingress.value protocol = "tcp" cidr_blocks = ingress.value } } }
Attempts:
2 left
💡 Hint
Check the expected type of the cidr_blocks attribute.
✗ Incorrect
The cidr_blocks attribute expects a list of strings. Here, ingress.value is a number (port), so Terraform raises a type error.
🔀 Workflow
advanced2:00remaining
Best workflow to update ingress rules dynamically
You have a Terraform configuration with dynamic ingress blocks based on a variable list of ports. You want to add port 3306 to the list and apply changes safely. What is the best sequence of commands to update your infrastructure?
Attempts:
2 left
💡 Hint
Think about safe infrastructure changes with Terraform.
✗ Incorrect
Updating the variable, then running terraform plan to preview changes before applying is the safest and recommended workflow.
🧠 Conceptual
expert2:00remaining
Why use dynamic blocks for ingress rules in Terraform?
Which of the following is the main advantage of using dynamic blocks for ingress rules in Terraform security groups?
Attempts:
2 left
💡 Hint
Think about code reuse and flexibility in Terraform configurations.
✗ Incorrect
Dynamic blocks let you generate multiple nested blocks from a list or map variable, reducing repetition and making configurations flexible.