I have a VM instance in Google Cloud with external ephemeral IP. The IP is supposed to change when the instance is stopped and started but it does not. How can I manually change the ephemeral IP?
I have a VM instance in Google Cloud with external ephemeral IP. The IP is supposed to change when the instance is stopped and started but it does not. How can I manually change the ephemeral IP?
Accordingly to the documentation IP Addresses section External IP addresses:
Ephemeral external IP addresses are available to VM instances and forwarding rules. Ephemeral external IP addresses remain attached to a VM instance only until the VM is stopped and restarted or the instance is terminated. If an instance is stopped, any ephemeral external IP addresses that are assigned to the instance are released back into the general Compute Engine pool and become available for use by other projects. When a stopped instance is started again, a new ephemeral external IP address is assigned to the instance.
As a possible workaround you can reserve an external IP address each time when you want to change it. Please have a look at my steps below:
$ gcloud compute instances create instance-1 --zone=us-central1-a --machine-type=n1-standard-1 --image=ubuntu-1804-bionic-v20200610 --image-project=ubuntu-os-cloud
Created [https://www.googleapis.com/compute/v1/projects/test-prj/zones/us-central1-a/instances/instance-1].
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
instance-1 us-central1-a n1-standard-1 10.128.0.5 35.239.122.55 RUNNING
$ gcloud compute addresses create external-ip-upd-1 --region=us-central1
Created [https://www.googleapis.com/compute/v1/projects/test-prj/regions/us-central1/addresses/external-ip-upd-1
$ gcloud compute addresses list
NAME ADDRESS/RANGE TYPE PURPOSE NETWORK REGION SUBNET STATUS
external-ip-upd-1 34.66.150.26 EXTERNAL us-central1 RESERVED
$ gcloud compute instances describe instance-1 --zone=us-central1-a
...
networkInterfaces:
- accessConfigs:
- kind: compute#accessConfig
name: external-nat
natIP: 34.66.150.26
networkTier: PREMIUM
type: ONE_TO_ONE_NAT
fingerprint: 4vzdAl27owk=
kind: compute#networkInterface
name: nic0
network: https://www.googleapis.com/compute/v1/projects/test-prj/global/networks/default
networkIP: 10.128.0.5
subnetwork: https://www.googleapis.com/compute/v1/projects/test-prj/regions/us-central1/subnetworks/default
...
$ gcloud compute instances delete-access-config instance-1 --access-config-name "external-nat" --zone=us-central1-a
Updated [https://www.googleapis.com/compute/v1/projects/test-prj/zones/us-central1-a/instances/instance-1].
$ gcloud compute instances list
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
instance-1 us-central1-a n1-standard-1 10.128.0.5 RUNNING
$ gcloud compute instances add-access-config instance-1 --access-config-name "external-nat-upd-1" --address 34.66.150.26 --zone=us-central1-a
Updated [https://www.googleapis.com/compute/v1/projects/test-prj/zones/us-central1-a/instances/instance-1].
$ gcloud compute instances list
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
instance-1 us-central1-a n1-standard-1 10.128.0.5 34.66.150.26 RUNNING
$ gcloud compute addresses create external-ip-upd-2 --region=us-central1
$ gcloud compute addresses list
NAME ADDRESS/RANGE TYPE PURPOSE NETWORK REGION SUBNET STATUS
external-ip-upd-2 34.71.149.223 EXTERNAL us-central1 RESERVED
external-ip-upd-1 34.66.150.26 EXTERNAL us-central1 IN_USE
$ gcloud compute instances delete-access-config instance-1 --access-config-name "external-nat-upd-1" --zone=us-central1-a
$ gcloud compute instances add-access-config instance-1 --access-config-name "external-nat-upd-2" --address 34.71.149.223 --zone=us-central1-a
$ gcloud compute instances list
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
instance-1 us-central1-a n1-standard-1 10.128.0.5 34.71.149.223 RUNNING
$ gcloud compute addresses delete external-ip-new --region=us-central1
If you noticed that newly reserved IP was used some time ago you can reserve another IP address to get another one and then release unused IPs.
UPDATE If you think that you found an issue you can file a report at Google Public Issue Tracker or request a feature to avoid manual reconfiguration.