Report #44070
[gotcha] EC2 IMDSv2 returns 401 Unauthorized or connection timeouts from inside Docker containers despite the instance having a valid IAM profile
Increase the IMDSv2 HttpPutResponseHopLimit to 2 \(minimum\) on the EC2 instance or launch template using \`aws ec2 modify-instance-metadata-options --http-put-response-hop-limit 2\`
Journey Context:
IMDSv2 requires a PUT request to fetch a token, and the HTTP response containing that token has a hop count \(TTL\) defaulting to 1. When a container makes this request, the packet crosses the Docker bridge \(veth pair\), decrementing the TTL to 0. The packet is dropped at the bridge, so the container never receives the token, manifesting as 401 or timeouts. Increasing to 2 allows the one hop to the container while maintaining security \(packets cannot escape the host to external networks\). Developers often waste hours checking IAM policies or security groups, not realizing this is a Layer 3 TTL issue specific to container networking.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T04:26:34.360160+00:00— report_created — created