Question 1 · choose 2
A configuration contains: resource "aws_instance" "web" { count = 3 # ... } Which two expressions, written in an output block of the same module, are valid references to these instances' IDs? (Choose TWO.)
- Aaws_instance.web[0].id
- Baws_instance.web.id
- Caws_instance.web[count.index].id
- Dvalues(aws_instance.web)[0].id
- Eaws_instance.web[*].id
Show the answer and why
Aaws_instance.web[0].id
Correct
With count, each instance is addressed by its zero-based index.
Baws_instance.web.id
Incorrect
That form fits a resource without count or for_each. A resource with count has no single instance; an index or a splat is required.
Caws_instance.web[count.index].id
Incorrect
count.index exists only inside a block that sets count itself, such as another counted resource; an output block has no count, so the reference is an error there.
Dvalues(aws_instance.web)[0].id
Incorrect
values() takes a map or object, which is what a for_each resource produces. A count resource is a list of instances, so the call fails.
Eaws_instance.web[*].id
Correct
The splat expression returns a list of the id attribute of every instance.
count makes a list of instances: index one with [n], or take them all with [*]. count.index and map functions belong to other contexts.
HashiCorp documentation