How would you reference the name value of the second aws_instance.web instance?

Prepare for the HashiCorp Terraform Associate Exam. Utilize flashcards and multiple-choice questions with detailed hints and explanations. Boost your confidence and be ready for success!

Multiple Choice

How would you reference the name value of the second aws_instance.web instance?

Explanation:
When you create multiple instances with count, Terraform exposes them as a list under the resource name. To get the second instance, you reference index 1, and to read a specific attribute of that instance you append .name. So aws_instance.web[1].name returns the name value of the second aws_instance.web instance. The other forms don’t target the second instance’s attribute correctly: element(aws_instance.web, 2) isn’t the right way to pick a property from a resource with count, aws_instance.web[1] would give you the second instance object but not its name, and aws_instance.web[0].name would give the first instance’s name, not the second.

When you create multiple instances with count, Terraform exposes them as a list under the resource name. To get the second instance, you reference index 1, and to read a specific attribute of that instance you append .name. So aws_instance.web[1].name returns the name value of the second aws_instance.web instance.

The other forms don’t target the second instance’s attribute correctly: element(aws_instance.web, 2) isn’t the right way to pick a property from a resource with count, aws_instance.web[1] would give you the second instance object but not its name, and aws_instance.web[0].name would give the first instance’s name, not the second.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy