Transferring GCE VM from One Project to Another
/ 1 min read
Table of Contents
Bad news, you can’t (as of Nov 2024). But you can kinda achieve something similar by copying the VM instead, using native GCP toolkit.
Below I’ve documented how I shifted my Moodle VM, moodlest-moodle
from ruban-moodle-cluster
to client-dev-cluster
via Google Cloud SDK in my PC.
Let’s 🚀
Step 1: Stop the Instance
gcloud compute instances stop moodlest-moodle --zone=asia-southeast1-b
Step 2: Create Snapshot
# Grab datetime firstdate +"%Y%m%d-%H%M%S" | xclip -sel clip
gcloud compute snapshots create moodlest-moodle-image-20241108-071735 \ --source-disk moodlest-moodle \ --snapshot-type STANDARD \ --source-disk-zone asia-southeast1-b
Step 3: Create Image
gcloud compute images create moodlest-moodle-image-20241108-071735 \ --source-snapshot=moodlest-moodle-image-20241108-071735 \ --storage-location=asia-southeast1
Step 4: Create VM in Project 2
gcloud compute instances create moodlest-moodle-image-20241108-071735 \ --project client-dev-cluster \ --zone asia-southeast1-b \ --image-project ruban-moodle-cluster \ --image moodlest-moodle-image-20241108-071735 --subnet projects/client-dev-server-cluster/regions/asia-southeast1/subnetworks/default
Step 5: Clean Up
After verifying that the VM works as expected in the new project, you can delete the image and snapshot from the old project to avoid unnecessary costs.
gcloud compute images delete moodlest-moodle-image-20241108-071735 --project=ruban-moodle-clustergcloud compute snapshots delete moodlest-moodle-image-20241108-071735 --project=ruban-moodle-cluster
And that’s a wrap
Now we just wait for Google to add native support for this feature