Home / Azure

AzureCheat sheetIntermediate

Azure Instance Metadata Service (IMDS) cheat sheet: VM metadata, managed identity tokens and failing calls

Query IMDS from scripts for VM size, zone, tags and network details, get managed identity tokens, and fix 400, 429, proxy and routing failures on 169.254.169.254.

Deepak Bhardwaj7 min read

Applies toAzure VMs and VM Scale Sets (Windows and Linux), IMDS api-version 2021-02-01 and later, managed identity token endpoint api-version 2018-02-01

When you need this

A script on a VM needs to know where it's running (region, zone, size, tags), or an app's managed identity call has started failing with Identity not found, timeouts or 429s. Both go through the Instance Metadata Service (IMDS) at 169.254.169.254. IMDS answers only from inside the VM, needs no credentials, and has a few strict rules. Most failures come from breaking one of those rules.

The rules every call must follow

Rule Detail
Header Metadata: true on every request
No proxy IMDS doesn't support proxies. Bypass the proxy explicitly
No X-Forwarded-For Requests carrying this header are rejected
api-version Required. Call /metadata/versions to list the versions the VM supports
GET only Other verbs return 405
Primary NIC Traffic must leave through the primary NIC and primary IP
Rate limit About 5 requests per second per VM. The identity endpoint allows more, but cache tokens anyway
Plain HTTP No HTTPS, and the address isn't routable from outside the VM

Instance metadata: Linux (curl)

IMDS="http://169.254.169.254/metadata"
H="Metadata:true"
V="api-version=2021-02-01"

# Which API versions this VM supports
curl -s -H $H --noproxy "*" "$IMDS/versions"

# Everything, as JSON
curl -s -H $H --noproxy "*" "$IMDS/instance?$V" | python3 -m json.tool

# Single values: leaf nodes need format=text
curl -s -H $H --noproxy "*" "$IMDS/instance/compute/vmSize?$V&format=text"
curl -s -H $H --noproxy "*" "$IMDS/instance/compute/location?$V&format=text"
curl -s -H $H --noproxy "*" "$IMDS/instance/compute/zone?$V&format=text"
curl -s -H $H --noproxy "*" "$IMDS/instance/compute/vmId?$V&format=text"
curl -s -H $H --noproxy "*" "$IMDS/instance/compute/resourceId?$V&format=text"

# Tags as JSON (easier to parse than the semicolon-separated 'tags' string)
curl -s -H $H --noproxy "*" "$IMDS/instance/compute/tagsList?$V"

# Private and public IP of the primary NIC
curl -s -H $H --noproxy "*" \
  "$IMDS/instance/network/interface/0/ipv4/ipAddress/0/privateIpAddress?$V&format=text"
curl -s -H $H --noproxy "*" \
  "$IMDS/instance/network/interface/0/ipv4/ipAddress/0/publicIpAddress?$V&format=text"

Tip

The public IP field is empty when the address is on a load balancer or NAT Gateway rather than on the NIC. To see a Standard Load Balancer's frontend IPs, query $IMDS/loadbalancer?api-version=2020-10-01.

Instance metadata: Windows (PowerShell)

$h = @{ Metadata = "true" }
$base = "http://169.254.169.254/metadata"

# PowerShell 7+: -NoProxy skips any configured proxy
Invoke-RestMethod -Headers $h -NoProxy -Uri "$base/instance?api-version=2021-02-01" |
  ConvertTo-Json -Depth 10

Invoke-RestMethod -Headers $h -NoProxy `
  -Uri "$base/instance/compute/vmSize?api-version=2021-02-01&format=text"

Windows PowerShell 5.1 has no -NoProxy parameter. Make sure the system proxy settings bypass 169.254.169.254, or run the call from PowerShell 7.

Other endpoints worth knowing

Path Use
/metadata/scheduledevents?api-version=2020-07-01 Upcoming reboots, redeploys and evictions (see Scheduled Events)
/metadata/attested/document?api-version=2020-09-01 Signed document that proves the VM's identity to a third party. Add &nonce=<10 digits>
/metadata/loadbalancer?api-version=2020-10-01 Standard LB frontend and outbound IPs
/metadata/identity/oauth2/token Managed identity access tokens (next section)

Managed identity tokens

# System-assigned identity, token for Azure Resource Manager
curl -s -H Metadata:true --noproxy "*" \
  "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://management.azure.com/"

# A specific user-assigned identity: pass exactly one of these
#   &client_id=<client id>
#   &object_id=<principal id>
#   &msi_res_id=<full resource ID of the identity>
curl -s -H Metadata:true --noproxy "*" \
  "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://vault.azure.net&client_id=00000000-0000-0000-0000-000000000000"

Common resource values: https://management.azure.com/ (ARM), https://vault.azure.net (Key Vault), https://storage.azure.com/ (Storage), https://database.windows.net/ (Azure SQL).

To decode the token and check which identity and audience you got, read the oid, aud and exp claims:

TOKEN=$(curl -s -H Metadata:true --noproxy "*" \
  "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://management.azure.com/" \
  | python3 -c "import sys,json; print(json.load(sys.stdin)['access_token'])")
echo "$TOKEN" | cut -d. -f2 | tr '_-' '/+' | base64 -d 2>/dev/null | python3 -m json.tool | grep -E '"(aud|oid|appid|exp)"'

Warning

Any process on the VM can request a token for any identity assigned to the VM. Don't attach powerful identities to VMs that run untrusted code, and treat a token you print while debugging as a credential.

Symptom → cause → fix

Symptom Cause Fix
400, "Required metadata header not specified" Missing Metadata: true header (or a proxy stripped it) Add the header and bypass the proxy
400 on a leaf value such as vmSize JSON format requested for a leaf Add &format=text
400, identity not found / 401 unauthorized_client No managed identity on the VM, or the wrong client_id az vm identity show -g <rg> -n <vm> and compare the IDs
400, multiple user-assigned identities Several user-assigned identities and none specified Pass client_id, object_id or msi_res_id
400 invalid_resource (AADSTS50001) Wrong or misspelled resource URI Use the exact audience the target service expects
403 from the target service, token returned fine Identity lacks RBAC on the target Assign the role, then get a new token
404 Path doesn't exist for this api-version, or a transient error Check /metadata/versions. Retry with backoff
405 Non-GET verb Use GET
410 IMDS is updating Retry for up to 70 seconds
429 Over the rate limit, usually a token fetched on every request Cache tokens until near expires_on. Use the Azure Identity SDK, which caches for you
500 IMDS or Entra ID failure Retry with exponential backoff (at least 1 s)
Timeout / connection refused Proxy in the path, guest firewall, or a missing route See the next section

Retry 404, 410, 429 and 5xx with exponential backoff. Don't retry other 4xx errors: they come from the request itself and won't fix themselves.

When 169.254.169.254 doesn't answer at all

Linux

ip route get 169.254.169.254      # must leave via the primary NIC (usually eth0)
env | grep -i proxy               # check no_proxy covers 169.254.169.254
sudo iptables -S | grep 169.254   # guest firewall rules blocking it
curl -sv -m 5 -H Metadata:true --noproxy "*" "http://169.254.169.254/metadata/versions"

Windows

route print 169.254.169.254       # a route must exist, via the primary NIC
Get-NetIPConfiguration | Select InterfaceAlias, IPv4Address, IPv4DefaultGateway
netsh winhttp show proxy          # system proxy without a 169.254.169.254 bypass breaks some agents

Compare the interface's MAC address with the primary NIC in Azure (az vm nic list -g <rg> --vm-name <vm>). On failover cluster nodes and VMs with several NICs, the route can end up on the wrong interface. Microsoft's fix is a persistent route through the primary NIC's address. Check the current IMDS troubleshooting docs for the exact command for your setup before you change routes on a cluster node.

Caution

Blocking IMDS in the guest firewall also breaks the VM agent, extensions, Azure Monitor Agent, backup and Entra ID login. If you need to restrict it, allow the agent and service accounts explicitly rather than blocking the address for everyone.

Prevent it next time

  • Use the Azure Identity SDK (DefaultAzureCredential or ManagedIdentityCredential) instead of hand-written token calls. It caches tokens, retries correctly and picks the right endpoint on App Service, Functions and AKS as well.
  • If you call IMDS directly, pin an api-version, always send --noproxy "*" or -NoProxy, and cache the results.
  • Put 169.254.169.254 in no_proxy and the WinHTTP bypass list in your base image.
  • Set client_id explicitly whenever a VM has user-assigned identities, so adding another identity later doesn't break the app.