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

后端 未结 4 472
别那么骄傲
别那么骄傲 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:03

    Seem's like I found what to do. If you pass not the maps of maps but the list of maps you can use such code

    resource "google_compute_instance" "node" {
        for_each = {for vm in var.vms:  vm.hostname => vm}
    
        name         = "${each.value.hostname}"
        machine_type = "custom-${each.value.cpu}-${each.value.ram*1024}"
        zone         = "${var.gcp_zone}"
    
        boot_disk {
            initialize_params {
            image = "${var.image_name}"
            size = "${each.value.hdd}"
            }
        }
    
        network_interface {
            network = "${var.network}"
        }
    
        metadata = {
            env_id = "${var.env_id}"
            service_types = "${join(",",each.value.service_types)}"
      }
    }
    

    It will create actual number of instance and when you remove for example middle one of three(if you create three:)), terraform will remove what we asked.

提交回复
热议问题