A dynamic block lets you generate multiple nested blocks inside a resource or module based on a list or map. It helps when the number of nested blocks is not fixed and depends on input data.
Click to reveal answer
beginner
What does for_each do in Terraform?
for_each lets you create multiple instances of a resource or module, one for each item in a map or set. It is used to manage multiple similar resources efficiently.
Click to reveal answer
intermediate
When should you use a dynamic block instead of for_each?
Use a dynamic block when you need to create multiple nested blocks inside a single resource or module instance, especially when the nested blocks vary in number or content.
Click to reveal answer
intermediate
When is for_each preferred over dynamic blocks?
Use for_each when you want to create multiple separate resource or module instances, each representing a distinct item, rather than multiple nested blocks inside one resource.
Click to reveal answer
advanced
Can you combine dynamic blocks and for_each in Terraform?
Yes, you can use for_each to create multiple resource instances and inside each resource use dynamic blocks to generate nested blocks based on data. This allows flexible and scalable configurations.
Click to reveal answer
What does a dynamic block in Terraform generate?
AA single resource with fixed blocks
BMultiple resource instances
CMultiple nested blocks inside a single resource
DA variable declaration
✗ Incorrect
Dynamic blocks generate multiple nested blocks inside one resource based on input data.
Which Terraform feature creates multiple resource instances from a map or set?
Alocals
Bdynamic block
Ccount
Dfor_each
✗ Incorrect
for_each creates multiple resource or module instances, one per item.
When should you use for_each instead of dynamic blocks?
ATo create multiple nested blocks inside one resource
BTo create multiple separate resource instances
CTo define variables
DTo create output values
✗ Incorrect
Use for_each to create multiple separate resource instances.
Can dynamic blocks be used inside resources created with for_each?
AYes, dynamic blocks can be used inside each resource instance
BOnly in modules, not resources
COnly with count, not for_each
DNo, they cannot be combined
✗ Incorrect
Dynamic blocks can be used inside each resource instance created by for_each.
Assuming var.instances is a map with nested ebs_volumes lists.
medium
A. Dynamic blocks cannot be nested inside resources
B. for_each cannot be used on resources
C. Incorrect reference to dynamic block variable inside content
D. Missing count argument on resource
Solution
Step 1: Identify the error in dynamic block reference
Inside the content block of a dynamic block, the iterator must be referenced as each.value, not ebs_block_device.value. The iterator variable is always each regardless of the dynamic block name.
Step 2: Confirm the fix
Change to device_name = each.value.device_name and volume_size = each.value.size. Other options are incorrect: dynamic blocks work inside resources, for_each works on resources, count is not required.
Final Answer:
Incorrect reference to dynamic block variable inside content -> Option C
Quick Check:
Dynamic block content uses each.value [OK]
Hint: Inside dynamic content, always use each.value, not block_name.value [OK]
Common Mistakes:
Using block_name.value inside dynamic block content instead of each.value
Trying to use for_each on resource incorrectly
Forgetting to define for_each on dynamic block
5. You want to create multiple AWS IAM users, each with multiple inline policies. Which Terraform approach is best to manage this efficiently?
A snippet of your variables:
variable "users" {
type = map(object({
policies = list(object({
name = string
policy = string
}))
}))
}
hard
A. Use for_each on aws_iam_user for users, and for_each on aws_iam_user_policy for policies
B. Use count on aws_iam_user for users, and for_each on aws_iam_user_policy for policies
C. Use for_each on aws_iam_user resource for users, and dynamic blocks inside aws_iam_user_policy for policies
D. Use dynamic blocks on aws_iam_user resource for users, and count on aws_iam_user_policy for policies
Solution
Step 1: Manage multiple users
Use for_each on aws_iam_user resource to create one user per map entry.
Step 2: Manage multiple policies per user
Since policies are separate resources (aws_iam_user_policy), use for_each on that resource to create one policy per user-policy pair.
Step 3: Why not dynamic blocks here?
Dynamic blocks repeat nested blocks inside a resource, but aws_iam_user_policy is a separate resource, so for_each is appropriate.
Final Answer:
Use for_each on aws_iam_user for users, and for_each on aws_iam_user_policy for policies -> Option A
Quick Check:
Multiple resources = for_each on both [OK]
Hint: Use for_each for multiple resources, dynamic blocks for nested blocks [OK]
Common Mistakes:
Using dynamic blocks to create multiple resources
Using count instead of for_each for maps
Trying to nest policies inside user resource with dynamic blocks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.