`curl http://169.254.169.254/latest/meta-data/` returns `401 - Unauthorized` on a new instance.
IntermediateTroubleshootingEC2, Auto Scaling and Load Balancing
Answer
Likely causes → The instance requires IMDSv2 (HttpTokens=required), and the request has no session token. Older SDKs and tools that only speak IMDSv1 break the same way.
Diagnose →
aws ec2 describe-instances --instance-ids i-0abc \
--query 'Reservations[].Instances[].MetadataOptions'
TOKEN=$(curl -sX PUT http://169.254.169.254/latest/api/token -H 'X-aws-ec2-metadata-token-ttl-seconds: 21600')
curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/instance-id
Fix →
- Update scripts to use the token flow, and upgrade SDKs and agents
- For containers on the host, set
HttpPutResponseHopLimitto 2:aws ec2 modify-instance-metadata-options --instance-id i-0abc --http-put-response-hop-limit 2
Prevent → Make IMDSv2 the default at account level (aws ec2 modify-instance-metadata-defaults), and track the MetadataNoToken metric before you enforce it.