Azure metadata service

I discovered this purely by chance this week and thought it would be useful to share.

Seems there is some functionality in Azure that lets you extract some information about the VM (that is not directly available on the VM itself) from the Azure platform itself - it's easier to explain what I mean by a simple example

If i'm logged in to a VM I have no way of knowing what type/size of VM that is - either by querying some o/s property in Linux or looking at some values or running some powershell on windows - that info is just not there.

I can find it simply enough in the portal of course - but is there an easy way to find this out from the VM directly?

This is where this metadata service comes in - it seems there is a site available to be queried from any VM - it's on a non routable ip and has no authentication around it - so it's easy to connect to and pull values from.

It's a rest endpoint so you'll need something like curl to get the info back - but that's easy enough to setup (if its not there by default).

So to query the details of my test VM I can simply run this command

curl -H Metadata:true

(note the Metadata:true header must be set)

when I do that - this is what I see

root@richtest:~# curl -H Metadata:true ""

Not the easiest to read in raw json but you can see the vmSize is in there - if I want to make this look a little nicer I can just pipe it to the jq utility - which then looks like this:

That's much nicer to the human eye - but actually programmatically I likely just want certain elements out of this - this can be achieved in one of 2 ways

1) Call a slightly different version of the API for specific info - for example this for the vmSize

curl -H Metadata:true ""

This directly returns Standard_B1s as the value and has none of the other json bits.

2) post process the output to get what you want - for example with jq below

And there you have it - not maybe for everyday use but maybe occasionally useful for day to day work - but I guess the primary aim of this is to aid in scripting particular tasks where you need to know something about the VM that is not 'gettable' from the VM

More info here from the excellent Microsoft docs site


Post a Comment