The EC2 instance metadata service (IMDS) is a link-local HTTP endpoint at 169.254.169.254 that lets software on an instance read facts about itself, such as its instance ID, Availability Zone and temporary IAM role credentials. IMDSv2 is the session-based version: every request needs a token that you obtain first with a PUT request. Requiring it blocks a class of attacks, mainly server-side request forgery (SSRF), that steal credentials through IMDSv1.
This guide covers how both versions work, how to call IMDSv2, how to enforce it, and the hop-limit and container traps that cause problems in production.
The service answers on http://169.254.169.254/latest/ and is reachable only from the instance itself. Common paths under meta-data/ include:
| Path | Returns |
|---|---|
instance-id | The instance ID |
instance-type | For example t3.micro |
placement/availability-zone | The AZ name |
local-ipv4, public-ipv4 | Private and public addresses |
iam/security-credentials/<role> | Temporary credentials for the attached role |
user-data (at /latest/user-data) | The user data supplied at launch |
AWS SDKs and the CLI use the credentials path automatically, which is how an instance with a role can call AWS APIs without stored keys.
IMDSv1 is a plain request and response. Any process that can make an HTTP GET to the address receives the data:
The danger is indirect. If your application has an SSRF flaw, an attacker can make the server fetch http://169.254.169.254/latest/meta-data/iam/security-credentials/<role> and return the temporary credentials in the response. Open proxies and misconfigured reverse proxies create the same exposure. The attacker then uses those keys from outside AWS.
IMDSv2 adds a session token. You request a token with PUT, then send it in a header on every GET:
The TTL can be from 1 to 21,600 seconds (six hours). This design defeats common SSRF paths for three reasons:
GET requests, but the token needs PUT.X-Forwarded-For header is rejected, so requests relayed through a proxy are refused.Current AWS SDKs and the CLI use IMDSv2 automatically, so most applications need no change.
Each instance has metadata options. Set HttpTokens to required to switch off IMDSv1:
The change takes effect without a reboot. For new instances, set the same options in the launch template under MetadataOptions. You can also set a Region-level default so new launches require tokens:
Recent Amazon Linux AMIs (Amazon Linux 2023) default to IMDSv2 only. To find laggards, watch the CloudWatch metric MetadataNoToken, which counts IMDSv1 calls per instance. When it reads zero for a while, enforce required safely.
You can also deny the launch of instances that allow IMDSv1 with an IAM or service control policy using the ec2:MetadataHttpTokens condition key.
The response hop limit sets how many network hops the token response may travel. The default is 1, which is exactly enough for a process on the instance itself. A container on a bridge network adds one hop, so a container that needs the metadata service gets no answer with the default.
| Situation | Suggested hop limit |
|---|---|
| Processes directly on the host | 1 |
| Containers using bridge networking that must reach IMDS | 2 |
| Containers that should not reach IMDS | 1 (keep it blocked) |
Raising it to 2 lets containers read the instance role's credentials, which widens the blast radius of a compromised container. On Amazon ECS and EKS, prefer task roles or IAM roles for service accounts so containers do not need the instance role at all.
401 Unauthorized means the token was missing, expired or invalid for IMDSv2. Request a new token.HttpTokens is required. Upgrade the SDK.curl that returns nothing at all may be blocked by a local firewall rule or by HttpEndpoint set to disabled.--http-endpoint disabled.On newer AMIs such as Amazon Linux 2023, yes. On older AMIs and many accounts, instances accept both versions until you set HttpTokens to required.
No. It blocks common SSRF and proxy paths, but malware running directly on the instance can still request a token. Use least-privilege roles and patch application flaws too.
Usually not. Current AWS SDKs request tokens automatically. Only custom scripts and very old SDKs need updating.
Yes, an IPv6 endpoint exists but is off by default; you enable it in the instance metadata options.
The instance metadata service gives an EC2 instance information about itself and its role credentials at 169.254.169.254. IMDSv2 protects it with a session token obtained through a PUT request. Check the MetadataNoToken metric, update old SDKs, set HttpTokens to required, keep the hop limit at 1 unless containers truly need more, and use task or service-account roles for containers.