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.
Core facts
| Item | What to remember |
|---|---|
| One role per profile | An instance profile holds one role. A role can sit in many profiles. Not adjustable |
| Console behavior | The 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 later | aws ec2 associate-iam-instance-profile works on a running or stopped instance |
| Change permissions | Replace the instance profile. Removing the role from a profile can take up to an hour to apply |
| Force a refresh | Disassociate then associate the profile, or stop and start the instance |
| Credentials | Temporary, 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
| Mechanism | Who assumes it | Used for |
|---|---|---|
| Instance profile | EC2 instance (container for one role) | Software on the instance calling AWS APIs. Credentials rotate through IMDS |
| Service role | An AWS service acting in your account | Image Builder, CodeBuild, Config, Systems Manager Automation |
| Service-linked role | A service, with a policy the service defines | Predefined by the service. You can't edit its permissions |
| Lambda execution role | Lambda | Everything the function's code does |
| ECS task role | Containers in the task | App calls to S3, DynamoDB and so on |
| ECS task execution role | The ECS agent | Pull from ECR, write logs, fetch secrets for the task definition |
| EKS Pod Identity / IRSA | A Kubernetes service account | Per-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:PassRolefor that role. Scope it with theiam:PassedToServicecondition. - 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 fetching169.254.169.254credentials through a GET, and open reverse proxies. - Set
HttpTokens=requiredon launch templates and existing instances. Make it the account-level default for new instances, and enforce with an SCP onec2:RunInstancesusing theec2:MetadataHttpTokenscondition. - 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:EC2InstanceSourceVPCandaws:EC2InstanceSourcePrivateIPv4condition keys.
Switching roles from the CLI
A developer on a dev instance needs production access without losing the dev role.
- Add a profile to
~/.aws/configthat names the production role ARN and asource_profile. - Put the access key for the source in
~/.aws/credentials. - Run commands with
--profile, for exampleaws 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
IMDSv2 requires a PUT to get a session token, which a typical SSRF can't do, so requiring it fixes the flaw. The SCP stops anyone launching instances that allow IMDSv1. Metadata traffic never leaves the host, so a NACL can't block it. Instance role credentials already rotate automatically, and replacing roles with access keys makes things worse.
Secrets referenced in the task definition are fetched by the ECS agent using the task execution role before the container starts, so that role needs read access to the one secret and its key. The task role is what the application code uses once running, for example for S3. A broad managed policy on both roles breaks least privilege, and plain-text environment variables expose the password.
Related
-
IDS, IPS and host-based security covers source/destination checks and host controls.
-
Attack surface reduction covers administrative access without SSH.
Further reading
Vulnerability scanning and patching
Amazon Inspector for EC2, ECR, Lambda and code repositories, GuardDuty Runtime Monitoring, Systems Manager Patch Manager and State Manager, and security scanning inside CI/CD pipelines.
Attack Surface Reduction
Layered controls that shrink what an attacker can reach, from security group chaining to CloudFront and Shield Advanced.