How to for_each through a list(objects) in Terraform 0.12

后端 未结 4 461
别那么骄傲
别那么骄傲 2021-02-03 23:24

I need to deploy a list of GCP compute instances. How do I loop for_each through the "vms" in a list of objects like this:

    "gcp_zone": &q         


        
4条回答
  •  伪装坚强ぢ
    2021-02-04 00:18

    You can do the following:

    for_each = toset(keys({for i, r in var.vms:  i => r}))
    cpu = var.vms[each.value]["cpu"]
    

    Assuming you had the following:

    variable "vms" {
        type = list(object({
            hostname        = string
            cpu             = number
            ram             = number
            hdd             = number
            log_drive       = number
            template        = string 
            service_types   = list(string)
        }))
        default = [
            {
                cpu: 1
                ...
            }
        ]
    }
    

提交回复
热议问题