0
0
Terraformcloud~5 mins

Count vs for_each decision in Terraform - Quick Revision & Key Differences

Choose your learning style9 modes available
Recall & Review
beginner
What is the main difference between count and for_each in Terraform?

count creates multiple instances based on a number, while for_each creates instances based on each item in a map or set.

Click to reveal answer
beginner
When should you prefer for_each over count?

Use for_each when you want to create resources with unique keys or names, especially when working with maps or sets.

Click to reveal answer
beginner
Can count be used with maps or sets directly?

No, count only accepts a number. To use maps or sets, for_each is required.

Click to reveal answer
intermediate
How does resource addressing differ between count and for_each?

count uses numeric indexes (e.g., resource[0]), while for_each uses keys from the map or set (e.g., resource["key"]).

Click to reveal answer
intermediate
What is a common pitfall when using count with conditional expressions?

Changing count dynamically can cause resource replacement because indexes shift, leading to unexpected resource destruction and recreation.

Click to reveal answer
Which Terraform argument allows creating resources based on keys from a map or set?
Afor_each
Bcount
Cdepends_on
Dlifecycle
What type of value does count accept?
ANumber
BMap
CSet
DList
If you want to create three identical resources, which is simpler to use?
Afor_each with a map of three keys
Bcount = 3
Cdepends_on
Dprovider block
What happens if you change the order of items in a for_each map?
ATerraform throws an error
BResources get replaced due to index shift
CResources keep their keys and do not get replaced
DResources are duplicated
Which method is better for resources that need unique names or identifiers?
Adepends_on
Bcount
Cvariable
Dfor_each
Explain when and why you would choose count versus for_each in Terraform.
Think about whether you need unique keys or just a number of copies.
You got /4 concepts.
    Describe how resource addressing differs between count and for_each and why this matters.
    Consider how Terraform tracks each resource instance.
    You got /4 concepts.