Is there any way to retrieve custom instance metadata value for a specific key ?
I tried gcloud compute instances describe instance-1
command, this return whole meta data text. But i just want to retrieve the value of a specific key only.
Asked
Active
Viewed 7,494 times
10

user3852688
- 42
- 5

junsid
- 335
- 3
- 14
-
Have you tried using the google cloud REST api? – Vishnu P N Feb 21 '18 at 10:59
-
Yes i tried that from google sdk, but where i face an authentication issue. – junsid Feb 21 '18 at 11:09
1 Answers
15
The gcloud
tool has built-in filters and formats, for details you can read gcloud topic filters
, gcloud topic formats
, and gcloud topic projections
.
For your specific use case, you can get the metadata value with:
gcloud compute instances describe INSTANCE-ID \
--format='value[](metadata.items.YOUR-KEY)'

Jofre
- 3,718
- 1
- 23
- 31
-
Thanks so much for this -- exactly what I was looking for + had been trying to recreate. What does the `[]` do there exactly? Seems to return the result fine even without that. – chinabuffet Apr 17 '18 at 17:03
-
2The `[]` are for the arguments. Indeed, they are not required in this instance, but I like to keep the brackets as a good practice to show I'm passing no arguments, and to avoid forgetting them when I need to pass them. – Jofre Apr 17 '18 at 17:29