Cloud Computing

Azure Functions MSI token requests intermittently 401 after cold scale‑out when accessing Key Vault

Environment

Environment

Consumption plan Azure Functions (v4, Node.js 18) using Managed Identity to call Azure Key Vault; observed during autoscale from 1→10 instances in West Europe; function app uses DefaultAzureCredential from @azure/identity.

Symptoms

Symptoms

During sudden scale‑out some function instances receive 401/unauthorized from Key Vault when fetching secrets; retries succeed after ~30–90s. A subset of requests from new instances fail consistently until token refresh.

Observations

Observations

Failed requests show HTTP 401 with WWW-Authenticate header referencing 'Bearer error="invalid_token"'. Function logs show token acquisition attempt succeeded locally but Key Vault rejects the presented token. Failures concentrated on instances created in the last 20s of the scale event.

Already attempted

Already attempted

Increased retry count/backoff in code; added 5s startup sleep before first Key Vault call; replaced DefaultAzureCredential with ManagedIdentityCredential directly; no change. Deployed same app to different region — same behavior during scale events.

Question

Question

What causes intermittent Managed Identity token rejection right after scale‑out, and how can we reliably avoid 401s on freshly started Function instances without adding long startup sleeps?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 4w ago• Trouble ID

Most likely causes

IMDS/Managed Identity endpoint not fully ready on new instance leading to partially issued tokens that Key Vault rejects 78%
Short clock skew on fresh instances causing tokens to appear not yet valid to Key Vault (nbf/iat mismatch) 45%
Credential library/library cache race: simultaneous token requests reuse an incomplete cache entry 40%

Summary

Timing issues between instance provisioning and managed identity token propagation or metadata service readiness are likely. Newly created instances may receive tokens not yet recognized by Key Vault due to short-lived credential caching, clock skew, or a regional identity propagation delay. Other possibilities include MSI endpoint not warming, transient network path to IMDS, or a bug in the credential library reusing stale cached keys during parallel startup.

Suggested Fixes

Diagnostic step 1
Enable structured logs for @azure/identity and record token iat/nbf/exp and IMDS responses on startup to correlate failures.
Diagnostic step 2
Add a short exponential-backoff retry on 401 specifically when immediately after startup (e.g., retry 3 times with 200ms→500ms→1s) rather than a fixed long sleep.
Diagnostic step 3
Ensure container/time sync on Functions: log local clock vs NTP and confirm <5s skew; if skewged, configure time sync or restart host.

Continue troubleshooting

Matched from the title, description and tags