Search code examples
google-cloud-platformterraformclouddevopsinfrastructure-as-code

How to update disk in GCP using terraform?


Is it possible to create a terraform module that updates a specific resource which is created by another module?

Currently, I have two modules...

  1. linux-system: which creates a linux vm with boot disks
  2. disk-updater: which I'm planning to use to update the disks I created from the first module

The reason behind is I want to create a pipeline that will do disk operations tasks via terraform like disk resizing.

data "google_compute_disk" "boot_disk" {
  name    = "linux-boot-disk"
  zone    = "europe-west2-b"
}

resource "google_compute_disk" "boot_disk" {
  name    = data.google_compute_disk.boot_disk.name
  zone    = data.google_compute_disk.boot_disk.zone
  size    = 25
}

I tried to use data block to retrieve the existing disk details and pass it to resource block hoping to update the same disk but it seems like it will just try to create a new disk with the same name thats why im getting this error.

Error creating Disk: googleapi: Error 409: The resource ... already exists, alreadyExists

I think I'm doing it wrong, can someone give me advice how to proceed without using the first module I built. btw I'm a newbie when it comes to terraform


Solution

  • updates a specific resource which is created by another module?

    No. You have to update the resource using its original definition.

    The only way to update it from other module, is to import to the other module, which is bad design, as now you will have to definitions for the same resource, resulting in out-sync state files.