Home > other >  How to define map(list(string) and map(string) as a variable type in single variable block?
How to define map(list(string) and map(string) as a variable type in single variable block?

Time:01-30

For azure cdn i'm excpecting to add rule engine/delivery rules ,so here in one of the block called request_scheme_condition contains two arguments are match_value & operator here match_value is expecting list of strings and operator is expecting string only.

variable "delivery_rules" {
   default = {
    name  = "test"
    order = 1
    request_scheme_condition = {
      match_value = ["HTTP"]

      operator = "Equal"
    }
  }

  type = object(
    {
      name                     = string
      order                    = number
      request_scheme_condition = map(list(string))

    }
  )

}
error: 
│ Error: Invalid default value for variable
│
│   on variables.tf line 92, in variable "delivery_rules":
│   92:   default = {
│   93:     name  = "test"
│   94:     order = 1
│   95:     request_scheme_condition = {
│   96:       match_value = "HTTP"
│   97:
│   98:       operator     = "Equal"
│   99:     }
│  100:   }
│
│ This default value is not compatible with the variable's type constraint: attribute "request_scheme_condition": element "

CodePudding user response:

Instead of map(list(string)) you need object again:

variable "delivery_rules" {
   default = {
    name  = "test"
    order = 1
    request_scheme_condition = {
      match_value = ["HTTP"]
      operator = "Equal"
    }
  }

  type = object(
    {
      name                     = string
      order                    = number
      request_scheme_condition =  object({
          match_value = list(string)
          operator = string
        })
   })
}

CodePudding user response:

I also got the same error when I run your terraform code in my environment as shown:

enter image description here

Cause is: The map type in Terraform is collection and all elements of a collection must always be of the same type.

As your variable has several types for distinct fields, map(list(string)) will not work.

I made a few changes to your script and was able to properly initialize the terraform with terraform init.

variable "delivery_rules" {
   default = {
    name  = "test"
    order = 1
    request_scheme_condition = {
      match_value = "[HTTP]"
      operator = "Equal"
    }
  }
  type = object(
    {
      name                     = string
      order                    = number
      request_scheme_condition = object({
      match_value = string
      operator = string
    })
}
)
}

Output:

enter image description here

Check to know the Type constraints in Terraform.

  • Related