Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a reserved IP to instance from command line (google cloud)

Tags:

gcloud

I'm using google cloud compute engine, on it I have 4 instances running Ubuntu 18.04. I have also reserved a static IP address in "VPC Network --> External IP Addresses".

Now I want to programatically assign that IP address to one of the different instances each time. In the console for google cloud it's easy, just click on "change" next to the reserved IP and "assign to" whatever instance I want. But how to do that via the command line with gcloud?

I have researched gcloud and gcloud beta commands but could not find it. Is there any solution?

like image 996
Ofer Sadan Avatar asked Jul 23 '18 09:07

Ofer Sadan


1 Answers

Thanks to @Mangu 's help I was able to solve this, so writing this for anyone else that has a problem with it. When assigning a new address you must first delete the old one from the instance and then add a new one. The basic commands are:

gcloud compute instances delete-access-config [INSTANCE_NAME] --access-config-name "[ACCESS_CONFIG_NAME]"
gcloud compute instances add-access-config [INSTANCE_NAME] --access-config-name "[ACCESS_CONFIG_NAME]" --address [IP_ADDRESS]

Access config name is usually, by default, "External NAT". To make sure, you can run gcloud compute instances describe [INSTANCE_NAME] and check the name.

Commands taken from here: https://cloud.google.com/compute/docs/ip-addresses/reserve-static-external-ip-address#IP_assign

like image 91
Ofer Sadan Avatar answered Nov 07 '22 02:11

Ofer Sadan