Skip to Content
Asterrr's Handbook
3 · Infrastructure security

Instance Profiles and Role Switching

How EC2 gets role credentials through an instance profile, how to change permissions, and how the CLI switches roles by profile.

Exam tasks: 3.2 (instance profiles, service roles and execution roles)

The decision: which role mechanism does this workload use, and how do you stop its credentials leaking through instance metadata?

An EC2 instance cannot assume a role directly. An instance profile is the container that passes one IAM role to the instance, so code on it can call AWS with temporary credentials and no stored access keys. Lambda needs no profile, because it has an execution role.

contained inassociatedmetadataAPI callIAM roleInstance profileEC2 instanceTemporarycredentialsS3
The instance profile carries a role to the instance. The SDK reads rotating credentials from the instance metadata service.
1 hop
IMDS hop limit that keeps containers on the host from reaching metadata. 2 lets them through.
169.254.169.254
Instance metadata address. SSRF target that IMDSv2 protects.

Core facts

ItemWhat to remember
One role per profileAn instance profile holds one role. A role can sit in many profiles. Not adjustable
Console behaviorThe console creates a profile with the same name as the role. With the CLI or API you create the role and the profile as separate steps
Attach lateraws ec2 associate-iam-instance-profile works on a running or stopped instance
Change permissionsReplace the instance profile. Removing the role from a profile can take up to an hour to apply
Force a refreshDisassociate then associate the profile, or stop and start the instance
CredentialsTemporary, delivered through instance metadata, so no keys on disk

Exam signal

"Application on EC2 needs S3 access without storing credentials": IAM role through an instance profile. Never place access keys in AMIs, user data or code.

Exam trap

Editing the role's permissions policy changes access quickly. Swapping roles inside an existing profile is the slow path. If a role must change right away on an instance, replace the association.

Giving workloads an identity

MechanismWho assumes itUsed for
Instance profileEC2 instance (container for one role)Software on the instance calling AWS APIs. Credentials rotate through IMDS
Service roleAn AWS service acting in your accountImage Builder, CodeBuild, Config, Systems Manager Automation
Service-linked roleA service, with a policy the service definesPredefined by the service. You can't edit its permissions
Lambda execution roleLambdaEverything the function's code does
ECS task roleContainers in the taskApp calls to S3, DynamoDB and so on
ECS task execution roleThe ECS agentPull from ECR, write logs, fetch secrets for the task definition
EKS Pod Identity / IRSAA Kubernetes service accountPer-pod AWS permissions instead of the node role
  • One role per workload, least privilege, no access keys on disk. Use IAM Access Analyzer policy generation to trim roles from CloudTrail activity (see Least privilege tooling).
  • A user who attaches a role to a resource needs iam:PassRole for that role. Scope it with the iam:PassedToService condition.
  • An instance holds one role at a time. Replace the profile association to change it, and running processes pick up new credentials on the next refresh.

Task role vs task execution role

If containers get AccessDenied calling S3, fix the task role. If the task fails to start because it can't pull the image or read a secret referenced in the task definition, fix the task execution role. Distractors often swap the two.

Instance metadata: require IMDSv2

  • IMDSv2 requires a session token obtained with an HTTP PUT. It blocks the classic SSRF attack, where a web app is tricked into fetching 169.254.169.254 credentials through a GET, and open reverse proxies.
  • Set HttpTokens=required on launch templates and existing instances. Make it the account-level default for new instances, and enforce with an SCP on ec2:RunInstances using the ec2:MetadataHttpTokens condition.
  • The hop limit of 1 keeps IMDS responses from reaching containers on the host (AMIs flagged for IMDSv2, such as Amazon Linux 2023, default to 2). Use 2 only when containers legitimately need IMDS. Use 1 on EKS nodes where pods should use Pod Identity instead.
  • Turn IMDS off entirely for instances that don't need it.
  • Detect stolen instance credentials: GuardDuty flags them when they're used from outside AWS or from another account. Limit them with the aws:EC2InstanceSourceVPC and aws:EC2InstanceSourcePrivateIPv4 condition keys.

Switching roles from the CLI

A developer on a dev instance needs production access without losing the dev role.

  1. Add a profile to ~/.aws/config that names the production role ARN and a source_profile.
  2. Put the access key for the source in ~/.aws/credentials.
  3. Run commands with --profile, for example aws s3 ls --profile prodaccess.

Only one profile is in force per command. While using the production profile you do not hold the dev permissions.

Scenarios

Scenario · choose 2
A penetration tester used a server-side request forgery flaw in a customer portal on EC2 to read http://169.254.169.254/latest/meta-data/iam/security-credentials/ and retrieve role credentials. The company runs 900 instances in 30 accounts and wants to stop this class of attack across the organization, including for instances launched later. Which combination of actions is best? (Choose TWO.)
Scenario
An ECS on Fargate service fails to start with the error 'unable to retrieve secret from asm' for a database password referenced in the task definition. Once running, the application must also read objects from an S3 bucket. Which change fixes the startup failure while following least privilege?

Further reading

On this page