Stop 05 · Compute With Amazon EC2Topics · 8/9

EC2 Instance Metadata & IMDSv2

Part of the AWS Roadmap

In short

Every EC2 instance can ask "who am I?" at a special local address and get its ID, Region and even temporary role credentials. That power is also why attackers love it, and why IMDSv2 matters.

The instance metadata service lets software on an instance read details such as its instance ID, Region and temporary role credentials from a special local address. IMDSv2 requires a session token, which protects against some server-side request forgery attacks.

The jaadu sits on a hotel bed holding a room phone to one ear while a small paper token slides out from a slot beside the phone.

Think of it like this

Hotel room service phone

From inside your room you dial 9 and the front desk tells you your room number and checkout time. With the old system anyone who could trick you into dialling got answers. Now the desk first asks for a fresh PIN that only someone inside the room can request. That PIN is the IMDSv2 token.

Watch it happen

Reading metadata with IMDSv2

Your app
IMDS 169.254.169.254

PUT /latest/api/token

1/4The app asks for a session token with a TTL header, for example six hours.

The big ideas

What you need to know

A link-local address

169.254.169.254 is reachable only from inside the instance. It never leaves the host.

Role credentials live here

Temporary credentials for the instance role come from IMDS. That's how SDKs authenticate without keys.

Require IMDSv2

Set HttpTokens=required so the old token-less IMDSv1 is turned off. Amazon Linux 2023 AMIs do this by default.

Hop limit

The token's IP hop limit controls whether containers on the host can reach IMDS. Default is 2 on many AMIs, 1 is stricter.

See it in code

Walk through the code

bash
1TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" \2  -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")3 4curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \5  http://169.254.169.254/latest/meta-data/instance-id6curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \7  http://169.254.169.254/latest/meta-data/placement/region8 9# From your laptop: enforce IMDSv2 on an existing instance10aws ec2 modify-instance-metadata-options \11  --instance-id i-0123456789abcdef0 \12  --http-tokens required --http-endpoint enabled

1/3A PUT request returns a session token valid for 21,600 seconds (six hours).

Reading metadata the IMDSv2 way from inside the instance, then switching off IMDSv1 on an older instance.

Save yourself the pain

Common mistakes

Oops

Leaving IMDSv1 enabled on internet-facing apps.

Do this instead

Require IMDSv2 on every instance, and set it as the default for new launches in your account settings.

Oops

Putting passwords in user data because 'it's only reachable from the instance'.

Do this instead

Any process on the instance can read IMDS. Store secrets in Secrets Manager and grant the role access.

Your mission

Explore your instance from inside

~20 min

Quick check

What makes IMDSv2 safer than IMDSv1?

Go deeper

  1. 1From an instance, get a token and read its instance ID with curl using IMDSv2.
  2. 2Check that new instances you launch require IMDSv2 in their metadata options.
  3. 3Read how the SDKs pick up role credentials from instance metadata automatically.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge