Gabe Black
Gabe Black

Reputation: 11

Querying GCE instance properties from the VM itself

I want to be able to query the external IP address of a GCE instance when the instance starts up. I'm planning to use that to fix up some configs which are copied to multiple similar instances. Is there a way to automatically discover an instance's external IP(s) or other properties from the instance itself? I see there are some things you can query with the gcloud tool, but for that you have to know the instance name, and it's not clear where to get that from.

Upvotes: 1

Views: 161

Answers (1)

Antxon
Antxon

Reputation: 1943

See Querying metadata in GCE public documentation. For example, for the instance's external IP:

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

This command will query the instance's private metadata server. Another option is configuring the instance's service account with the right scopes as described at Preparing an instance to use service accounts in the public documentation. This way, gcloud command can be used directly in the instance to get information from the project without authentication.

Upvotes: 2

Related Questions