Terraform v1.3.0-rc1 Optional attributes and null defaults

Given this

variable "with_optional_attribute" {
  type = object({
    a = string                # a required attribute
    b = optional(string)      # an optional attribute
    c = optional(number, 127) # an optional attribute with a default value
  })
  default = null
}

Should I be able to rely on the default value of null being set for the variable?
When I have this

output "test" {
  value = var.with_optional_attribute == null ? "default" : "set"
}

I get the following error:

β•·
β”‚ Error: Invalid value for input variable
β”‚ 
β”‚   on variables.tf line 11:
β”‚   11: variable "with_optional_attribute" {
β”‚ 
β”‚ The given value is not suitable for var.with_optional_attribute declared at variables.tf:11,1-35: attribute "a" is required.
β•΅

I can explicitly set the variable to null and it works so is optionality now completely defined by the object attribute definitions for object variables now?

Thanks for posting this! This looks like a bug to me, and I’ll look into it. Would you mind reporting this through the bug tracker so that we can keep track of it?

For anyone else following along, here’s the issue (thanks again!): 1.3.0 and variable defaults Β· Issue #31795 Β· hashicorp/terraform Β· GitHub

1 Like