7

I have created a VM in project A in GCE. I want to create a new instance based on this VM in another project B.

I reckon I can spawn a new instance based on the a disk snapshot. However I cannot find any option to transfer such a snapshot across project.

My questions are:

1) how can I transfer a disk snapshot across projects in Google Cloud Platform projects?

2) Is there a better way to achieve this other than using a docker image?

11

Since I cannot turn off the source VM because it is currently in use in a production environment, I have to use the following steps to create a mirror VM in another project:

1) Create a snapshot of the boot disk of the source VM

2) Create a disk based on this snapshot in the target project

 gcloud compute disks create vm-prod-disk --source-snapshot \
 https://www.googleapis.com/compute/v1/projects/<source-\
 project>/global/snapshots/<source-vm-snapshot> --project target-project

3) Create a VM based on the new disk from step 2

gcloud compute instances create vm-prod-duplicate \
--project target-project --disk name=vm-prod-disk,boot=yes
1
  • 2
    Creating a snapshot of a running instance may result in an inconsistent snapshot. – Brian Dec 12 '17 at 19:47
4

You first have to create an image in your old-project

gcloud compute images create "my-image" --source-disk "my-disk"

Now, you can create a machine in another project with it, since images are global resources:

gcloud compute instances create "my-instance" \
    --image "my-image" \
    --image-project "new-project"

There are also other solutions.

3
  • I tried the image create but it complaints about The disk resource 'my-disk' is already being used by 'my-vm' – Anthony Kong Feb 9 '17 at 21:34
  • You first have to turn off the VM and detach the disk from the VM. After that you can reattach it again. – Alex Palcuie Feb 9 '17 at 21:39
  • I see. Unfortunately I cannot turn off the vm instance. It is a production instance – Anthony Kong Feb 9 '17 at 22:08
-1

create an image in your old-project

    gcloud compute images create "my-image" --source-disk "my-disk"

copy image to new-project

    gcloud compute --project=newprojectid images create image-2 --source-image=image-1 --source-image-project=oldprojectid

create instance using this image

1
  • 2
    This is almost identical to the accepted answer, with the exception that it's less complete. – Spooler Mar 20 '19 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.