1

I have a GCP instance created using Terraform. When I increase the size of its root disk, Terraform tries to destroy and recreate a new instance which is unacceptable. Here is my terraform code:

resource "google_compute_instance" "test" {
  ...
  boot_disk {
  auto_delete = true
    initialize_params {
      image = var.image
      size  = 10 # I want to change it to 20
      type  = "pd-standard"
    }
  }
  ...
}

The var.image is: https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-2004-focal-v20201028

How do I resize root disk of the gcp instance without recreating it? (I guess I can avoid recreating the instance by resizing it manually? see How can size of the root disk in Google Compute Engine be increased?. But I do not want to resize manually because there are a lot of instances I need to resize. and Besides, if I manually change the size, terraform will show drift).

note for AWS EC2, terraform will not recreate a new EC2 when we change the size.

user389955
  • 9,605
  • 14
  • 56
  • 98

1 Answers1

2

From https://github.com/hashicorp/terraform-provider-google/issues/12124

I understand that "initialize_params indicates "I want the instance to have been created with these properties" meaning that recreating it when changed is the expected behaviour of the provider"

From https://github.com/hashicorp/terraform-provider-google/issues/6087#issuecomment-619270971

I understand that "you can create a disk in Terraform that can be updated whenever you want, and we can let initialize_params be something that truly means exactly what it says: parameters that are set when the disk is initialized"

Here is how to make a disk that you can resize in terraform :

data "google_compute_image" "my_image" {
  family  = "debian-9"
  project = "debian-cloud"
}

resource "google_compute_disk" "foobar" {
  name  = "my-disk"
  zone  = "us-central1-a"
  // only use an image data source if you're ok with the disk recreating itself with a new image periodically
  image = data.google_compute_image.my_image.self_link
}

resource "google_compute_instance" "foobar" {
  name         = "my-instance"
  machine_type = "n1-standard-1"
  zone         = "us-central1-a"

  boot_disk {
    source = google_compute_disk.foobar.name
  }

  network_interface {
    network = "default"
  }
}

This way, you will be able to dynamically change the disk specs with terraform, without having to use initialize_params which is meant for recreating and not for modifying.

From https://github.com/hashicorp/terraform-provider-google/issues/12655 and previous links in this answer, it looks like terraform will not allow you to change the way initialize_params work : these are to tell that you want an instance created, not modified.

"initialize_params is meant to be a create-only field. Updating anything within this field is defined as recreate/destroy behavior. If you want to do in-place updates, I believe the source field as pointed out in the comment above, will be what you need."

If you are okay to update the disk size without Terraform, it is possible this way.

If you already have an instance of wrong size created using initialize_params and want to modify the disk size without creating drift in Terraform, as of 2/2023 you can :

  1. change the disk to new size X without Terraform, following the previous link.
  2. change your Terraform config with new size instead of old size

Hurrah !

NanoPish
  • 1,379
  • 1
  • 19
  • 35
  • Thanks NanoPish. that is ridiculas. and it seems there is no fix :-(, I have many instance created using initialize_params. so sad. Do you happen to have any work around solution? – user389955 Jan 27 '23 at 13:48
  • @user389955 Yes in my answer, the way to make a resizable disk attached to your instance, it is a workaround ! Read https://github.com/hashicorp/terraform-provider-google/issues/6087#issuecomment-619270971 for more info, I got the info here – NanoPish Jan 28 '23 at 11:20
  • @user389955 The main point I wanted to convey in my answer is you can't use that kind of parameters to resize an instance disk directly, but you can specify disks attached to an instance and resize those disks dynamically – NanoPish Jan 28 '23 at 11:33
  • @user389955 You can accept my answer if it responds to your question ! – NanoPish Jan 30 '23 at 14:21
  • @ NanoPish: but I already created the root disk with size = 10 using initialize_params (when I created 1 year ago I did not know initialize_params cannot resize), how can I increase the size of the "existing" disk? I know I cannot use Terraform, but is there any way I can use to resize it? there are a lot of instances created with initialize_params unfortunately. Thanks – user389955 Jan 30 '23 at 15:27
  • @user389955 I updated the end of my answer on how to do that directly without terraform – NanoPish Jan 30 '23 at 15:43
  • @ NanoPish: I will try that way, I know how to manually resize an instance. but I am afraid after I manually resize the instances, the terraform will show drift (because actual size and terraform size are different) which means my terraform folder cannot be used anymore.... But any way, since there is no other solution which is better than yours, I will accept your answer. Thanks – user389955 Jan 30 '23 at 15:52
  • @user389955 Maybe you can make a test instance with the old size, and change the disk to new size X without Terraform, and then changing your Terraform config with X instead of old size, and test if there is drift in terraform apply. If it works, hurrah ! If not, you can make a new terraform with distinct dynamic disk and migrate your old instances ? – NanoPish Jan 30 '23 at 16:07
  • @ NanoPish: I have verified that if I manually increase size from 10 to 20 in gcloud CLI/Console, then ssh to resize its partiion/filesystem, then increase size to 20 in terraform, the terraform folder is still in sync. so this workaround works!, you can mention it in your answer. Thank you! – user389955 Feb 02 '23 at 14:52
  • @user389955 Great, happy to hear, I am adding it – NanoPish Feb 02 '23 at 14:53