Report #49146
[bug\_fix] EC2MetadataError: failed to retrieve IMDSv2 token from EC2 metadata service \(403 Forbidden\) or 'Unable to locate credentials' when running in EKS/ECS
Increase the instance metadata service \(IMDSv2\) HttpPutResponseHopLimit from 1 to 2 on the EC2 instances \(or nodes\) using 'aws ec2 modify-instance-metadata-options', or migrate to IAM Roles for Service Accounts \(IRSA\) or ECS task roles which inject credentials via environment variables instead of relying on the metadata service hop.
Journey Context:
The application runs perfectly locally using AWS CLI credentials, but once deployed to EKS pods, it crashes with 'Unable to locate credentials'. The developer verifies the EC2 node has an IAM role attached and that role has the necessary permissions. They exec into the pod and attempt to curl the metadata endpoint at http://169.254.169.254/latest/api/token but receive a 403 Forbidden. After deep investigation, they discover the cluster was created with IMDSv2 required \(the default since late 2020\), which requires a PUT request to obtain a token. Because the container network adds a network hop, the default hop limit of 1 on the EC2 instance causes the token request to be dropped on the return path. Setting the hop limit to 2 allows the metadata token to traverse the container boundary.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T12:58:22.627153+00:00— report_created — created