Identify which compute instance a static external IP address is associated with

6/5/2018

Is it possible to identify which compute instances a static external IP address is associated with? I see the following chain of connection:

address -> forwarding rule -> target proxy -> url map -> backend service -> instance group

Now I can list the instances contained in the eventual instance group. But this is a long chain to follow. Is there any idiomatic way to achieve this quickly, or will I need to script this?

-- mkingston
gcloud
google-cloud-platform
google-kubernetes-engine

1 Answer

6/5/2018

Notice that gcloud can list the external IPs of an instance:

$ gcloud compute instances list
NAME  ZONE          MACHINE_TYPE   ...  EXTERNAL_IP  STATUS
test  asia-east1-a  n1-standard-1  ...  1.1.1.1      RUNNING
...

If you run again with the --verbosity info flag, you'll see that the EXTERNAL_IP row in the column comes from (see gcloud topic formats and gcloud topic projections):

networkInterfaces[].accessConfigs[0].natIP.notnull().list():label=EXTERNAL_IP

You can get a list of all of the NAT IPs for an instance via (notice the dropped 0 for accessConfigs; this aggregates all NAT IPs:

$ gcloud compute instances list \
      --format 'value(networkInterfaces[].accessConfigs[].natIP)'
[u'1.1.1.1']
[u'1.1.1.2']
...

With a specific IP in mind, you can turn that into a filter expression:

$ gcloud compute instances list \
      --filter 'networkInterfaces[].accessConfigs[].natIP:1.1.1.1'
NAME  ZONE          MACHINE_TYPE   ...  EXTERNAL_IP  STATUS
test  asia-east1-a  n1-standard-1  ...  1.1.1.1      RUNNING
...

This works for me when the instance is part of an instance group.

-- Zachary Newman
Source: StackOverflow