Retrieving GCE instance metadata

456 views
Skip to first unread message

Sivaram Iyer

unread,
Apr 9, 2021, 11:53:04 AM4/9/21
to gce-discussion
Hi,

How can I access the current instance's metadata (instance ID, name, tags etc) within a java application that is deployed on a gce compute instance? Any reference to the java docs/API samples for the same would be very helpful.

Those from AWS land, I'm looking to achieve something similar as provided by - 

Any pointers much appreciated, thanks in Advance!

Best,
Siv

Derek Murphy

unread,
Apr 9, 2021, 1:38:18 PM4/9/21
to gce-discussion

I am wondering if you have looked at this link[1]. It does not contain Java code but you should be able to run the “curl” command and receive the metadata you want.

It does state : 

“Every instance stores its metadata on a metadata server. You can query this metadata server programmatically, from within the instance and from the Compute Engine API. “

 Does this help?


[1] Storing and retrieving instance metadata : 

https://cloud.google.com/compute/docs/storing-retrieving-metadata

Sivaram Iyer

unread,
Apr 9, 2021, 2:55:47 PM4/9/21
to gce-discussion
Yes, it certainly helps, and noticed that. 
I was wondering whether there is any client/exposed API methods that can be leveraged from within the context of CE, rather than placing a URL call to the metadata server. If there is any exposed methods/utilities, I'd prefer that than having to make a URL call. 

-Siv

Derek Murphy

unread,
Apr 12, 2021, 4:33:20 PM4/12/21
to gce-discussion

I was wondering if you have seen this link[1]. It may be of interest to you.


[1] Method: instances.get : 

https://cloud.google.com/compute/docs/reference/rest/v1/instances/get

Digil (Google Cloud Platform Support)

unread,
Apr 13, 2021, 10:15:01 AM4/13/21
to gce-discussion
In support with the other community support member's comment, you could additionally use the 'gcloud compute instances describe' command to query instance metadata as well. Details on using this command to query the metadata is explained in the gcloud tab of 'querying custom metadata' article. As you can see over there, you have to use the  '--flatten' flag to scope the output to a relevant metadata key.
Reply all
Reply to author
Forward
0 new messages