Google Cloud and Disk Snapshots for Testing

In my journey from Linux Admin to DevOps Engineer, it has been interesting to see the differences between on-premise and Google Cloud environments.

In both environments, Ansible was used to deploy and provision the virtual machine and required set of applications. Moving to the GCP environment, deploying VMs takes a bit longer than on-premise. There was a need to be able to quickly roll back a freshly deployed virtual machine for multiple tests.

This is an overview of the process for creating and rolling back to snapshots.

*Note: disk storage created in the cloud will have a cost, so be aware of your cloud budget when creating snapshots, and clean them up often.

# Log into gcloud auth
gcloud auth login

# Check the VM instance
gcloud beta compute instances list --filter="name~'$vmname'"

# Create a snapshot
gcloud compute disks snapshot $diskname --zone=$zonename --snapshot-names=$snapshotname --storage-location=$storagelocation --description="snapshot create for $vmname"

# List snapshots
gcloud compute snapshots list --filter="name~'^$snapshotname'" | awk '{print $1}' | cut -f 1

# Describe the VM instance in more detail
gcloud beta compute instances describe $vmname --zone=$zonename

# Shut down the VM before next steps
gcloud compute instances stop $vmname

# Detach the VM's disk
gcloud beta compute instances detach-disk $vmname --zone=$zonename --disk $diskname

# Create a new disk from the snapshot
gcloud compute disks create $diskname_new --source-snapshot $snapshotname --zone=$zonename

# Attach the disk created from previous step as the new bootable disk
gcloud beta compute instances attach-disk $vmname --disk $diskname_new --boot --zone=$zonename

# Configure disk auto-delete for the restored VM when destroyed
gcloud compute instances set-disk-auto-delete $vmname --disk=$diskname_new

# Delete an old disk - use caution - it will prompt you
gcloud compute disks delete $diskname --zone=$zonename

# Restart the VM
gcloud compute instances start $vmname --zone=$zonename
Advertisement