# Attach multiple disk to multiple instances using google\_compute\_attached\_disk

**URL:** <https://discuss.hashicorp.com/t/attach-multiple-disk-to-multiple-instances-using-google-compute-attached-disk/3353>\
**Category:** Google\
**Created:** [October 15, 2019, 6:25pm UTC](https://discuss.hashicorp.com/t/attach-multiple-disk-to-multiple-instances-using-google-compute-attached-disk/3353 "2019-10-15T18:25:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![deangelo](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/deangelo/32/2677_2.png) [@deangelo](https://discuss.hashicorp.com/u/deangelo)\
**Post date:** [October 15, 2019, 6:25pm UTC](https://discuss.hashicorp.com/t/attach-multiple-disk-to-multiple-instances-using-google-compute-attached-disk/3353/1 "2019-10-15T18:25:16Z")

</div>

I have been trying to create a script that allows the attachment of multiple disks into multiple instances using google\_compute\_attached\_disk. What should I do in the resource “google\_compute\_attached\_disk”?

```
resource "google_compute_disk" "encrypted_disk" {
          count = "${var.resource_count}"
          name = "${var.disk_name}${count.index}"
          type = "${var.disk_type}"
          zone = "${var.instance_zone}"
          size = "${var.disk_size}"
          image = "${var.image}"
          labels = "${var.disk_labels}"
          disk_encryption_key {
            kms_key_self_link = "${var.encryption_key}"
          }
        }

resource "google_compute_attached_disk" "attached_encrypted_disk" {
  disk = "${google_compute_disk.encrypted_disk.self_link}"
  instance = "${google_compute_instance.compute_instance.self_link}"
}

resource "google_compute_instance" "compute_instance" {
  count = "${var.resource_count}"
  name = "${var.instance_name}${count.index}"
  machine_type = "${var.instance_type}"
  zone = "${var.instance_zone}"

  tags = "${var.target_tags}"

  boot_disk {
    kms_key_self_link = "${var.encryption_key}"
    auto_delete = "${var.disk_auto_delete}"
    initialize_params {
      size = "${var.image_size}"
      type = "${var.disk_type}"
      image = "${var.image}"
    }
  }

  network_interface {
    network = "${var.network}"
    network_ip = "${var.private_ip}"
  }

  lifecycle {
    ignore_changes = ["attached_disk"]
  }

  metadata = "${var.metadata}"

  labels = "${var.instance_labels}"

  scheduling {
    preemptible = "${var.preemptible}"
    automatic_restart = "${var.automatic_restart}"
  }

  service_account {
    scopes = "${var.scopes}"
  }
}
```

---

<div class="post-metadata">

**Author:** ![Raj200](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/raj200/32/8850_2.png) [@Raj200](https://discuss.hashicorp.com/u/Raj200)\
**Post date:** [August 17, 2020, 3:25pm UTC](https://discuss.hashicorp.com/t/attach-multiple-disk-to-multiple-instances-using-google-compute-attached-disk/3353/2 "2020-08-17T15:25:36Z")

</div>

Even I am also facing the same issue. reading few blogs I understand that using count as a loop is not recommendable. The reason is justified in this link ([https://github.com/hashicorp/terraform/issues/17179](https://github.com/hashicorp/terraform/issues/17179)), instead try to use for\_each.
