I already have a vm instance running in GCP.Is there a way we can add custom metadata to it using terraform. Terraform has a resource to add custom metadata to a project but not a instance.
I tried to update it by using the google_compute_instance
resource by setting the lifecycle.prevent_destroy = true
but that does not seem the correct way.
data "google_compute_instance" "instance" {
name = var.instance_name
project = var.project_id
zone = var.zone
}
resource "google_compute_instance" "instance" {
name = data.google_compute_instance.instance.name
machine_type = data.google_compute_instance.instance.machine_type
project = var.project_id
zone = var.zone
# ... rest of the config
metadata = {
foo = "bar"
}
lifecycle {
prevent_destroy = true
}
}
But I get an error saying instance already exists
which suggests that it is trying to create a new one but not update.
Instead of the data block you need an import block (https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance#import), otherwise Terraform would try to create it.