Report #9097
[gotcha] IMDSv2 token expiration causing 401 Unauthorized on long-running AWS SDK processes
Ensure the AWS SDK or CLI is configured to automatically refresh IMDSv2 session tokens, or increase the token TTL \(default 6 hours, max 6 hours\) and implement retry logic with exponential backoff for 401 errors when fetching credentials.
Journey Context:
IMDSv2 \(Instance Metadata Service v2\) requires a PUT request to fetch a token with a TTL \(time-to-live\) before subsequent GET requests for credentials. The default and maximum TTL is 6 hours \(21600 seconds\). Long-running applications or connection pools may hold a cached token beyond its TTL, resulting in 401 Unauthorized errors when attempting to refresh credentials or access the instance metadata. The AWS SDKs \(Java v2, Python Boto3, Go v2\) generally handle token refresh automatically if using the default credential chain, but custom HTTP clients or manual metadata access often omit the refresh logic. The fix is to either rely on the SDK's built-in IMDSv2 support \(which refreshes tokens pro-actively\) or explicitly handle the 401 by re-PUTting for a new token before retrying the original request.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T07:16:39.352233+00:00— report_created — created