How to Copy a GCP Compute Engine Instance between projects

Google Cloud Platform (GCP) is one of the leading cloud service providers, offering a wide range of tools and services to help businesses and developers build, deploy, and scale applications seamlessly. One of the most widely used services within GCP is Compute Engine, which allows you to create and manage virtual machines (VMs) tailored to your specific needs. Whether you’re running a web application, a database, or a development environment, Compute Engine provides the flexibility and scalability required for modern workloads.

However, as organizations grow or restructure, there may come a time when you need to transfer a Compute Engine instance from Project A to Project B. This is often the case when migrating resources between teams, consolidating projects, or even collaborating with another organization. While GCP does not provide a direct “copy” function for moving VM instances between projects—especially when the projects are owned by different accounts—there is a reliable workaround using snapshots and shared permissions.

In this comprehensive guide, we’ll walk you through the entire process step by step, covering everything from creating snapshots of your VM to setting up permissions and deploying the instance in the target project. Whether you’re a seasoned cloud engineer or a beginner exploring GCP, this guide will provide you with the knowledge and tools to perform this task efficiently. By the end of this article, you’ll be able to migrate your GCP Compute Engine instance seamlessly, even across projects owned by separate accounts.

Let’s dive in and simplify what might initially seem like a complex migration process.



Why Copy a GCP Compute Instance Between Projects?

There are several reasons why you might need to copy a VM instance between projects:

  • Project Migration: Moving resources to a new project for better organization or billing purposes.
  • Collaboration: Sharing a VM with another team or organization.
  • Backup and Recovery: Creating a backup of a VM in a different project for disaster recovery.

Prerequisites

Before you begin, ensure the following:

  1. Access Permissions:
    • You have Owner or Editor permissions in Project A (source project).
    • You have sufficient permissions (e.g., roles/compute.admin) in Project B (target project).
  2. Billing Enabled:
    • Both projects must have billing enabled to create and use resources.
  3. Cloud SDK Installed:
  4. Source VM Snapshot:
    • You’ll need to create a snapshot of the source VM instance.

Step-by-Step Guide to Copy a GCP Compute Instance

Step 1: Create a Snapshot of the Source VM in Project A

A snapshot is a point-in-time copy of the VM’s disk. To create a snapshot:

  1. Open the Google Cloud Console for Project A.
  2. Go to Compute Engine > Snapshots.
  3. Click Create Snapshot.
  4. Fill in the required details:
    • Name: Give the snapshot a unique name (e.g., my-vm-snapshot).
    • Source Disk: Select the disk of the source VM.
    • Location: Choose a multi-region or region close to your target project.
  5. Click Create.

Alternatively, you can use the following gcloud command:

gcloud compute snapshots create my-vm-snapshot \
    --source-disk=SOURCE_DISK_NAME \
    --source-disk-zone=SOURCE_DISK_ZONE \
    --project=PROJECT_A_ID

Replace:

  • SOURCE_DISK_NAME with the name of the source VM’s disk.
  • SOURCE_DISK_ZONE with the zone of the source VM (e.g., us-central1-a).
  • PROJECT_A_ID with the ID of Project A.

Step 2: Share the Snapshot with Project B

To allow Project B to access the snapshot, you must grant permissions to the target account.

  1. In Project A, go to Compute Engine > Snapshots.
  2. Select the snapshot you created.
  3. Click Show Info Panel (or the pencil icon for editing).
  4. Add an IAM Policy Binding:
    • Memberuser:[email protected] (replace with the email of the target account).
    • RoleCompute Storage Admin.

Alternatively, use this gcloud command:

gcloud compute snapshots add-iam-policy-binding my-vm-snapshot \
    --member="user:[email protected]" \
    --role="roles/compute.storageAdmin" \
    --project=PROJECT_A_ID


Step 3: Use the Snapshot to Create a Disk in Project B

Now that the snapshot is shared, you can create a new disk in Project B using the snapshot.

  • Switch to Project B in the Google Cloud Console or Cloud Shell:
gcloud config set project PROJECT_B_ID
  • Create a new disk from the shared snapshot:
gcloud compute disks create my-new-disk \
    --source-snapshot=projects/PROJECT_A_ID/global/snapshots/my-vm-snapshot \
    --zone=TARGET_ZONE \
    --project=PROJECT_B_ID

    Replace:

    • PROJECT_A_ID with the ID of Project A.
    • my-vm-snapshot with the name of the snapshot.
    • TARGET_ZONE with the zone where you want to create the disk (e.g., us-central1-a).
    • PROJECT_B_ID with the ID of Project B.

    Step 4: Create a New VM in Project B Using the Disk

    Once the disk is created, you can use it to launch a new VM in Project B.

    • Run the following command to create a new VM:
    gcloud compute instances create my-new-vm \
        --zone=TARGET_ZONE \
        --disk=name=my-new-disk,boot=yes,auto-delete=no \
        --project=PROJECT_B_ID
    
    
    • Replace the placeholders with the appropriate values:
      • my-new-vm: Name of the new VM.
      • my-new-disk: Name of the disk created from the snapshot.
      • TARGET_ZONE: Zone where the VM will be created.
      • PROJECT_B_ID: ID of Project B.

    Step 5: Verify the New VM

    After the VM is created, verify that it’s running and accessible:

    1. Go to Compute Engine > VM Instances in Project B.
    2. Check the status of the new VM.
    3. SSH into the VM to ensure it’s working as expected.

    Best Practices

    • Clean Up Resources: Delete the snapshot in Project A if it’s no longer needed to avoid unnecessary charges:
    gcloud compute snapshots delete my-vm-snapshot --project=PROJECT_A_ID
    
    • Use Labels: Add labels to the new VM and disk in Project B for better resource management.
    • Monitor Costs: Keep an eye on billing for both projects to avoid unexpected charges.

    Conclusion

    Copying a GCP Compute Engine instance from one project to another, especially when the projects are owned by different accounts, involves creating a snapshot, sharing it, and then using it to create a new VM. By following the steps outlined in this guide, you can successfully migrate your VM while ensuring security and efficiency.

    If you found this guide helpful, feel free to share it with others or leave a comment below. Let us know if you have any questions or additional tips for managing GCP resources!

    Leave a Comment