31
votes

Querying the network interface will only return the internal instance IP address info.
Is it possible to get external IP address info from somewhere inside the running instance?

As an example, AWS has a metadata REST endpoint for instance info:
curl http://169.254.169.254/latest/meta-data/public-ipv4

(source: http://tech-queries.blogspot.co.uk/2011/06/how-to-get-public-ip-for-amazon-ec2.html)

2
Update: Turns out there is a metadata endpoint. For the sake of anyone else with this question - you can simply execute curl -H "Metadata-Flavor: Google" http://metadata/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ipCloud66
Consider moving your comment to an answer, so you can mark this question answered. That way other people will know it has been answered when they're searching (and you'll get credit for the answer!).Brian Dorsey
Update2: Google changed the DOMAIN for metadata: curl -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip see: cloud.google.com/compute/docs/…Andy H.

2 Answers

29
votes

You can get the public IP address with an HTTP GET of http://metadata/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip. You must set the Metadata-Flavor header to Google as per the documentation.

26
votes

Here are the links for getting Public IPv4 on various clouds.

Azure

curl -H Metadata:true "http://169.254.169.254/metadata/instance/network/interface/0/ipv4/ipAddress/0/publicIpAddress?api-version=2017-08-01&format=text"

AWS

curl http://169.254.169.254/latest/meta-data/public-ipv4

GCP

curl -H "Metadata-Flavor: Google" http://169.254.169.254/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip

Since 169.254.169.254 is a global metadata server for these above cloud vendors, no need to use different domain names.