Loop within a loop for GCP instance creation with multiple volumes

Hi guru’s,

I creating some terraform which automates the creation of GCE instances, along with multiple volumes. I’m trying to make it flexible, so all I need to do is input the GCE instance(s) variables (in a map), along with it’s volume resource requirements if the instance has multiple volumes. That way all I need to do is update the tfvars files with the new info for the application to create all of the GCE instances, and their associated volumes and other resources.

I have the CGE instances create loop working fine (using a map(object)), but now i’m working on creating the volumes within that process and I’m lost.

How do I get the Volume loop working within the GCE instance creation loop?

Thanks in advance. Here is my code so far:

Variables.tf

variable “vm_variables” {

  type = map(object({
    vm_name       = string
    vm_type       = string
    vm_zone       = string
    vm_labels     = map(any)
    vm_boot_image     = string
    vm_boot_disk_type = string
    vm_boot_disk_size = string
    vm_network        = string
    vm_subnetwork     = string
    vm_disks          = map(any)

}))
}

terraform.tfvars

vm_variables = {

       "vm1" = {
          vm_name   = "xxxxtest1"
          vm_type   = "e2-micro"
          vm_zone   = "us-east4-b"
          vm_boot_image  = "ubuntu-os-cloud/ubuntu-2004-focal-v20220712"
          vm_boot_disk_size = "40"
          vm_boot_disk_type = "pd-standard"
          vm_network = "vpc-shared-vsnew-prod"
          vm_subnetwork = "subnet-hhc-cs-poc-us-east4"
          vm_labels = {
            owner = "zzzzzzzz"
            application = "testapp1"
          }
          vm_disks = {
            disk_1 = {
              vm_diskname  = "test-disk-1"
              vm_disktype  = "pd-ssd"
              vm_disksize  = "30"
            }
            disk_2 = {
              vm_diskname  = "test-disk-2"
              vm_disktype  = "pd-ssd"
              vm_disksize  = "30"
            }
          }

       }
       "vm2" = {
          vm_name   = "aaatest2"
          vm_type   = "e2-micro"
          vm_zone   = "us-east4-b"
          vm_boot_image  = "ubuntu-os-cloud/ubuntu-2004-focal-v20220712"
          vm_boot_disk_size = "40"
          vm_boot_disk_type = "pd-standard"
          vm_network = "vpc-shared-vsnew-prod"
          vm_subnetwork = "subnet-hhc-cs-poc-us-east4"
          vm_labels = {
            owner = "zzzzzzzz"
            application = "testapp2"
          }
          vm_disks = {
            disk_1 = {
              vm_diskname  = "test-disk-3"
              vm_disktype  = "pd-Standard"
              vm_disksize  = "30"
            }
            disk_2 = {
              vm_diskname  = "test-disk-4"
              vm_disktype  = "pd-ssd"
              vm_disksize  = "30"
            }
          }
       }

}

main.tf

provider “google” {

  project = "prj-shared-migration"
  region  = "us-east4"
  zone    = "us-east4-b"
  credentials = "/Users/xxxxxxx/.config/gcloud/application_default_credentials.json"
}

resource "google_compute_instance" "default" {
  provider = google

  for_each     = var.vm_variables
  name         = each.value["vm_name"]
  machine_type = each.value["vm_type"]
  labels       = each.value["vm_labels"]

  network_interface {
    network = data.google_compute_network.my-network[each.key].self_link
    subnetwork = data.google_compute_subnetwork.my-subnetwork[each.key].self_link
  }

  boot_disk {
    initialize_params {
      image = "ubuntu-os-cloud/ubuntu-2004-focal-v20220712"
      labels = each.value["vm_labels"]
      type = each.value["vm_boot_disk_type"]
      size = each.value["vm_boot_disk_size"]
    }
  }

  dynamic "attached_disk" {
    for_each = each.value["vm_disks"]
      content{
        source = values(google_compute_disk.default)[*].self_link
#        source = google_compute_disk.default[attached_disk.value].self_link
      }
    }
}

resource "google_compute_disk" "default" {
    for_each = var.vm_variables
      name  = each.value["vm_disks"].vm_diskname
      type  = each.value["vm_disks"].vm_disktype
      size  = each.value["vm_disks"].vm_disksize
      labels = {
        environment = "dev"
      }
      physical_block_size_bytes = 4096
}

data "google_compute_network" "my-network" {
  for_each = var.vm_variables
  name = each.value["vm_network"]
  project = "prj-shared-network-prod-hhc"
}

data "google_compute_subnetwork" "my-subnetwork" {
  for_each = var.vm_variables

  name = each.value["vm_subnetwork"]
  project = "prj-shared-network-prod-hhc"

}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.