Search code examples
pythongoogle-cloud-platformipgoogle-compute-enginegcloud

How to switch an IP from an instance to another with gcloud


Is there a way to "detach" a reserved internal or external IP address from a VM to assign it to another instance with the gcloud command?

I want to do this as I'm scripting with Python and want to automate this process.


Solution

  • Following commands might help you -

    [1]. To reserve external ip addresses.

    gcloud compute addresses create mysecond  \
        --region=us-central1
    

    [2]. To unset the external ip address.

    gcloud compute instances delete-access-config instance-1 \
     --access-config-name="External NAT"
    

    [3]. To assign a new external ip address to a resource.

    gcloud compute instances add-access-config instance-1 \
    --access-config-name="External NAT" --address=0.0.0.0(Your external ip address)
    

    [4]. To list available external ip addresses in project

    gcloud compute addresses list
    

    gcp public documentation link -

    https://cloud.google.com/compute/docs/ip-addresses/reserve-static-external-ip-address