Search code examples
terraform

How to use a list in a map(any) in Terraform format


I wrote a terraform code using variables in the form of may(any) on var.tf as below.

...
variable "usernodepool_vm" {
  description = "VM of AKS Cluster"
  type        = map(any)
  default = {
    vm1 = {
      user_agents_name         = "upool01"
      user_agents_size         = "Standard_D2s_v5"
      user_agents_count        = "1"
      user_agents_os_disk_size = "32"
      max_pods                 = "20"
      orchestrator_version     = "1.27.7"
      node_taints              = null
      node_labels = {
        "commponent" = "idx"
        "keyexample"     = "valueexample"
      }
    }
    vm2 = {
      user_agents_name         = "upool02"
      user_agents_size         = "Standard_D2s_v5"2
      user_agents_count        = "1"
      user_agents_os_disk_size = "32"
      max_pods                 = "20"
      orchestrator_version     = "1.27.7"
      node_taints              = [    #<======== this configuration is list string.
        "key01=value01:NoSchedule"
      ]
      node_labels = {
        "compponent" = "oom"
        "no" = "numeric"
      }
    }
  }
}
...

I'm trying to set the "node_tints" part and this setting is list(string). Running this code causes the following error. Is there a way to add a list to the map(any)?

│ Error: Invalid default value for variable
│
│   on var.tf line 279, in variable "usernodepool_vm":
│  279:   default = {
│  280:     vm1 = {
│  281:       user_agents_name         = "upool01"
│  282:       user_agents_size         = "Standard_D2s_v5"
│  283:       user_agents_count        = "1"
│  284:       user_agents_os_disk_size = "32"
│  285:       max_pods                 = "20"
│  286:       orchestrator_version     = "1.27.7"
│  287:       node_taints              = null
│  288:       node_labels = {
│  289:         "commponent" = "idx"
│  290:       }
│  291:     }
│  292:     vm2 = {
│  293:       user_agents_name         = "upool02"
│  294:       user_agents_size         = "Standard_D2s_v5"
│  295:       user_agents_count        = "1"
│  296:       user_agents_os_disk_size = "32"
│  297:       max_pods                 = "20"
│  298:       orchestrator_version     = "1.27.7"
│  299:       node_taints              = ["key01=value01:NoSchedule"] <===== Error
│  300:       node_labels = {
│  301:         "compponent" = "oom"
│  302:         "no" = "numeric"
│  303:       }
│  304:     }
│  305:   }

I know that if I change to map(any) -> any, this code will succeed. But I want to know how to set node_tints without changing map(any) to any. Please help me.


Solution

  • The issue you're encountering with the "Invalid default value for variable" error in your Terraform code is likely due to the inconsistent use of types for the node_taints field across your map entries. In Terraform, even though map(any) theoretically allows any type for the value, each specific key should consistently have the same type in all instances within the map. In your original configuration, node_taints is set to null for vm1 and a list of strings for vm2, which I feel is what’s causing the error.

    To resolve this issue while maintaining your structure (map(any)), I’ll suggest you ensure that the data type for node_taints remains consistent across all instances. This means setting an empty list for node_taints where no taints are to be applied, rather than using null. Here’s what I mean:

    variable "usernodepool_vm" {
      description = "VM of AKS Cluster"
      type        = map(any)
      default = {
        vm1 = {
          user_agents_name         = "upool01"
          user_agents_size         = "Standard_D2s_v5"
          user_agents_count        = "1"
          user_agents_os_disk_size = "32"
          max_pods                 = "20"
          orchestrator_version     = "1.27.7"
          node_taints              = []  # Use an empty list instead of null
          node_labels = {
            "component" = "idx"        # Correcting a typo in 'component'
            "keyexample" = "valueexample"
          }
        }
        vm2 = {
          user_agents_name         = "upool02"
          user_agents_size         = "Standard_D2s_v5"
          user_agents_count        = "1"
          user_agents_os_disk_size = "32"
          max_pods                 = "20"
          orchestrator_version     = "1.27.7"
          node_taints              = ["key01=value01:NoSchedule"]
          node_labels = {
            "component" = "oom"       # Correcting a typo in 'component'
            "no" = "numeric"
          }
        }
      }
    }
    

    By making node_taints an empty list where there are no taints, and ensuring accurate label spellings, I believe your variable should work without throwing errors, while keeping the configuration clear and error-free.

    Hope it helps.