Search code examples
google-cloud-platformgoogle-compute-engine

Is it possible to change the vpc and subnet of GCP instance?


I have created one gcp instance in default vpc. But now i knew that VM needed to created in another vpc.

So far, i have done lot of work on it. So creating new vm in other vpc and do same stuff there is not possible.

I have read somewhere that VPC can be selected only while creating instance. Can i change the VPC and Subnet of my instance ?


Solution

  • Can I change the VPC and Subnet of my instance ? The short answer is Yes, you can.

    If you already have your new VPC create you can follow the steps below if not you can consult the following documentation: Creating networks

    To migrate a VM instance from one network to another.

    1. In the Cloud Console, go to the VM instances page.
    2. Click the VM instance name to open the details page.
    3. Click Stop.
    4. If a confirmation dialog appears, click Stop.
    5. After the VM stops, click Edit.
    6. Under Network interfaces, click the interface you want to move.
    7. In the Network field of the interface, select the new network for the interface.
    8. In the Subnetwork field of the interface, select the new subnet for the interface.
    9. In the Internal IP address field, specify Automatic if you want the system to allocate an IP address from the subnet range or Custom if you want to specify an unused one yourself.
    10. Click Done to close the network interface edit panel.
    11. Click Save.
    12. After the VM finishes saving, click Start.
    13. If a confirmation dialog appears, click Start.

    Also you can execute this change by gcloud command line:

    1. Stop the VM
    gcloud beta compute instances stop INSTANCE_NAME \
        --zone=ZONE_NAME
    
    1. Migrate the VM
    gcloud beta compute instances network-interfaces update INSTANCE_NAME \
        --zone=ZONE_NAME \
        --network-interface=NIC \
        --network=NETWORK_NAME \
        --subnetwork=SUBNET_NAME
    

    3.Start the VM

    gcloud beta compute instances start INSTANCE_NAME \
        --zone=ZONE_NAME
    

    Please check the following documentation for further details