32

I have two projects in my developer console. I have taken a "Snapshot" of one of the VMs in project-1. I want to create a new VM in project-2 using the snapshot created in project-1. Right now snapshot is not showing in the option. How can I import snapshot from one project to another?

Remis Haroon - رامز
  • 3,304
  • 4
  • 34
  • 62

7 Answers7

49

You can create an image from the snap in Project 1, then create an instance from that image using Project 2.

I'm assuming you have edit rights in both projects.

Your question says you have a snapshot and want to make an instance in project 2 from the snap in project 1.

If you still have the disk available that you had snapshotted, make sure it's no longer attached to an instance. If it's still attached to the instance, uncheck "delete boot disk when deleting instance" and delete the instance. Go to Images and click create image from disk, and create an image from this disk.

If you do not have the disk available, but just the snapshot, create an instance and set the boot disk as a snapshot and select your snapshot. Then follow the directions above to create an image by deleting the instance first.

Now you have an image in project 1. You should see it listed under images.

I'm not sure why, but you won't see the image listed in the console in project 2, however you can use gcloud to create an instance in project 2 using the image from project 1. In project 1, click on the image and click "view REST" there will be a full URL to the image, similar to this:

https://www.googleapis.com/compute/v1/projects/cpomeroy-whitelist/global/images/ruby-image

Use gcloud to create an instance in project 2 using the image in project 1:

gcloud config set project <project-id-of-project-2>
gcloud config list

(You should verify you are in project 2)

gcloud compute instances create <name of instance> --image https://www.googleapis.com/compute/v1/projects/cpomeroy-whitelist/global/images/ruby-image

Obviously your URL will be different.

I just tested this and it works. Let me know if you need more help.

chrispomeroy
  • 800
  • 5
  • 6
  • Thanks for the suggestion. I check for the option to authorize the gserviceaccount from Project 1 to project2. Can you please help me how to do that? Thanks in advance – Remis Haroon - رامز Apr 14 '15 at 03:47
  • You don't need to mess around with gserviceaccounts if you have edit access to both projects (presumably you do since you are the creator of both I imagine) – chrispomeroy Apr 15 '15 at 05:47
  • Thank You @chrispomeroy , I tried your suggestion, and it worked very well. Thanks for the time and effort spend on solving this and for the overall contributions. – Remis Haroon - رامز Apr 15 '15 at 06:40
  • @chrispomeroy, I don't have image yet but I want to create it from running insatnce without deleting it . How can I do that ? – Sunil Garg Aug 06 '15 at 07:33
  • Sunil, Create a [snapshot from the disk](https://cloud.google.com/sdk/gcloud/reference/compute/disks/snapshot?hl=en) of the running instance then follow my instruction above starting at **If you do not have the disk available, but just the snapshot** – chrispomeroy Aug 06 '15 at 22:40
  • @chrispomeroy, Hey, I'm trying to accomplish the same thing, and I've done what you've suggested. The problem is, after trying to create the new instance I get this error: ```ERROR: (gcloud.compute.instances.create) could not parse resource: [https://www.googleapis.com/compute/beta/projects//global/images/```. Do you have any idea why I'm getting this? Thanks! – jonny bordo Aug 24 '15 at 10:50
  • Typically you see the convention here and on forums and documentation where angle brackets "< >" are used when you're suppose to replace the actual name in that spot. So you should put in your actual project name in the spot where i"ve listed and the actual image name in the spot where I've listed . Your final gcloud commands should not have any angle brackets in them or it will cause a parse error as you found. – chrispomeroy Aug 25 '15 at 16:29
  • 4
    If you replace the "beta" in the URL with "v1", it works. – nachi Sep 15 '15 at 15:42
  • 1
    Sooo much easier than the 'recommended' way to do a move as documented here: https://medium.com/google-cloud/how-to-move-a-vm-instance-between-projects-on-google-cloud-platform-39d1690c73a . And thank you so much @nachi! – Sebastian J. Oct 01 '15 at 14:12
  • @chrispomeroy's method worked for me, but had to specify an --image-project in addition, to be able to use the image from project1 – John Sercel Aug 29 '15 at 22:27
  • As John says I also had to add --image-project, but keep in mind that instead of passing the whole URL to --image you just need to pass your image name there. – dgaviola Sep 07 '15 at 13:49
  • @SebastianJ. that's exactly where I started! Glad I ended up finding this answer before I got too far along in that process. – David Jones Jul 14 '16 at 00:26
  • This is a perfect solution. That link at medium.com ( where I started as well ) must be some sort of hazing. ( Welcome to the cloud!! Here's all you have to do when this happens ... ) My REST output didn't give me a full URL, however, so I actually finished it with jiminikiz's solution. ( below ) – Cognitiaclaeves Jul 27 '16 at 09:31
  • unnecessary steps, you do not need to create an image beforehand. – master May 26 '20 at 18:24
  • Will this also transfer the GPU limit(from all quotas)? – Prajwal Aug 06 '20 at 06:09
29

You don't need an image or a scratch VM, and you don't have to interrupt the source VM. Just create a snapshot in the source project:

$ gcloud compute --project p1 disks snapshot the-snapshot src-disk --snapshot-names=the-snapshot
Created [https://compute.googleapis.com/compute/v1/projects/p1/global/snapshots/the-snapshot].

Then create a disk in the destination project with --source-snapshot pointing at the 'Created' URL returned above:

$ gcloud compute --project p2 disks create the-disk \
    --source-snapshot https://compute.googleapis.com/compute/v1/projects/p1/global/snapshots/the-snapshot

This usage was not shown in the gcloud docs, I found it in @krishna praveen's answer, but his explanation is incorrect; you do not need to delete any instances, or use images. And this works even if both source and destination are boot disks:

$ gcloud compute --project p2 instances create the-vm --disk name=the-disk,boot=yes

If for some reason you require an image, you can still restore a snapshot to a disk and use this to create the image without a scratch VM. This is preferable if a scratch VM would automatically start services on boot which could interfere with other running VMs on the same project network.

$ gcloud compute images create image-1 --source-disk=src-disk-image --source-disk-zone=zone1

This image can now be used from another project (as shown by @jiminikiz above).

$ gcloud compute --project p2 instances create <name-of-new-instance> --image image-1 \
    --image-project p1 --zone=zone
vdm
  • 481
  • 4
  • 8
  • Can this work in Deployment Manager, where you only have the param "sourceImage" to define what the disk should be based on? there doesn't appear to be any "sourceSnapshot" equivalent – xref Mar 12 '18 at 01:49
  • This is the correct way. Creating an image is an unnecessary step. – master May 26 '20 at 18:22
28

The answer posted by @chrispomeroy worked for me, but I was able to simplify it a little as I need to do this more and more.

Let's say you have an image in project-1, and need to create an instance using that image in project-2.

gcloud config set project "project-2"
gcloud compute instances create <name-of-new-instance> \
    --image <name-of-your-image-from-project-1> \
    --image-project "project-1"

This eliminates the need to worry about using a URL for anything.

EDIT: My answer pretty much looks like his at this point, but the docs for this stuff is here:

gcloud compute instances create

jiminikiz
  • 2,867
  • 1
  • 25
  • 28
2

The solution provided by "chrispomeroy" works fine but require to init gcloud with your personal google user account (instead of the project2 service account) first (since it is the one who has permission to access to both project):

gcloud init (and chose [2] Login with new credentials)

Then you can indeed create the VM on project 2 (from a base image on project 1) with :

gcloud compute instances create testimg --image --image-project (no need for URL) I tested today (nov 2015) and works fine

2

This is click only solution through the browser. What you need? You need to have image. To create image from disk, the disk must be detached from any instance.

What are the steps if you have just instance in Project1:

  • Create snapshot from the instance in Project1.

  • Create instance from this snapshot in Project1. Untick "Delete boot disk when instance is deleted". This instance it's used only for
    now and gonna be deleted

  • Delete the instance that you just created

  • Go to the "Disks" menu and you must see there the disk from the instance.

  • Go to "Images" menu -> "Create an image". Here you can create image. If you don't have detached disk you won't have any disk available in the dropdown.

  • Go to Project 2 and create instance using the custom image that you created for Project 1. How? Boot disk -> change -> Custom images-> Select Project 1-> Here you can see your custom image

makkasi
  • 6,328
  • 4
  • 45
  • 60
0

AFAIK, it is not possible. To accomplish what you have described, the best course of action is to use this tutorial. You have a few steps for creating a blank disk, attaching it to the machine in question, tarball the boot partition and upload it to cloud storage. Once that is done, download it locally, switch projects and upload to the other project. You will then be able to just select the machine from the list of Images when clicking on New Instance

Marius I
  • 1,461
  • 12
  • 13
  • In the tutorial it is asking to format and mount the external disk, but in my case an external disk is already attached and i cant format it , any other option ? – Sunil Garg Aug 06 '15 at 07:52
  • Now it is possible. Follow answer from @jiminikiz – Manel Feb 05 '19 at 10:42
0

Instances can be created across the project using images. Now, if you have an image in Project 1, you can select that in Project 2 as well.

But, as of today, you can't see the disks across the project. If you have to migrate an VM with associated additional disks from Project 1 to Project 2, follow the below.

  1. Use the snapshot and create the disk in Project 2 by connecting the gcloud command interface. Connect to Project 2, and then execute:

    gcloud compute --project "GCPProject2" disks create "myserver-disk1" --size "50" --zone "us-east1-b" --source-snapshot https://www.googleapis.com/compute/v1/projects/GCPProject1/global/snapshots/snapshot-myserver-disk1 --description "DriveName" --type "pd-standard"
    
  2. Above will create the disk in Project2. Then you need to delete the instance in Project1 by retaining the boot disk. Then, create the image from the disk myserver-bootdisk. Once the image is created, switch over to Project 2 and then create the server from image, use the dropdown and select the image from Project 1.

  3. Add the additional disk which you have crated from the snapshot and create the VM.

Chait
  • 1,052
  • 2
  • 18
  • 30