GCP: assign/remove ephemeral IP to an existing instance

Viewed 7823

I have few instances in GCP and for administrative purposes I need to briefly connect in SSH and launch few commands. These instances do not have external IP in "normal" mode but for these brief maintenance I would like to assign ephemeral IP, do the maintenance and then remove them.

One can do this easily on the web interface (edit the instance change the NIC config to add ephemeral NAT IP) but I would like to avoid this since I have several instances... Am I missing something in gcloud documentation?

2 Answers

With Google Cloud SDK you could use a workflow like the following:

Set up some variables;

instance=instance-1
zone=asia-northeast2-a

Set an external ephemeral ipv4 address for the instance, issue the maintainance commands to it, and unset its external ephemeral ipv4 address;

gcloud compute instances add-access-config $instance --zone=$zone

gcloud compute ssh $instance --zone=$zone --command="maintenance #..."

gcloud compute instances delete-access-config $instance --zone=$zone

Correspondent Cloud SDK documentation links are instances/describe, instances/add-access-config, ssh and instances/delete-access-config.

Related