'Copying VM between projects in GCP

I'm currently using windows VM instance in GCP(project1), i want to create same VM with data in my another project(project2)(project in another gmail account). The project2 have access to my project1 which i had setup IAM role.

So what the next step to copy the instance between the project1 to project2 (without loosing the data)?



Solution 1:[1]

You can copy a VM instance from one project to another either using a custom image or using disk.

Method 1: Using Custom image

You have to use the following steps to create a mirror VM in another project:

  1. In your source project, create a snapshot of the VM's boot disk, using one of the below command:

    $ gcloud compute snapshots create <SNAPSHOT_NAME> --source-disk <SOURCE_DISK> --source-disk-zone <SOURCE_DISK_ZONE>
    
  2. Create a custom image from the snapshot using the following command:

    $ gcloud compute images create <IMAGE_NAME> --source-snapshot=<SOURCE_SNAPSHOT> [--storage-location=<LOCATION>]
    
  3. In your destination project, create a VM from the custom image using the following command:

    $ gcloud compute instances create <VM_NAME> --image-project <IMAGE_PROJECT> [--image <IMAGE> | --image-family <IMAGE_FAMILY>]
    

Method 2: Using disk

  1. In your source project, create a snapshot of the VM's boot disk, using one of the below command:

    $ gcloud compute snapshots create <SNAPSHOT_NAME> --source-disk <SOURCE_DISK> --source-disk-zone <SOURCE_DISK_ZONE>
    
  2. Then create a disk in the destination project with --source-snapshot:

    $ gcloud compute disks create <DISK_NAME> --source-snapshot <SOURCE_SNAPSHOT> --project <destination-project>
    
  3. Create a VM based on the new disk from step 2

    $ gcloud compute instances create <VM_NAME> --project <destination-project> --disk name=<DISK_NAME>,boot=yes
    

Refer Copying VMs between projects for information.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jyothi Kiranmayi