optional function for list(object) in terraform

I am using below experimental block module in terraform settings block to use optional function .

experiments = [module_variable_optional_attrs]

I want to make variable urlMaps which is list(object) as optional . When I dont give urlMaps as a input to terraform.tfvars , I am expecting it send a null vaule. But it is giving exception mentioned below. If its not possible, is there any alternative to achieve this.

terraform.tfvars

urlMaps = [
  {
    hosts   = ["example.com"]
    paths   = ["/image"]
    service = "backend-service"
  }
]

Variables.tf

variable "urlMaps" {
  description = "Netowork endpoint group region"
  type        = optional(list(object({
    hosts     = list(string)
    paths     = list(string)
    service   = string
  })))
}

Exception

│ Error: Invalid type specification
│ 
│   on lb/variables.tf line 30, in variable "urlMaps":
│   30:   type        = list(optional(object({
│ 
│ Keyword "optional" is valid only as a modifier for object type attributes.

>Solution :

There are two posibilities:

  1. Add a default value to your urlMaps.
  2. Make it a normal list(map), not list(object).

Leave a Reply