If you manage EC2 instances, you have probably seen the recommendation to “enforce IMDSv2”, from AWS Security Hub, your compliance team or the EC2 launch wizard itself. Most engineers flip the setting without knowing what actually changed. In this deep dive I’ll explain what the instance metadata service is, how attackers abused IMDSv1 to steal AWS credentials, exactly what IMDSv2 fixed, and how to migrate safely.

What is the Instance Metadata Service (IMDS)?

Every EC2 instance can query a special link-local address, 169.254.169.254, to learn about itself: its instance ID, region, network details, user data and, most importantly, the temporary IAM credentials of the role attached to the instance.

This is how the AWS CLI and SDKs on an instance get credentials automatically without access keys stored on disk. That is a good security design, as long as only the instance itself can reach that endpoint.

# IMDSv1-style request: a simple GET, no authentication
curl http://169.254.169.254/latest/meta-data/iam/security-credentials/
curl http://169.254.169.254/latest/meta-data/iam/security-credentials/<role-name>

The second request returns an AccessKeyId, SecretAccessKey and Token, which are valid AWS credentials for that role.

The Problem: SSRF + IMDSv1

Server-Side Request Forgery (SSRF) is a vulnerability where an attacker tricks your application into making HTTP requests on their behalf. A common example is a feature that fetches a URL supplied by the user, such as an image preview, a webhook tester or a PDF generator.

If that application runs on EC2 with IMDSv1 enabled, the attack is trivial. The attacker submits:

http://169.254.169.254/latest/meta-data/iam/security-credentials/<role-name>

The server fetches it from inside the instance, where the metadata endpoint is reachable, and returns the role’s credentials in the response. The attacker now has AWS credentials with every permission that role has, usable from anywhere until they expire.

This is not theoretical. A widely reported 2019 breach of a large US bank followed this pattern: an SSRF flaw in a misconfigured web application firewall on EC2 was used to pull IAM credentials from the metadata service, and an over-permissive role allowed the attacker to read data from S3.

What IMDSv2 Changed

IMDSv2 makes metadata access session-oriented. Before reading anything, a client must first obtain a token with an HTTP PUT request, then send that token as a header on every request:

TOKEN=$(curl -s -X 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/iam/security-credentials/

Several protections come from this design:

  • PUT is required. Most SSRF vulnerabilities only let an attacker trigger simple GET requests with no control over the method or headers, so they cannot obtain a token.
  • A custom header is required. The token must be sent in X-aws-ec2-metadata-token, which typical SSRF cannot add.
  • Hop limit of 1 by default. The token response is sent with an IP TTL (hop limit) of 1, so it cannot travel beyond the instance. This stops misconfigured proxies, and by default containers on bridged networks, from relaying it.
  • Forwarded requests are rejected. Token requests containing an X-Forwarded-For header are refused, blocking abuse through open reverse proxies and WAFs.
  • Tokens expire. Each token has a TTL of up to six hours.

IMDSv2 does not fix SSRF itself; you should still fix that in the application. But it removes the most valuable thing an SSRF attacker on EC2 usually goes after.

How to Check Which Version Your Instances Use

aws ec2 describe-instances \
  --query "Reservations[].Instances[].{ID:InstanceId,Tokens:MetadataOptions.HttpTokens}" \
  --output table

HttpTokens: required means IMDSv2 is enforced. optional means IMDSv1 is still allowed. The CloudWatch metric MetadataNoToken shows whether anything on an instance is still making IMDSv1 calls.

How to Enforce IMDSv2

On an existing instance

aws ec2 modify-instance-metadata-options \
  --instance-id i-0123456789abcdef0 \
  --http-tokens required \
  --http-endpoint enabled

On new instances

  • In the launch wizard, under Advanced details → Metadata version, choose V2 only (token required).
  • In launch templates and Terraform, set http_tokens = "required" in the metadata_options block.
  • Set the account-level default so all new instances in a region use IMDSv2: aws ec2 modify-instance-metadata-defaults --http-tokens required

Containers on EC2

If containers on the instance (Docker with bridge networking, or Kubernetes pods) legitimately need instance credentials, raise the hop limit to 2 with --http-put-response-hop-limit 2. Better still on EKS, give pods their own credentials with IRSA or EKS Pod Identity and keep the hop limit at 1, so pods cannot borrow the node’s role.

Migrating Safely

  1. Inventory instances where HttpTokens is optional.
  2. Check the MetadataNoToken metric to find instances still using IMDSv1.
  3. Update old software: current AWS CLI and SDK versions use IMDSv2 automatically. Very old SDKs, agents and custom scripts using plain curl may need updating.
  4. Enforce required in a test environment first, then roll out.
  5. Set the account default and add an SCP or AWS Config rule (ec2-imdsv2-check) so new instances cannot launch with IMDSv1.

Defence in Depth

  • Give instance roles the least privilege they need, so stolen credentials can do little.
  • Fix SSRF by validating and allow-listing outbound URLs in your application.
  • Use AWS WAF rules to block requests containing metadata addresses; see AWS WAF Explained.
  • Enable GuardDuty, which alerts when instance credentials are used from outside AWS.

Do this next

IMDSv2 is one of the cheapest, highest-impact security changes you can make on AWS. It is a single setting, it breaks almost nothing on modern software, and it shuts down the most common path from an SSRF bug to a full cloud breach. If your instances still allow IMDSv1, make enforcing v2 your next task. For more on securing AWS access, read AWS IAM Assume Role.

Watch the live demo on my YouTube channel, Madhukar Reddy (@awsandevops).

Madhukar Reddy

DevOps engineer focused on AWS, Docker, Kubernetes, cloud infrastructure, and cyber security. Shares practical cloud and DevOps content based on hands-on deployments, infrastructure troubleshooting, and real-world projects.

$ This blog is currently running on AWS EC2 using Docker-based deployment.

Leave a response