Report #7450
[bug\_fix] AWS IMDSv2 session token expiry causing credential refresh failures in containers on EC2
Increase the IMDSv2 hop limit to 2 on the EC2 instance using 'aws ec2 modify-instance-metadata-options --http-put-response-hop-limit 2 --instance-id \[ID\]'. This allows the PUT request for the IMDSv2 session token to traverse the Docker bridge NAT \(1 hop\) and reach the instance metadata service. Alternatively, configure the SDK to use IMDSv1 \(not recommended for security\), or run the container with host networking to avoid the extra hop.
Journey Context:
Developer deploys a Python application in a Docker container on an EC2 instance. The instance has an IAM Instance Profile attached. The application uses boto3 to upload files to S3. Initially, everything works. After 6 hours \(the default IMDSv2 session token TTL\), the application starts throwing 'ExpiredToken: The security token included in the request is expired'. The developer enters the running container and runs 'curl http://169.254.169.254/latest/meta-data/iam/security-credentials/\[role-name\]' and receives '401 Unauthorized'. They notice that IMDSv2 requires a session token. They try to fetch a token with 'curl -X PUT http://169.254.169.254/latest/api/token -H X-aws-ec2-metadata-token-ttl-seconds:21600' from inside the container, but the request times out or returns no response. From the host, the same command works instantly. The developer realizes that IMDSv2 uses a PUT request to obtain a session token, and the default hop limit for this PUT request is 1. When the request passes through the Docker bridge \(docker0\), the NAT decrements the TTL to 0 and drops the packet. The container therefore cannot refresh the session token after the initial 6-hour window. The developer runs 'aws ec2 modify-instance-metadata-options --instance-id \[ID\] --http-put-response-hop-limit 2' on the host. The container can now successfully PUT to the IMDS endpoint, retrieve a fresh session token, and refresh credentials indefinitely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T02:44:56.466559+00:00— report_created — created