You want to create 3 identical EC2 instances with different names using a single `resource` block. Which Terraform meta-argument is most appropriate?

Direct Answer (Verified)Reviewed by Sunny (Senior Cloud Engineer)

Correct Answer: Either B or C works equally well for this use case

Both `count` and `for_each` can create multiple instances. `count` with `count.index` works but produces instances addressed as `aws_instance.web[0]`, `aws_instance.web[1]`, etc. —...

Real Exam Scenario

You want to create 3 identical EC2 instances with different names using a single `resource` block. Which Terraform meta-argument is most appropriate?

✓ Correct Answer & Explanation

Correct Answer

Either B or C works equally well for this use case

Why This Answer Is Correct

Both `count` and `for_each` can create multiple instances. `count` with `count.index` works but produces instances addressed as `aws_instance.web[0]`, `aws_instance.web[1]`, etc. — removing middle elements shifts indices and can trigger unintended replacements. `for_each` with a set of names (e.g., `toset(["web-1","web-2","web-3"])`) addresses by key, making removals safer. For named resources, `for_each` is generally preferred.