Which expression references the name of the first aws_instance.web in Terraform?

Prepare for the HashiCorp Terraform Associate Exam with quizzes, flashcards, and multiple-choice questions. Each question includes hints and explanations. Boost your confidence and ace your exam!

Multiple Choice

Which expression references the name of the first aws_instance.web in Terraform?

Explanation:
When you create multiple instances with count, Terraform treats them as a list and you address a specific one by its index. The first instance is at index 0, so to get an attribute from that instance you append [0] and then the attribute name. The best expression to reference the first aws_instance.web’s name is aws_instance.web[0].name. It directly targets the first instance and then accesses its name attribute in a clean, idiomatic way. Using a different index would pull a different instance (not the first). Referencing the resource without an attribute would give you the object itself, not its specific attribute. The element(...) form is possible, but the bracket notation is the most straightforward and commonly used approach.

When you create multiple instances with count, Terraform treats them as a list and you address a specific one by its index. The first instance is at index 0, so to get an attribute from that instance you append [0] and then the attribute name.

The best expression to reference the first aws_instance.web’s name is aws_instance.web[0].name. It directly targets the first instance and then accesses its name attribute in a clean, idiomatic way.

Using a different index would pull a different instance (not the first). Referencing the resource without an attribute would give you the object itself, not its specific attribute. The element(...) form is possible, but the bracket notation is the most straightforward and commonly used approach.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy