3

I am trying to add a second disk to a Google Compute instance using Terraform. This seems to be correct:

resource "google_compute_disk" "seconddisk" {
    name  = "seconddisk"
    type  = "pd-standard"
    zone  = "us-west1-a"
    size = "100"
}

resource "google_compute_instance" "someinstance" {

    name         = "someinstance"
    machine_type = "n1-standard-4"
    zone         = "us-west1-a" 

    disk {
        image = "${var.image_url}"
    }

    disk {
        disk = "${google_compute_disk.seconddisk.name}"
    }

    ...
}

However, I get the following error:

google_compute_instance.kafka1: Error creating instance: googleapi: Error 409: The resource '...' already exists, alreadyExists.

Thoughts?

Tim Dalsing
  • 159
  • 2
  • 7
  • That should work fine for attaching mulitple disks to an instance: https://gist.github.com/ryane/392c1565d83c63df851d1be36b2f4a8a It looks like you are running into a different problem. Perhaps you have an unexpected instance leftover in your account that is causing a conflict? – Ryan E Mar 02 '17 at 15:09
  • It may just a timing issue. I added a depends on flag and it seems to work, but it still fails occasionally. I was thinking there was something I was missing or leaving out. – Tim Dalsing Mar 03 '17 at 16:11
  • is there a way to mount a disk in R/W across multiple VMs? – mike.bukosky Jun 14 '17 at 13:29
  • @mike.bukosky https://stackoverflow.com/questions/26910960/share-a-persistent-disk-between-google-compute-engine-vms – Laszlo Valko Mar 07 '19 at 10:57

1 Answers1

3

As of 2022 May, you can do following

# disk
resource "google_compute_disk" "default" {
  name = "compute-disk"
}

# compute
resource "google_compute_instance" "default" {
  name         = "attached-disk-instance"
  machine_type = "e2-medium"
  zone         = "us-west1-a"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }
  network_interface {
    network = "default"
  }
  lifecycle {
    ignore_changes = [attached_disk]
  }
}

# connect compute & disk
resource "google_compute_attached_disk" "default" {
  disk     = google_compute_disk.default.id
  instance = google_compute_instance.default.id
}

Helpful resources

sam
  • 1,819
  • 1
  • 18
  • 30