This document describes how to copy your VM to a different project.
Before you begin
- Review Best practices for persistent disk snapshots and prepare your boot disk for snapshots.
- If you haven't already, set up authentication
.
Authentication verifies your identity for access to Google Cloud services and APIs. To run
code or samples from a local development environment, you can authenticate to
Compute Engine by selecting one of the following options:
-
Install the Google Cloud CLI. After installation, initialize the Google Cloud CLI by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity .
- Set a default region and zone .
-
Required roles
To get the permissions that
you need to copy VM between projects,
ask your administrator to grant you the Compute Instance Admin (v1)
( roles/compute.instanceAdmin.v1
)
IAM role on the project.
For more information about granting roles, see Manage access to projects, folders, and organizations
.
This predefined role contains the permissions required to copy VM between projects. To see the exact permissions that are required, expand the Required permissionssection:
Required permissions
The following permissions are required to copy VM between projects:
-
compute.instances.createon the project - To use a custom image to create the VM:
compute.images.useReadOnlyon the image - To use a snapshot to create the VM:
compute.snapshots.useReadOnlyon the snapshot - To use an instance template to create the VM:
compute.instanceTemplates.useReadOnlyon the instance template - To specify a subnet for your VM:
compute.subnetworks.useon the project or on the chosen subnet - To specify a static IP address for the VM:
compute.addresses.useon the project - To assign an external IP address to the VM when using a VPC network:
compute.subnetworks.useExternalIpon the project or on the chosen subnet - To assign a legacy network
to the VM:
compute.networks.useon the project - To assign an external IP address to the VM when using a legacy network:
compute.networks.useExternalIpon the project - To set VM instance metadata for the VM:
compute.instances.setMetadataon the project - To set tags for the VM:
compute.instances.setTagson the VM - To set labels for the VM:
compute.instances.setLabelson the VM - To set a service account for the VM to use:
compute.instances.setServiceAccounton the VM - To create a new disk for the VM:
compute.disks.createon the project - To attach an existing disk in read-only or read-write mode:
compute.disks.useon the disk - To attach an existing disk in read-only mode:
compute.disks.useReadOnlyon the disk
You might also be able to get these permissions with custom roles or other predefined roles .
Copy a VM to another project
-
In your source project, create a snapshot of the VM's boot disk, using one of the following commands:
Zonal boot disk
If your VM has a zonal boot disk, create a snapshot using the following command:
gcloud compute snapshots create SNAPSHOT_NAME \ --source-disk SOURCE_DISK \ --snapshot-type SNAPSHOT_TYPE \ --source-disk-zone SOURCE_DISK_ZONE
Replace the following:
- SNAPSHOT_NAME : A name for the snapshot.
- SOURCE_DISK : The name of the zonal Persistent Disk volume from which you want to create a snapshot.
- SNAPSHOT_TYPE : The snapshot type, either STANDARDor ARCHIVE. If a snapshot type is not specified, a STANDARDsnapshot is created. Choose Archive for more cost-efficient data retention.
- SOURCE_DISK_ZONE : The zone of the zonal Persistent Disk volume from which you want to create a snapshot.
Regional boot disk
If your VM has a regional boot disk, create a snapshot using the following command:
gcloud compute snapshots create SNAPSHOT_NAME \ --source-disk SOURCE_DISK \ --source-disk-region= SOURCE_DISK_REGION \ --snapshot-type= SNAPSHOT_TYPE
Replace the following:
- SNAPSHOT_NAME : A name for the snapshot.
- SOURCE_DISK : The name of the regional Persistent Disk volume from which you want to create a snapshot.
- SOURCE_DISK_REGION : The region of the regional Persistent Disk volume from which you want to create a snapshot.
- SNAPSHOT_TYPE : The snapshot type, either STANDARDor ARCHIVE. If a snapshot type is not specified, a STANDARDsnapshot is created.
-
Create a custom image from the snapshot using the following command:
gcloud compute images create IMAGE_NAME \ --source-snapshot= SOURCE_SNAPSHOT \ [--storage-location= LOCATION ]
Replace the following:
-
IMAGE_NAME: a name for the new image -
SOURCE_SNAPSHOT: the snapshot from which you want to create the image -
LOCATION: Optional: a flag that lets you designate the region or multi-region where your image is stored. For example, specifyusto store the image in theusmulti-region, orus-central1to store it in theus-central1region. If you don't make a selection, Compute Engine stores the image in the multi-region closest to your image's source location.
-
-
Optional: Share the custom image with users who create VMs in the destination project. For more information about sharing custom images, see Sharing custom image within an organization .
-
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_FLAG \ --subnet SUBNET
Replace the following:
-
VM_NAME: the name of the VM -
IMAGE_PROJECT: the ID of the Google Cloud project that contains the image -
IMAGE_FLAG: specify one of the following:- Use the
--image IMAGE_NAMEflag to specify a custom image.For example,
--image my-debian-image-v2. - If you created your custom images
as part of a custom image family
,
use the
--image-family IMAGE_FAMILY_NAMEflag to specify that custom image family.This creates the VM from the most recent, non-deprecated OS image and OS version in your custom image family. For example, if you specify
--image-family my-debian-family, Compute Engine creates a VM from the latest OS image in your custommy-debian-familyimage family.
- Use the
-
SUBNET: if the subnet and instance are in the same project, replaceSUBNETwith the name of a subnet that is in the same region as the instance.To specify a subnet in a Shared VPC network, replace
SUBNETwith a string of the form:projects/ HOST_PROJECT_ID /regions/ REGION /subnetworks/ SUBNET_NAME
Replace the following:
-
HOST_PROJECT_ID: the project ID of the Shared VPC host project -
REGION: the region of the subnet -
SUBNET_NAME: the name of the subnet
The region of the subnet for a Shared VPC network must also match the region containing the instance.
-
-
What's next
- Customize the destination project's VPC network .

