AWS in 1 Min · Episode 65

EC2 user data and instance metadata explained

How an instance sets itself up. Watch the one-minute video, then read the details.

David Gomez1:23 video · 3 min read

"You must log in to set up a new server." Not on EC2. An instance can configure itself at launch, with nobody typing commands. Two features make that possible: user data, which tells the instance what to do, and instance metadata, which tells the instance about itself.

User data: a script for the first boot

User data is text you pass to an instance when you launch it. On Linux it is usually a shell script (starting with #!) or cloud-init directives. On Windows, the launch agent runs batch or PowerShell scripts.

What to know:

  • By default the script runs once, during the first boot. It does not run again on later restarts unless you configure that.
  • On Linux it runs as root, so there is no need for sudo.
  • It is limited to 16 KB before encoding. For anything bigger, keep the script short and have it download the rest, for example from Amazon S3.

A typical script installs packages, pulls the application, and starts the service. If something fails on a Linux instance, the output is in /var/log/cloud-init-output.log, which is the first place to look.

Never put secrets in user data

User data is not a safe place for secrets. It is not protected by authentication or encryption: any process on the instance can read it, and so can anyone with permission to view the instance's attributes. AWS's guidance is not to store sensitive data such as passwords in it. Give the instance an IAM role and let the script fetch secrets from AWS Secrets Manager or Systems Manager Parameter Store instead.

Instance metadata: facts about the instance

Instance metadata is information about the running instance: its instance ID, instance type, private and public IP addresses, Availability Zone, security groups, and more. Software on the instance reads it from the Instance Metadata Service (IMDS) at a special link-local address, 169.254.169.254, which only answers from inside the instance.

That is how a script learns where it is running without anything being hard-coded. The same service also returns the user data, and, most importantly, the temporary credentials of the IAM role attached to the instance. The AWS SDKs and CLI fetch those credentials automatically, which is why code on EC2 needs no access keys.

Require IMDSv2

Because IMDS hands out credentials, it is a target. With the original version, IMDSv1, a simple request returned data. An application flaw that lets an attacker make the server fetch a URL of their choice (server-side request forgery) could be used to read the role's credentials.

IMDSv2 makes that much harder. It is session-oriented: the caller must first make a PUT request to get a session token, then send the token with every request. Typical request-forgery flaws can't do that.

Both versions can be enabled by default, so set instances to require IMDSv2 (token usage "required"). You can do it per instance, in launch templates, or as an account-level default for new launches.

Where AI fits

GPU servers are expensive, so you want them doing useful work minutes after launch, and gone when the demand is over. User data makes that automatic. A script on a new GPU instance can download the model from S3, start the inference service, and register with the load balancer or the cluster, with no manual steps. Combine it with an image that already has the drivers and frameworks installed, and Auto Scaling can add or remove GPU capacity on its own. The script only fetches the model; keep the model, the code and the secrets outside the user data.

Quick recap

  • User data is a launch script: runs once at first boot, as root, 16 KB maximum, no secrets.
  • Instance metadata describes the instance and delivers its IAM role credentials through IMDS.
  • Require IMDSv2 so those credentials are harder to steal.
AWS in 1 Min #65 · 1:23

Need an AWS expert on your team?

I'm David, an AWS expert and fractional cloud consultant. I work alongside your team to run your cloud and infrastructure: architecture, security, cost and day-to-day operations. You get senior AWS experience when you need it, without the cost of a full-time hire.

  • Architecture reviews and a clear plan for what to fix first
  • Security hardening: IAM, networking, audits
  • Lower AWS bills without slowing your team down
  • Hands-on help with AI workloads on AWS