2
votes

environment:

  • terraform v0.10.7
  • google cloud platform
  • various .tf files for creating backend, variables etc

issue:

i am able to create multiple vm instances and also multiple additional disks (boot_disk is working fine on each instance) but I want to be able to attach those additional disks to each vm accordingly without having to have individual adds for each vm (if that makes sense!).

the code I have so far is (which works ok for building multiple compute instances and also multiple additional disks): note (I have commented out the attached_disk which errors atm)

# vm1.tf

variable "node_count" {
  default = "3"
 }

resource "google_compute_disk" "test-node-1-index-disk-" {
    count   = "${var.node_count}"
    name    = "test-node-1-index-disk-${count.index}-data"
    type    = "pd-standard"
    zone    = "${var.zone}"
    size    = "5"
}
resource "google_compute_instance" "test-node-" {
    count = "${var.node_count}"
    name = "test-node-${count.index}"
    machine_type = "${var.machine_type}"
    zone = "${var.zone}"

    boot_disk {
    initialize_params {
    image = "${var.image}"
    }
   }
#    attached_disk {
#        source      = "${google_compute_disk.test-node-1-index-disk-0}"
#        device_name = "${google_compute_disk.test-node-1-index-disk-0}"
#   }


    network_interface {
      network = "default"
      access_config {
        // Ephemeral IP
      }

    }
}

If i do individual .tf the attached_disk works no problem.

My desired end state, is to be able to build multiple vm's, multiple additional disks using count and attach/assign each added disk to each vm instance with a relationship of 1:1 but preferable within a single .tf and block...

I guess, I could look to apply a post gcloud compute command to attach (knowing the expected naming convention) but i'd like it to be more dynamic and done at point of creation.

Am I approaching this wrong? Any help/pointers greatly appreciated!

Thx Bry

2

2 Answers

3
votes
# vm1.tf

variable "node_count" {
  default = "3"
 }

resource "google_compute_disk" "test-node-1-index-disk-" {
    count   = "${var.node_count}"
    name    = "test-node-1-index-disk-${count.index}-data"
    type    = "pd-standard"
    zone    = "${var.zone}"
    size    = "5"
}
resource "google_compute_instance" "test-node-" {
    count = "${var.node_count}"
    name = "test-node-${count.index}"
    machine_type = "${var.machine_type}"
    zone = "${var.zone}"

    boot_disk {
    initialize_params {
    image = "${var.image}"
    }
   }
    attached_disk {
        source      = "${element(google_compute_disk.test-node-1-index-disk-.*.self_link, count.index)}"
        device_name = "${element(google_compute_disk.test-node-1-index-disk-.*.name, count.index)}"
   }


    network_interface {
      network = "default"
      access_config {
        // Ephemeral IP
      }

    }
}
2
votes

if you want static ip

variable "node_count" {
 default = "3"
}

resource "google_compute_address" "static-ip-address" {
  count = "${var.node_count}"
  name = "${var.tag}-static-ip-${count.index + 1}"
}

resource "google_compute_disk" "test-node-1-index-disk-" {
  count   = "${var.node_count}"
  name    = "test-node-1-index-disk-${count.index}-data"
  type    = "pd-standard"
  zone    = "${var.zone}"
  size    = "5"
}
resource "google_compute_instance" "test-node-" {
  count = "${var.node_count}"
  name = "test-node-${count.index}"
  machine_type = "${var.machine_type}"
  zone = "${var.zone}"

boot_disk {
  initialize_params {
  image = "${var.image}"
  }
}
attached_disk {
    source      = "${element(google_compute_disk.test-node-1-index-disk-.*.self_link, count.index)}"
    device_name = "${element(google_compute_disk.test-node-1-index-disk-.*.name, count.index)}"
}


network_interface {
  network = "default"
  access_config {
    nat_ip = "${element(google_compute_address.static-ip-address.*.address, count.index)}"
  }

}