AWS in 1 Min · Episode 27

EC2 instance profiles explained

How EC2 gets its role. Watch the one-minute video, then read the details.

David Gomez1:19 video · 3 min read

"My server needs access keys in a config file." Please don't. A key in a file gets copied wherever the file goes: into backups, machine images, container layers and, sooner or later, a code repository. One leak exposes everything that key can do, and the key keeps working until someone deletes it. EC2 has a built-in alternative that needs no stored secret at all: give the instance a role through an instance profile.

What an instance profile is

An instance profile is a container for an IAM role that EC2 can attach to an instance. The role holds the permissions; the profile is what passes the role to the server (EC2 basics if you need them).

The rules are simple:

  • An instance profile can contain only one role. That limit can't be raised.
  • You can attach one role to an instance at a time, but the same role can be used by many instances.
  • You can attach, replace or detach the profile on a running instance.

If you create a role for EC2 in the console, AWS creates an instance profile for you with the same name, which is why many people never notice it exists. With the CLI, API or infrastructure as code, the role and the instance profile are separate resources, possibly with different names, and you have to create both and add the role to the profile. Forgetting that step is the classic cause of "my role exists, but I can't select it for the instance".

How the credentials reach your code

Inside the instance, the Instance Metadata Service (IMDS) hands out temporary credentials for the role: an access key ID, a secret access key and a session token, with an expiry.

You don't have to fetch them yourself. The AWS SDKs and the CLI look for them automatically, so code running on the instance calls AWS with no credentials in its configuration.

AWS rotates those credentials automatically and makes new ones available before the old ones expire. Nothing to store, nothing to renew, nothing to forget.

Two things to get right

  • Least privilege. Every process on the instance can use the role, so grant only what the workload needs: this bucket, this queue, not *.
  • Require IMDSv2. The newer, session-oriented version of the metadata service needs a token obtained with a separate request first, which blocks common tricks where an attacker gets a vulnerable web app to fetch the metadata URL on their behalf. You can require IMDSv2 per instance and make it the default for new instances in your account.

To change what an instance can do, edit the role's policies or replace the instance profile. AWS advises against removing the role from the profile, because that change can take up to an hour to take effect.

Where AI fits

Machine-learning work on EC2 is a textbook case. A GPU instance training or fine-tuning a model has to read large datasets from S3 and write checkpoints back. With an instance profile, the role grants read access to the dataset bucket and write access to the output prefix, and the training script just uses the SDK.

That matters more than it sounds. Notebooks and training scripts get shared, exported and pasted into chat assistants all the time; if they contain no keys, sharing them leaks nothing. The same applies to an AI coding agent working on the instance: it inherits the role's limited, temporary permissions instead of finding a long-lived key in a config file.

Quick recap

  • No keys on servers. Give the instance a role instead.
  • An instance profile carries exactly one role to an EC2 instance.
  • The credentials are temporary and rotate automatically; keep the role least privilege and require IMDSv2.
AWS in 1 Min #27 · 1:19

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