Roles and STS
IAM roles give temporary credentials to AWS services, applications, users and other accounts, through AWS STS.
What it is
An IAM role is an identity with permissions but no password and no access keys. Instead, someone or something assumes the role and receives temporary credentials that expire by themselves.
The service that hands out those credentials is the AWS Security Token Service (STS).
Roles exist because long-term credentials are risky. An access key works until someone deletes it, so a leaked key is dangerous for months. Temporary credentials last from 15 minutes to a few hours, then stop working on their own.
Roles are used for:
- AWS services acting on the account's behalf, such as an EC2 instance reading from S3, or a Lambda function writing to DynamoDB.
- Cross-account access, such as an admin in one account working in another.
- Federation: people who sign in elsewhere (a company directory, Google, IAM Identity Center) and get AWS access through a role.
How it works
- A user or application that already has credentials calls
sts:AssumeRolefor a role. - The role's trust policy says who may assume it. If the caller isn't listed, the request fails.
- If the caller is trusted, STS returns temporary credentials: an access key ID, a secret access key and a session token, plus an expiry time.
- The caller uses those credentials to call AWS. While they're used, the caller has the role's permissions, not its own.
Two policies on every role
- Trust policy: who can assume the role. It's a resource-based policy on the role, with a
Principal, such as an AWS service (ec2.amazonaws.com), another account, or a federated identity provider. - Permissions policy: what the role can do once assumed. Same JSON as any identity policy.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "ec2.amazonaws.com" },
"Action": "sts:AssumeRole"
}
]
}Roles for AWS services
The most common roles let a service act on the account's behalf: EC2 instance roles, Lambda execution roles, and roles CloudFormation uses to create resources.
The role is attached to the EC2 instance through an instance profile, a small container that holds one role (the console creates it automatically). AWS delivers temporary credentials to the instance through the instance metadata service and rotates them before they expire. The AWS SDK and CLI on the instance find them automatically, so the code contains no keys at all.
Service-linked roles are a special kind: created and owned by an AWS service (for example Auto Scaling or Organizations), with permissions the service defines. They can be viewed, but their permissions can't be edited.
STS operations
AssumeRole: assume a role in the same account or another account.AssumeRoleWithSAML: for users signed in through a SAML 2.0 identity provider (such as Active Directory Federation Services).AssumeRoleWithWebIdentity: for users signed in with an OpenID Connect provider (Google, Facebook, GitHub Actions). For mobile apps, AWS recommends Cognito instead.GetSessionToken: temporary credentials for an IAM user, usually to add MFA to CLI work.GetFederationToken: temporary credentials for a federated user, through a custom broker.GetCallerIdentity: shows who the current credentials belong to.
How long credentials last
AssumeRolecredentials last 1 hour by default. A session can be requested from 15 minutes up to the role's maximum session duration, which is set between 1 and 12 hours.- Role chaining (using one role's credentials to assume another role) is limited to 1 hour.
GetSessionTokenandGetFederationTokenlast 12 hours by default, up to 36 hours.
Cross-account roles and switching roles
To let users in account A work in account B:
- In account B, create a role whose trust policy names account A (or specific users or roles in it).
- Attach a permissions policy to the role with what they may do in B.
- In account A, give the users permission to call
sts:AssumeRoleon that role's ARN.
In the console, users then choose Switch role, enter account B's ID and the role name, and work in B until they switch back. With the CLI, they add a profile with role_arn and source_profile.
Remember: while a user is in the role, they give up their own permissions. The roles vs resource-based policies page explains when that matters.
Beyond the exam: external IDs for third parties
When a third-party company (a monitoring vendor, for example) is allowed to assume a role in the account, add an external ID condition to the trust policy. The vendor passes this secret value when assuming the role. It stops another customer of the same vendor from tricking the vendor into accessing the account (the "confused deputy" problem).
Real-world examples
- A photo app on EC2 saves uploads to S3 using an instance role. No keys exist anywhere in the code or on the servers.
- A company with dev and prod accounts lets its engineers sign in once in a central account and switch into a read-only role in prod, and an admin role in dev.
- GitHub Actions deploys to AWS by assuming a role through OpenID Connect, so no AWS keys are stored in GitHub.
Exam tips
- "Give an EC2 instance / Lambda function access to an AWS service" → an IAM role (instance profile for EC2, execution role for Lambda).
- "Temporary credentials" → AWS STS, usually
AssumeRole. - "Users in another account need access" → a cross-account role with a trust policy naming that account.
- "Federated users from a corporate directory (SAML)" →
AssumeRoleWithSAML, or, with less setup, IAM Identity Center. - "Who can assume this role?" → the trust policy. "What can the role do?" → the permissions policy.
Exam tip
If an answer stores access keys anywhere (in code, user data, environment variables, S3), and another answer uses a role, the role is almost always right.
Common traps
- "Attach a policy to the EC2 instance." A role is attached (via an instance profile); the policy goes on the role.
- "Roles have passwords or long-term keys." They don't. Only temporary credentials.
- "When I assume a role I keep my own permissions too." The session gets only the role's permissions.
- "Role chaining can last 12 hours." It's capped at 1 hour.
Hands-on lab: switch into a read-only role
Goal: create a role your own account can assume, then switch into it in the console and with the CLI.
Free
IAM and STS have no charge.
Before you start: sign in as your IAM admin user. Note your 12-digit account ID (top-right menu).
Create the role
Open IAM → Roles → Create role. Choose AWS account → This account. Attach the AWS managed policy ReadOnlyAccess. Name the role saa-readonly and create it.
Read its trust policy
Open the role → Trust relationships. The principal is arn:aws:iam::<your-account-id>:root, which means "any identity in this account that has permission to assume the role". Your admin user has that permission through AdministratorAccess.
Switch role in the console
Open the account menu → Switch role. Enter your account ID and saa-readonly, pick a colour, and switch. The top bar now shows the role. Try to create an S3 bucket: it's denied. Then choose Switch back.
Assume it with the CLI
aws sts assume-role \
--role-arn arn:aws:iam::<your-account-id>:role/saa-readonly \
--role-session-name lab-test --duration-seconds 900The output holds AccessKeyId, SecretAccessKey, SessionToken and an Expiration 15 minutes from now.
Expected result: inside the role you can read everything and change nothing, and the CLI credentials expire after 15 minutes.
Clean up: delete the saa-readonly role.