Recall & Review
beginner
What is a dynamic block in Terraform?
A dynamic block lets you generate multiple nested blocks inside a resource or module based on a list or map. It helps create repeated configurations without writing them manually.
Click to reveal answer
beginner
Why use dynamic blocks in security groups?
Dynamic blocks allow you to create multiple security group rules easily from a list of rules, making your code cleaner and easier to manage.
Click to reveal answer
intermediate
In Terraform, what does the 'for_each' argument inside a dynamic block do?
'for_each' loops over a collection (list or map) and creates one nested block for each item in that collection.
Click to reveal answer
intermediate
How do you reference the current item inside a dynamic block?
You use the 'each.value' expression to access the current item in the loop inside the dynamic block.
Click to reveal answer
intermediate
Give a simple example of a dynamic block inside an AWS security group resource.
Inside the aws_security_group resource, you can use a dynamic block to create multiple ingress rules from a list, like this:
dynamic "ingress" {
for_each = var.ingress_rules
content {
from_port = each.value.from_port
to_port = each.value.to_port
protocol = each.value.protocol
cidr_blocks = each.value.cidr_blocks
}
}
Click to reveal answer
What is the main benefit of using dynamic blocks in Terraform security groups?
✗ Incorrect
Dynamic blocks let you generate repeated nested blocks based on a collection, reducing manual repetition.
Which keyword is used inside a dynamic block to loop over items?
✗ Incorrect
'for_each' is the correct keyword to loop over a list or map inside a dynamic block.
How do you access the current item in a dynamic block loop?
✗ Incorrect
'each.value' refers to the current element in the for_each loop inside the dynamic block.
What type of Terraform resource commonly uses dynamic blocks for multiple rules?
✗ Incorrect
aws_security_group often uses dynamic blocks to define multiple ingress or egress rules.
If you have a list of ingress rules, how does a dynamic block help?
✗ Incorrect
Dynamic blocks create one nested block for each item in the list, automating rule creation.
Explain how dynamic blocks work in Terraform security groups and why they are useful.
Think about how to avoid repeating similar blocks manually.
You got /4 concepts.
Describe a simple Terraform example using a dynamic block to create multiple ingress rules in an AWS security group.
Imagine you have a list of rules and want Terraform to create one ingress block per rule.
You got /4 concepts.