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?
ATo speed up Terraform apply by skipping validations
BTo create multiple repeated blocks from a list or map automatically
CTo encrypt security group rules
DTo create security groups without specifying rules
✗ 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?
Afor_each
Bloop
Crepeat
Diterate
✗ 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?
Aeach.value
Bitem.value
Ccurrent.value
Dthis.value
✗ 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?
Aaws_s3_bucket
Baws_instance
Caws_lambda_function
Daws_security_group
✗ 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?
AIt disables ingress rules
BIt merges all rules into one block
CIt creates one ingress block per rule automatically
DIt deletes all rules before applying
✗ 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.
Practice
(1/5)
1. What is the main purpose of using dynamic blocks in Terraform security groups?
easy
A. To delete all security group rules at once
B. To manually write each security group rule one by one
C. To encrypt security group rules for security
D. To create multiple similar security group rules from a list automatically
Solution
Step 1: Understand the role of dynamic blocks
Dynamic blocks allow repeating a block of code multiple times based on a list or map input.
Step 2: Apply this to security groups
In security groups, dynamic blocks help create many similar rules without writing each rule manually.
Final Answer:
To create multiple similar security group rules from a list automatically -> Option D
Quick Check:
Dynamic blocks automate repeated blocks [OK]
Hint: Dynamic blocks repeat code for lists automatically [OK]
Common Mistakes:
Thinking dynamic blocks delete rules
Believing dynamic blocks encrypt rules
Confusing dynamic blocks with manual rule writing
2. Which of the following is the correct syntax to start a dynamic block named ingress in a Terraform security group resource?
easy
A. dynamic ingress {
B. dynamic "ingress" {
C. dynamic ingress() {
D. dynamic "ingress"() {
Solution
Step 1: Recall Terraform dynamic block syntax
Dynamic blocks use the keyword dynamic followed by the block name in quotes and curly braces.
Step 2: Match syntax for ingress block
The correct syntax is dynamic "ingress" { to define a dynamic ingress block.
Final Answer:
dynamic "ingress" { -> Option B
Quick Check:
Dynamic block name must be in quotes [OK]
Hint: Use quotes around block name in dynamic block [OK]
Common Mistakes:
Omitting quotes around block name
Adding parentheses after block name
Using incorrect keywords
3. Given the following Terraform snippet, what will be the number of ingress rules created?
Inside a dynamic block named "ingress", the loop variable is ingress, not rule.
Step 2: Correct variable reference
References should be ingress.value.from and ingress.value.to to access current item values.
Final Answer:
Incorrect reference to loop variable; should use ingress.value instead of rule.value -> Option C
Quick Check:
Loop variable matches dynamic block name [OK]
Hint: Loop variable = dynamic block name [OK]
Common Mistakes:
Using wrong loop variable name
Omitting quotes around dynamic block name
Misunderstanding for_each data types
5. You want to create a security group with dynamic ingress rules from a list of maps, but only include rules where enabled is true. Which Terraform code snippet correctly uses a dynamic block with filtering?