nerdexam
HashiCorp

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.

Read, generate, and modify configuration

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)
  • B
    8% (2)
  • C
    4% (1)
  • D
    88% (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`.

Avsphere_datacenter.dc.id

This syntax is used for resources, not data sources; data sources require the `data.` prefix.

Bdata.vsphere_datacenter.dc

This references the entire data source object, not a specific attribute like `id`.

Cdata.dc,id

This syntax is incorrect; it misses the `data_source_type` and uses a comma instead of a dot.

Ddata.vsphere_datacenter.dc.idCorrect

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

#Terraform syntax#Data sources#Attribute referencing

Community Discussion

No community discussion yet for this question.

Full TA-002-P Practice