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 http://169.254.169.254/metadata/instance?api-version=2017-12-01


(note the Metadata:true header must be set)


when I do that - this is what I see


root@richtest:~# curl -H Metadata:true "http://169.254.169.254/metadata/instance?api-version=2017-12-01"
{"compute":{"location":"westeurope","name":"richtest","offer":"UbuntuServer","osType":"Linux","placementGroupId":"","platformFaultDomain":"0","platformUpdateDomain":"0","publisher":"Canonical","resourceGroupName":"rg-tf","sku":"18.04-LTS","subscriptionId":"60162758-010a-4bec-89f5-6c903cfe769a","tags":"","version":"18.04.201805220","vmId":"e04f6ace-d167-4519-98a5-1d8fb12d352b","vmScaleSetName":"","vmSize":"Standard_B1s","zone":""},"network":{"interface":[{"ipv4":{"ipAddress":[{"privateIpAddress":"10.0.4.4","publicIpAddress":"40.114.254.219"}],"subnet":[{"address":"10.0.4.0","prefix":"24"}]},"ipv6":{"ipAddress":[]},"macAddress":"000D3A2E74E1"}]}}


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 "http://169.254.169.254/metadata/instance/compute/vmSize?api-version=2017-08-01&format=text"


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


https://docs.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service



Comments

Post a Comment