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

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

    Using the for_each block is pretty new and there's not too much documentation. Some of the best info comes from their announcement blog post: https://www.hashicorp.com/blog/hashicorp-terraform-0-12-preview-for-and-for-each/

    Also make sure to check out the Dynamic Blocks section of their documentation: https://www.terraform.io/docs/configuration/expressions.html#dynamic-blocks

    From what your example looks like you need to have a set of values for each instance that is created so you'll have a map of maps:

    Below is an example I created using Terraform 0.12.12:

    variable "hostnames" {
        default = {
            "one" = {
                "name" = "one",
                "machine" = "n1-standard-1",
                "os" = "projects/coreos-cloud/global/images/coreos-stable-2247-5-0-v20191016",
                "zone" = "us-central1-a"
            },
            "two" = {
                "name" = "two",
                "machine" = "n1-standard-2",
                "os" = "projects/centos-cloud/global/images/centos-8-v20191018",
                "zone" = "us-central1-b"
            }
        }
    }
    
    resource "google_compute_instance" "default" {
        for_each = var.hostnames
        name         = each.value.name
        machine_type = each.value.machine
        zone         = each.value.zone
    
        boot_disk {
            initialize_params {
                image = each.value.os
            }
        }
    
        scratch_disk {
        }
    
        network_interface {
            network = "default"
        }
    }
    

    Terraform plan output:

    Terraform will perform the following actions:
    
      # google_compute_instance.default["one"] will be created
      + resource "google_compute_instance" "default" {
          + can_ip_forward       = false
          + cpu_platform         = (known after apply)
          + deletion_protection  = false
          + guest_accelerator    = (known after apply)
          + id                   = (known after apply)
          + instance_id          = (known after apply)
          + label_fingerprint    = (known after apply)
          + machine_type         = "n1-standard-1"
          + metadata_fingerprint = (known after apply)
          + name                 = "one"
          + project              = (known after apply)
          + self_link            = (known after apply)
          + tags_fingerprint     = (known after apply)
          + zone                 = "us-central1-a"
    
          + boot_disk {
              + auto_delete                = true
              + device_name                = (known after apply)
              + disk_encryption_key_sha256 = (known after apply)
              + kms_key_self_link          = (known after apply)
              + mode                       = "READ_WRITE"
              + source                     = (known after apply)
    
              + initialize_params {
                  + image  = "projects/coreos-cloud/global/images/coreos-stable-2247-5-0-v20191016"
                  + labels = (known after apply)
                  + size   = (known after apply)
                  + type   = (known after apply)
                }
            }
    
          + network_interface {
              + address            = (known after apply)
              + name               = (known after apply)
              + network            = "default"
              + network_ip         = (known after apply)
              + subnetwork         = (known after apply)
              + subnetwork_project = (known after apply)
            }
    
          + scheduling {
              + automatic_restart   = (known after apply)
              + on_host_maintenance = (known after apply)
              + preemptible         = (known after apply)
    
              + node_affinities {
                  + key      = (known after apply)
                  + operator = (known after apply)
                  + values   = (known after apply)
                }
            }
    
          + scratch_disk {
              + interface = "SCSI"
            }
        }
    
      # google_compute_instance.default["two"] will be created
      + resource "google_compute_instance" "default" {
          + can_ip_forward       = false
          + cpu_platform         = (known after apply)
          + deletion_protection  = false
          + guest_accelerator    = (known after apply)
          + id                   = (known after apply)
          + instance_id          = (known after apply)
          + label_fingerprint    = (known after apply)
          + machine_type         = "n1-standard-2"
          + metadata_fingerprint = (known after apply)
          + name                 = "two"
          + project              = (known after apply)
          + self_link            = (known after apply)
          + tags_fingerprint     = (known after apply)
          + zone                 = "us-central1-b"
    
          + boot_disk {
              + auto_delete                = true
              + device_name                = (known after apply)
              + disk_encryption_key_sha256 = (known after apply)
              + kms_key_self_link          = (known after apply)
              + mode                       = "READ_WRITE"
              + source                     = (known after apply)
    
              + initialize_params {
                  + image  = "projects/centos-cloud/global/images/centos-8-v20191018"
                  + labels = (known after apply)
                  + size   = (known after apply)
                  + type   = (known after apply)
                }
            }
    
          + network_interface {
              + address            = (known after apply)
              + name               = (known after apply)
              + network            = "default"
              + network_ip         = (known after apply)
              + subnetwork         = (known after apply)
              + subnetwork_project = (known after apply)
            }
    
          + scheduling {
              + automatic_restart   = (known after apply)
              + on_host_maintenance = (known after apply)
              + preemptible         = (known after apply)
    
              + node_affinities {
                  + key      = (known after apply)
                  + operator = (known after apply)
                  + values   = (known after apply)
                }
            }
    
          + scratch_disk {
              + interface = "SCSI"
            }
        }
    
    Plan: 2 to add, 0 to change, 0 to destroy.
    

提交回复
热议问题