Terraform: v0.12 'Error: Ambiguous attribute key' thrown for simple, non-literal key

Created on 3 Jun 2019  ยท  7Comments  ยท  Source: hashicorp/terraform

Terraform Version

Terraform v0.12.0

Terraform Configuration Files

terraform {
  required_version = ">= 0.12"
}

variable "name_test" {
  default = "hello"
}

locals {
  hash = {
    (var.name_test) = "world",
  }
}

Debug Output

https://gist.github.com/mavin/4e796c4d43b233ce75f2a8cfc9557141

Expected Behavior

The given config should be valid, according to the docs; if it isn't then there should be a more helpful error message.

Actual Behavior

An error is thrown that recommends changes that are already present in the config.

$ terraform validate

Error: Ambiguous attribute key

  on vars.tf line 11, in locals:
  11:     (var.name_test) = "world",

If this expression is intended to be a reference, wrap it in parentheses. If
it's instead intended as a literal name containing periods, wrap it in quotes
to create a string literal.

Steps to Reproduce

  1. terraform init
  2. terraform validate

Additional Context

All this happens with a fresh environment and a new config.

bug config v0.12

Most helpful comment

Is this still a bug or we need to refactor our code to comply with the new syntax? I'm using v0.11.13.

All 7 comments

Thanks for reporting this, @mavin!

This is indeed a bug, probably in the underlying HCL library. :confounded: We'll investigate it.

In the mean time, you may be able to get this to work by using the interpolation syntax as an alternative way to disambiguate it:

    "${var.name_test}" = "world",

:taco:

Is this still a bug or we need to refactor our code to comply with the new syntax? I'm using v0.11.13.

I am trying to upgrade our (huge) repo from V0.11.14 to 0.12.14. It does not look viable to manually change to interpolation syntax everywhere. Is there a better way to get it working until we fix this bug?

@apparentlymart Given that 0.13 is coming soon, does it make sense to assume that this bug won't be fixed in 0.12?

This bug was actually fixed in 0.12.10, as part of this PR updating the HCL version. It continues to be fixed in 0.13.

$ cat main.tf
terraform {
  required_version = ">= 0.12"
}

variable "name_test" {
  default = "hello"
}

locals {
  hash = {
    (var.name_test) = "world",
  }
}
$ terraform-0.12.9 validate

Error: Ambiguous attribute key

  on main.tf line 11, in locals:
  11:     (var.name_test) = "world",

If this expression is intended to be a reference, wrap it in parentheses. If
it's instead intended as a literal name containing periods, wrap it in quotes
to create a string literal.

$ terraform-0.12.10 validate
Success! The configuration is valid.

$ terraform-0.12.28 validate
Success! The configuration is valid.

$ terraform-0.13.0-beta2 validate
Success! The configuration is valid.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rjinski picture rjinski  ยท  3Comments

ronnix picture ronnix  ยท  3Comments

rkulagowski picture rkulagowski  ยท  3Comments

franklinwise picture franklinwise  ยท  3Comments

darron picture darron  ยท  3Comments