TA-002-P · Question #365
How would you be able to reference an attribute from the vsphere_datacenter data source for use with the argument within the vsprere_folder resource in the following configuration?
The correct answer is D. data.vsphere_datacenter.dc.id. To reference an attribute from a Terraform data source, the syntax is data.data_source_type.data_source_local_name.attribute_name.
Question
How would you be able to reference an attribute from the vsphere_datacenter data source for use with the argument within the vsprere_folder resource in the following configuration?
Options
- Avsphere_datacenter.dc.id
- Bdata.vsphere_datacenter.dc
- Cdata.dc,id
- Ddata.vsphere_datacenter.dc.id
How the community answered
(25 responses)- B8% (2)
- C4% (1)
- D88% (22)
Why each option
To reference an attribute from a Terraform data source, the syntax is `data.data_source_type.data_source_local_name.attribute_name`.
This syntax is used for resources, not data sources; data sources require the `data.` prefix.
This references the entire data source object, not a specific attribute like `id`.
This syntax is incorrect; it misses the `data_source_type` and uses a comma instead of a dot.
In HCL, attributes from data sources are referenced using the syntax `data.data_source_type.local_name.attribute_name`. Therefore, to access the `id` attribute of a `vsphere_datacenter` data source named `dc`, the correct syntax is `data.vsphere_datacenter.dc.id`.
Concept tested: HCL data source attribute referencing syntax
Source: https://developer.hashicorp.com/terraform/language/expressions/references
Topics
Community Discussion
No community discussion yet for this question.