Query Azure VM information from within a VM running in Azure
Want to know the size of your Azure VM but don’t have access to Azure? Well if you have access to the VM itself (RDP/SSH or remote script) then the Azure Instance Metadata Service (AIMS) is the solution, it can be used to query a running VM from the VM itself. This is very useful if you don’t have access to Azure itself via the Portal, Cli or PowerShell.
Off the top of my head I can think of a few of scenarios where this could be very useful:
- If you have different teams managing different elements of your IT infrastructure and don’t want to grant Azure access (even read) to your team that looks after the OS of your VMs.
- You work with a 3rd party who manages the Azure elements but you still have access to the VMs themselves.
- You have a system management tool that can run remote scripts on your VMs but it lacks any sort of Azure integration.
While I’ve called out VM size information as something your can query there is a lot more information available including planned maintenance schedule information. More information AIMS is available here.
To query metadata about your VM you’re going to need to make a REST API call to the AIMS REST endpoint (169.254.169.254). This endpoint is only available from the VM running in Azure itself.
So lets say you want to know the size of the VM you’ve deployed, is it a D series or maybe an A?
Query from a Windows VM
Logon to Windows, load PowerShell and run the following:
$MyResponse = Invoke-RestMethod -Headers @{"Metadata"="true"} -URI 'http://169.254.169.254/metadata/instance?api-version=2017-08-01' -Method get
Write-Host $MyResponse.Compute.vmSize
Query from a Linux VM
SSH to your host and run the following:
curl -s -H Metadata:true "http://169.254.169.254/metadata/instance?api-version=2017-08-01" | jq -r '.compute.vmSize'
Hopefully you’ll find this useful!