Do I need interpolation syntax when assigning a variable to a resource argument?

95 views Asked by At

In the Terraform docs, I sometimes stumble upon examples like this:

resource "vsphere_folder" "folder" {
  path          = "terraform-test-folder"
  type          = "vm"
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

where they use interpolation syntax ("${...}") to assign a variable to a resource argument and sometimes I stumble upon examples like this:

resource "vsphere_virtual_disk" "virtual_disk" {
  size               = 40
  type               = "thin"
  vmdk_path          = "/foo/foo.vmdk"
  create_directories = true
  datacenter         = data.vsphere_datacenter.datacenter.name
  datastore          = data.vsphere_datastore.datastore.name
}

where they do not use interpolation syntax.

In my testing, I was not able to craft content for a variable (with spaces and special chars) that needed the interpolation syntax. So, do I need it at all? Does it make my code more robust in any way?

1

There are 1 answers

2
Matthew Schuchard On BEST ANSWER

On May 19, 2019 Terraform 0.12 was released with first class expression syntax. This corresponds to the update from HCL to HCL2. Prior to that update string interpolation syntax was required. Any examples in documentation that still use the string interpolation syntax essentially imply that the documentation has not been updated in more than four years.

Note that usage of string interpolation syntax where it was not required began emitting warnings in version 0.14, and was auto-corrected to a first class expression with terraform fmt beginning in version 0.15.