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

variable validation fails on optional variables #30403

Closed
philip-harvey opened this issue Jan 24, 2022 · 2 comments
Closed

variable validation fails on optional variables #30403

philip-harvey opened this issue Jan 24, 2022 · 2 comments
Labels
bug new new issue not yet triaged

Comments

@philip-harvey
Copy link

When trying to perform variable validation on a variable with optional values Terraform returns an error, "This object does not have an attribute named XXXX" if any of the elements in the map are not set (set to null by being optional).
Tested with Terraform 1.1.2 and 1.1.4

Code to reproduce the issue:
main.tf:

terraform {
  experiments = [module_variable_optional_attrs]
}

variable "var1" {
  type = map(object({
    config_str = optional(string)
  }))
  validation {
    condition     = alltrue([for k in var.var1 : (k.config_str != "bad-config")])
    error_message = "Config_str must not be set to 'bad-config'."
  }
}

terraform.tfvars:

var1 = {
  config1 = {}
  config2 = {config_str = "good-config"}
}

Expected Behavior

Validation should pass or fail depending on the validation rule.

Actual Behavior

Terraform returns an error. e.g.

Error: Unsupported attribute
   on main.tf line 10, in variable "var1":
   10:     condition     = (var.var1.config_str != "bad-config")
     var.var1 is object with 1 attribute "config1"
 This object does not have an attribute named "config_str".

Steps to Reproduce

  1. terraform init
  2. terraform plan

Additional Context

As a work around it is possible to use try or lookup, but it gets a little ugly. e.g:

variable "var1" {
  type = map(object({
    config_str = optional(string)
  }))
  validation {
    condition     = alltrue([for k in var.var1 : (lookup(k,"config_str",null) != "bad-config")])
    error_message = "Config_str must not be set to 'bad-config'."
  }
}
@philip-harvey philip-harvey added bug new new issue not yet triaged labels Jan 24, 2022
@jbardin
Copy link
Member

jbardin commented Jan 25, 2022

Hi @philip-harvey,

Thanks for filing the issue. I confirmed this is fixed by #29959.

@jbardin jbardin closed this as completed Jan 25, 2022
@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug new new issue not yet triaged
Projects
None yet
Development

No branches or pull requests

2 participants