Creating a snapshot of a Compute Engine instance’s boot disk allows you to preserve the disk’s state and use it to create new instances or restore the disk to a previous state. Here’s how to create a snapshot using GCP Console, gcloud CLI, and Terraform:
1. Using GCP Console:
a. Go to the GCP Console: https://console.cloud.google.com/
b. Navigate to Compute Engine > Disks.
c. Locate the boot disk of the instance you want to create a snapshot for, and click on the disk name.
d. Click on the “Create snapshot” button at the top of the page.
e. Enter a name for the snapshot and configure any additional settings (such as encryption or location).
f. Click the “Create” button to create the snapshot.
2. Using gcloud CLI:
a. First, get the boot disk name by running the following command:
gcloud compute instances describe INSTANCE_NAME --zone ZONE --format="get(disks[0].source.basename())"
Replace INSTANCE_NAME
and ZONE
with appropriate values.
b. Run the following command to create a snapshot of the boot disk:
gcloud compute disks snapshot DISK_NAME --snapshot-names SNAPSHOT_NAME --zone ZONE
Replace DISK_NAME
, SNAPSHOT_NAME
, and ZONE
with appropriate values.
3. Using Terraform:
Terraform does not provide a direct way to create a snapshot of an existing instance’s boot disk. However, you can create a snapshot when defining a boot disk in a Terraform configuration:
resource "google_compute_instance" "example" {
# ... existing configuration ...
}
resource "google_compute_snapshot" "example" {
name = "example-snapshot"
source_disk = google_compute_instance.example.boot_disk.0.source
zone = "ZONE"
}
Replace ZONE
with the appropriate value.
b. Run the following commands to apply the changes:
terraform init
terraform plan
terraform apply
This will create a snapshot of the instance’s boot disk when the instance is created using the Terraform configuration. Note that this approach is different from creating a snapshot of an existing instance’s boot disk, as it’s done during the instance creation process.
Leave a Reply