Google Compute Engine : Use snapshot from another project?

24,286

Solution 1

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.

Solution 2

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

Solution 3

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://www.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://www.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

Solution 4

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

Solution 5

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

Share:
24,286
Remis Haroon - رامز
Author by

Remis Haroon - رامز

Experienced Data Engineer with exposure to the design and architecture of Big Data Pipelines and integrating with legacy systems. Has several years of experience in working on highly scalable projects using leading technology stack such as Apache Spark, Hadoop, SQL, and NoSQL databases such as Cassandra.

Updated on July 09, 2022

Comments

  • Remis Haroon - رامز
    Remis Haroon - رامز almost 2 years

    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 - رامز
    Remis Haroon - رامز about 9 years
    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
  • chrispomeroy
    chrispomeroy about 9 years
    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)
  • Remis Haroon - رامز
    Remis Haroon - رامز about 9 years
    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.
  • TravisChambers
    TravisChambers almost 9 years
    It should be noted that the <name-of-project-2> is not the project name, but the project ID. dabase.com/blog/invalid_value_for_project
  • chrispomeroy
    chrispomeroy almost 9 years
    Travis, I edited my answer to incorporate your suggestion, thanks
  • Sunil Garg
    Sunil Garg almost 9 years
    @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
    Sunil Garg almost 9 years
    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 ?
  • chrispomeroy
    chrispomeroy almost 9 years
    Sunil, Create a snapshot from the disk of the running instance then follow my instruction above starting at If you do not have the disk available, but just the snapshot
  • jonny bordo
    jonny bordo over 8 years
    @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/<project-1‌​-ID>/global/images/<‌​image-name>. Do you have any idea why I'm getting this? Thanks!
  • chrispomeroy
    chrispomeroy over 8 years
    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 <project-1-ID> and the actual image name in the spot where I've listed <image name>. Your final gcloud commands should not have any angle brackets in them or it will cause a parse error as you found.
  • Admin
    Admin over 8 years
    @chrispomeroy's method worked for me, but had to specify an --image-project in addition, to be able to use the image from project1
  • dgaviola
    dgaviola over 8 years
    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.
  • nachi
    nachi over 8 years
    If you replace the "beta" in the URL with "v1", it works.
  • Jorge_Freitas
    Jorge_Freitas over 8 years
    Sooo much easier than the 'recommended' way to do a move as documented here: medium.com/google-cloud/… . And thank you so much @nachi!
  • Remis Haroon - رامز
    Remis Haroon - رامز over 8 years
    nice, may I know the source of this information? is it documented by google?
  • David Jones
    David Jones almost 8 years
    @SebastianJ. that's exactly where I started! Glad I ended up finding this answer before I got too far along in that process.
  • Cognitiaclaeves
    Cognitiaclaeves almost 8 years
    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 )
  • xref
    xref about 6 years
    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
  • Eduardo B.
    Eduardo B. over 5 years
    This is indeed the shortest way provided you have correct permissions on both projects.
  • Manel
    Manel over 5 years
    Now it is possible. Follow answer from @jiminikiz
  • MeLight
    MeLight almost 5 years
    Defo the simplest way
  • master
    master almost 4 years
    This is the correct way. Creating an image is an unnecessary step.
  • master
    master almost 4 years
    unnecessary steps, you do not need to create an image beforehand.
  • Prajwal
    Prajwal almost 4 years
    Will this also transfer the GPU limit(from all quotas)?