hashicorp / terraform-provider-google

Terraform Provider for Google Cloud Platform
https://registry.terraform.io/providers/hashicorp/google/latest/docs
Mozilla Public License 2.0
2.29k stars 1.72k forks source link

GCE instance left without boot disk #18413

Open jrhellriegeljr opened 3 months ago

jrhellriegeljr commented 3 months ago

Community Note

Terraform Version & Provider Version(s)

Terraform v1.8.5 on linux_amd64

Affected Resource(s)

google_compute_disk google_compute_instance

Terraform Configuration

variable "project_id" {
  type          = string
  default       = <project_id>
}
variable "gce_instance_subnet" {
  type          = string
  default       = <subnet link>
}
variable "zone" {
  type          = string
  default       = "us-central1-a"
}
resource "google_compute_disk" "vm_disk_boot" {
  project       = var.project_id
  name          = "gceinstance01-disk-boot"
  type          = "pd-standard"
  size          = 10
  image         = "cos-cloud/cos-dev"
  zone          = var.zone
}
resource "google_compute_instance" "sql_proxy_gce_instance" {
  project      = var.project_id
  name         = "gceinstance01"
  machine_type = "e2-micro"
  zone         = var.zone
  boot_disk     { 
    source     = google_compute_disk.vm_disk_boot.name 
  }
  network_interface {
    subnetwork = var.gce_instance_subnet
  }
}

Debug Output

No response

Expected Behavior

One of:

Actual Behavior

A google_compute_instance with a boot disk linked to a google_compute_disk resource that becomes destroyed and recreated as part of a terraform apply results in the instance no longer having any boot disk association (yet terraform still thinks there is a disk attached).

Steps to reproduce

  1. terraform apply
  2. Power down created GCE instance
  3. Change google_compute_disk.vm_disk_boot.image to something else (cos-stable)
  4. terraform apply

Important Factoids

No response

References

In fairness it seems that the provider has other code paths that require a delete/create of the google_compute_instance so this alternate path may be less desirable - still - something is not right:

The still-attached-disk probably shouldn't be deleted (or add a flag to force detachement) The GCE instance shouldn't be left without a boot disk (unless maybe the added flag was used) Ideally the GCE instance would reflect the new disk without having to be destroy/created again (which was my ultimate goal when I set out to write this code).

ggtisc commented 3 months ago

Hi @jrhellriegeljr!

Today there are many ways to handle this. You can implement for example an auto_delete for the boot_disk then when you need to apply changes you can create a new one and attach it to the google_compute_instance. You can read the Google Cloud documentation to understand how the different available options works. Or maybe you are looking for something like a google_compute_image

In terraform registry and Google Cloud Documentation you can find many features that could fit your needs.