Azure Functions MSI token requests intermittently 401 after cold scale‑out when accessing Key Vault
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
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
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
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
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?
Verified community troubleshooting
No answers yet..
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags