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.
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 :
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 :
Hurrah !