Search code examples
google-cloud-platformterraformdiskterraform-provider-gcp

How to add a snapshot schedule to GCP boot_disk via terraform


I'm trying to add snapshot schedule to the boot disk of the vm_instance.

provider "google" {
  project = "xxxxxx"
}
resource "google_compute_instance" "xxxxxx" {
  name         = "xxxxxx"
  machine_type = "xxxxxx"
  zone         = "xxxxxx"

  boot_disk {
    initialize_params {
      image = "???"
    }
  }
  metadata_startup_script = ";;"
  network_interface {
    network = "default"
   }
}      

I know how to add a schedule to an external disk: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_disk_resource_policy_attachment

But how do you do this with the vm_instance disk ?

Thanks


Solution

  • To create a snapshot scheduler on a boot disk, modify the value of “disk” field in “google_compute_disk_resource_policy_attachment” resource so that it points to the boot disk as it has the same name as the VM name and a “Standard Persistent disk” type by default. So using the name of the VM created i.e., “google_compute_instance.< reference-name >.name” will allow you to point the snapshot scheduler to the boot disk of the VM.

    Argument Reference for more information.

    Refer to the sample configuration below:

    Sample.tf

    resource "google_compute_disk_resource_policy_attachment" "attachment" {
      name = google_compute_resource_policy.policy.name
      disk = google_compute_instance.<reference-name>.name
      zone = "<zone-name>"
    }
    

    Reference: google_compute_disk_resource_policy_attachment