Instance metadata and user data
How an EC2 instance learns about itself, gets role credentials through IMDSv2, and configures itself at first boot with user data and cloud-init.
Launching and connecting to instancesWorkload identity for EC2, Lambda and CI
Instance metadata and user data
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- The instance metadata service at 169.254.169.254 tells software on an instance about itself and hands out the instance role's temporary credentials.
- IMDSv2 requires a PUT to get a session token before any read, which defeats most request-forgery attacks that only control a GET. Require it.
- The response hop limit stops the token travelling further than allowed. Containers on bridge networking may need a limit of 2.
- User data is run by cloud-init as root, by default only on the first boot, and is limited to 16 KB.
- Never put secrets in user data: it is readable from the instance and through the EC2 API.
- Read /var/log/cloud-init-output.log when a boot script does not do what you expected.
Who am I?
A freshly booted server from a shared image knows nothing about itself. Which Region is it in? What is its address? What is it allowed to do? Baking the answers into the image would need a different image per server.
So EC2 gives every instance a private question line. The instance metadata service (IMDS) answers at 169.254.169.254 (or fd00:ec2::254 over IPv6), a link-local address reachable only from the instance itself. It returns the instance ID, type, zone, network details, tags if you enable that, and the instance profile role’s temporary credentials. That last item is how the SDKs on an instance authenticate, and why it needs protecting.
The attack, and IMDSv2
The first version answered any plain GET. Now imagine an application that fetches URLs on a user’s behalf, such as an image importer. Give it http://169.254.169.254/latest/meta-data/iam/security-credentials/... and it happily returns your credentials. This is server-side request forgery, and it has been used in real breaches.
IMDSv2 adds a handshake. First a PUT asking for a session token, then reads carrying it:
TOKEN=$(curl -sX PUT http://169.254.169.254/latest/api/token \
-H "X-aws-ec2-metadata-token-ttl-seconds: 300")
curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \
http://169.254.169.254/latest/meta-data/placement/availability-zone
Think of a receptionist who will answer only after you have signed in and been given a badge. A forged request that can only make the server GET a URL never gets the badge. A token lives from 1 second to 6 hours.
The token response also has a hop limit. At 1, the reply cannot cross another network hop, which keeps it on the host. Containers behind a bridge network add a hop, so they may need 2.
Require IMDSv2. Per instance it is --metadata-options HttpTokens=required. Some newer images, and account settings, make it the default, but don’t assume. Each Region has account-level defaults, plus an enforcement setting that makes launches allowing IMDSv1 fail. Check that your software uses a current SDK first.
IMDSv2 narrows the attack, it doesn’t remove the consequences. Code running on the instance can still read the credentials, so keep the role small.
User data: the first-boot instructions
User data is a blob you attach at launch. On Linux images, cloud-init reads it and, if it starts with #!, runs it as a script; it also accepts declarative #cloud-config documents.
#!/bin/bash
set -euxo pipefail
dnf install -y nginx
# fetch the zone with a token, as above
echo "hello from $AZ" > /usr/share/nginx/html/index.html
systemctl enable --now nginx
Rules to remember:
- It runs as root, without a terminal. No
sudo, and no prompts (-y). - By default it runs once, on first boot.
- At most 16 KB before encoding. Fetch larger things from S3.
- The API carries it base64-encoded. Encoding is not encryption:
$ aws ec2 describe-instance-attribute --instance-id i-... --attribute userData \
--query UserData.Value --output text | base64 -d | head -3
#!/bin/bash
set -euxo pipefail
dnf install -y nginx
Anyone with that permission, or any process on the instance, can read it. No secrets. Fetch them from Secrets Manager or Parameter Store.
A good boot script is also safe to read and boring to run. Pin what you install where it matters, and finish by checking that the service answers, so a broken instance fails loudly instead of joining the fleet half-configured. If user data must run again, write each step so that repeating it does no harm.
When boot goes wrong, read /var/log/cloud-init-output.log. set -euxo pipefail makes the script stop at the first failure and echo each command into that log.
Script or image?
Everything user data does at boot adds time and a chance of failure, such as a package mirror being down while you are trying to scale out. Put the slow, stable parts in an AMI and keep user data for what differs per environment. For ongoing configuration of running servers, use Systems Manager instead.
About the video
The embed is a clip (2:06 to 19:03) of cloudonaut’s “Transition to IMDSv2 on EC2”, August 2021. The chapters in this range explain the metadata service, compare IMDSv1 with IMDSv2 and run a reverse proxy demo, and the description mentions a pitfall with Docker. It predates account-level defaults and enforcement, and doesn’t cover user data; Rahul Wagh’s video in deeper does. I judged both from chapters, titles and description, since transcripts couldn’t be fetched.
What was run: the boot script passed bash -n, and the base64 round trip ran against moto, a local emulator. Nothing booted, so the script never executed and no metadata service was queried. Metadata behaviour and limits are from AWS documentation, checked in November 2026.
Where this fits
The instance needs somewhere durable to keep data: EBS volumes and snapshots.
Check yourself
A web app fetches any URL a user submits. Why is that dangerous on an instance that still allows IMDSv1?
The user can submit the metadata address and have the app read and return the role's credentials. IMDSv2 needs a PUT with a custom header first, which such a fetch normally cannot do.
You edit the user data of a stopped instance and start it. Nothing changes. Why?
By default user data scripts run only on the first boot. Replace the instance, or configure cloud-init to run the script on every boot.
An SDK inside a Docker container on an instance cannot find credentials, but works on the host. What is a likely cause?
The token response's hop limit is 1, and the extra network hop into the container drops it. Raise the limit to 2, or give the container its own credentials.
Your script needs a database password at boot. Where should it come from?
From Secrets Manager or Parameter Store, fetched at boot using the instance role. The user data should hold only the name of the secret.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.