Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid argument name when upgrading to 0.12 #22901

Closed
Edeholland opened this issue Sep 25, 2019 · 3 comments
Closed

Invalid argument name when upgrading to 0.12 #22901

Edeholland opened this issue Sep 25, 2019 · 3 comments

Comments

@Edeholland
Copy link

Terraform Version

Terraform v0.12.8

Terraform Configuration Files

variable "valid_tiers" {
  default = ["group", "app"]
}

variable "tier" {
  default = "app"
}

resource "null_resource" "assert_tier" {
  count                            = "${contains(var.valid_tiers, var.tier) == true ? 0 : 1}"
  "ERROR: Invalid value for tier." = true
}

Expected Behavior

Configuration should be valid.

Actual Behavior

Error: Invalid argument name

  on main.tf line 11, in resource "null_resource" "assert_tier":
  11:   "ERROR: Invalid value for tier." = true

Argument names must not be quoted.

Steps to Reproduce

  1. Copy Terraform code above
  2. Run terraform init
  3. Run terraform apply

Additional Context

I tried running terraform 0.12upgrade. This resulted in the following error:

Error: Unrecognized attribute name

  on main.tf line 11, in resource "null_resource" "assert_tier":
  11:   "ERROR: Invalid value for tier." = true

No attribute named "ERROR: Invalid value for tier." is expected in
null_resource.assert_tier.

Is there any way I can keep the variable name I have now when upgrading from 0.11 to 0.12?

@teamterraform
Copy link
Contributor

Hi @Edeholland!

This configuration seems to be relying on some undocumented behavior that changed in Terraform 0.12. The configuration was invalid in Terraform 0.11 too, but some different implementation details caused Terraform 0.11 to not detect it. (Internally, this was because Terraform 0.11 relied entirely on provider logic for validation, while Terraform 0.12 includes its own validation step that runs during static validation regardless of the provider's validation behavior.)

The only way to make this work in Terraform 0.12 is to remove the invalid argument name. It looks like what you are looking for here is the feature being discussed in #2847, in which case we'd suggest that you vote 👍 on that issue and watch it for future updates.

In the mean time, you could get a similar result (failing if the tier is invalid) in this case by accessing the tiers through an "identity map" that just maps the valid keys to themselves:

locals {
  tiers = {
    group = "group"
    app   = "app"
  }
  selected_tier = local.tiers[var.tier] # If this fails, the tier variable is invalid
}

If you access the tier as local.selected_tier rather than var.tier directly then the map lookup will fail for invalid tiers. The source code snippet in the error message will include the comment at the end of the line and thus provide a similar indirect clue to the caller as to what the real cause of this error is, until a feature similar to what's described in #2847 can make such an error message first-class in the language.

Since #2847 is already covering the use-case, we're going to close this out to consolidate the discussion. Thanks!

@Edeholland
Copy link
Author

Thanks for the explanation, I got my code working now!

@ghost
Copy link

ghost commented Oct 26, 2019

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.

@ghost ghost locked and limited conversation to collaborators Oct 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants