Sub topic 11
Assuming a Role with AWS STS
This chapter assumes you know what an IAM role is, how a trust policy names who may assume it, and how to read a policy document. It covers how AWS Security Token Service (STS) turns an allowed AssumeRole call into temporary credentials, how to use those credentials, and the errors you are most likely to hit.
What STS does
AWS STS is a global service that issues short-lived credentials. When a principal calls sts:AssumeRole, STS checks the request, then returns three values:
- An access key ID, which starts with
ASIAinstead of theAKIAprefix of long-lived user keys. - A secret access key.
- A session token, which must be sent with every request signed by those keys.
The credentials also carry an expiration time. After it passes, they stop working and cannot be renewed. You call AssumeRole again to get new ones.
The assumed role is not a copy of your original identity. While you use the new credentials, you act only as the role. The permissions of the identity that called AssumeRole do not apply to those requests.
Two policies must both allow the call
Assuming a role needs agreement from both sides:
- The role's trust policy must name the caller as a trusted principal and allow
sts:AssumeRole. - The caller's own permissions must allow
sts:AssumeRoleon the role's ARN.
For a caller in the same account, either side can be enough on its own, depending on how the trust policy names the principal. For a caller in a different account, both are required. The cross-account chapter covers that case in detail.
The trust policy below lets principals in account 111122223333 assume the role only when they signed in with MFA.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:root" },
"Action": "sts:AssumeRole",
"Condition": {
"Bool": { "aws:MultiFactorAuthPresent": "true" }
}
}
]
}The :root in the principal ARN means "the account", not the root user. Naming the account hands the decision to that account's administrators, who then grant sts:AssumeRole to specific users or roles.
A matching identity-based policy on the caller looks like this:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::444455556666:role/ReadOnlyAuditor"
}
]
}The assume-role flow
Assuming a role with the AWS CLI
The command needs a role ARN and a session name. The session name is a label you choose. It appears in CloudTrail logs, so make it identify the person or job.
aws sts assume-role \
--role-arn arn:aws:iam::444455556666:role/ReadOnlyAuditor \
--role-session-name audit-session
# The response contains a Credentials object. Export its values:
export AWS_ACCESS_KEY_ID=<AccessKeyId from the response>
export AWS_SECRET_ACCESS_KEY=<SecretAccessKey from the response>
export AWS_SESSION_TOKEN=<SessionToken from the response>
aws sts get-caller-identityThe final command should print an ARN of the form arn:aws:sts::444455556666:assumed-role/ReadOnlyAuditor/audit-session. That ARN is how you confirm the switch worked. If it still shows your user ARN, the environment variables were not exported in the shell you are using.
Using a profile instead of exporting keys
Copying keys by hand is error-prone. The AWS CLI can call AssumeRole for you and refresh the credentials when they expire. Add a profile to ~/.aws/config:
[profile auditor]
role_arn = arn:aws:iam::444455556666:role/ReadOnlyAuditor
source_profile = default
role_session_name = audit-sessionThen run aws s3 ls --profile auditor. The CLI uses the default profile's credentials to call AssumeRole, caches the result, and requests new credentials when needed.
If the trust policy requires MFA, add mfa_serial with your MFA device ARN to the profile. The CLI then prompts for a code.
Session duration
The default session lasts one hour. You can request a different length with --duration-seconds, within these limits:
| Setting | Value |
|---|---|
| Minimum duration | 900 seconds (15 minutes) |
| Default duration | 3600 seconds (1 hour) |
| Maximum, set per role | 1 hour to 12 hours (`MaxSessionDuration`) |
If you request more than the role's maximum, the call fails. Role chaining is stricter. When one assumed role assumes another, the second session is limited to one hour, whatever MaxSessionDuration says.
Narrowing a session with a session policy
You can pass an inline session policy with --policy to shrink what a single session can do. The session's effective permissions are the intersection of the role's permissions policies and the session policy. A session policy can only remove permissions. It cannot grant anything the role lacks.
Other ways to obtain temporary credentials
AssumeRole is the general-purpose call. STS has other operations for specific callers.
| Operation | Typical caller | Purpose |
|---|---|---|
| `AssumeRole` | IAM users and roles | Switch to a role, often in another account |
| `AssumeRoleWithSAML` | Users signed in through a SAML identity provider | Exchange a SAML assertion for role credentials |
| `AssumeRoleWithWebIdentity` | Users or workloads with an OIDC token | Exchange an OIDC token for role credentials |
| `GetSessionToken` | IAM users | Get temporary credentials for the same user, often to add MFA |
Common errors
AccessDeniedonAssumeRole. One of the two policies does not allow the call. Check that the trust policy names the caller and that the caller hassts:AssumeRoleon the exact role ARN. Also check any condition, such asaws:MultiFactorAuthPresent, which fails if the caller signed in without MFA.ExpiredToken. The session ended. Assume the role again.InvalidClientTokenIdorSignatureDoesNotMatchafter exporting keys. The session token is missing or was copied incompletely. Temporary keys always need all three values.- Old keys still set in the environment. Environment variables take precedence over profiles in the CLI's credential chain. A leftover
AWS_ACCESS_KEY_IDcan make a profile appear to be ignored. - Duration rejected. The requested value exceeds the role's
MaxSessionDuration, or you are role chaining.
Why this matters
Temporary credentials expire without anyone remembering to rotate them. A leaked set is usable only until its expiry. A leaked long-lived access key is usable until someone finds and deletes it. This is the main reason to give workloads roles instead of IAM user keys.
Exercise
You run aws sts assume-role for a role in another account and get AccessDenied. The role's trust policy names your account's root as principal. Name the two places you should check, and say what output of aws sts get-caller-identity proves a successful switch afterward.
Show answer
Check (1) the role's trust policy, including any condition such as MFA that your session may not meet, and (2) your own identity-based policy, which must allow sts:AssumeRole on that exact role ARN. After exporting the three returned values (access key ID, secret access key, session token), get-caller-identity should show an assumed-role/<RoleName>/<SessionName> ARN in the role's account.
Exercise
A role has MaxSessionDuration set to 12 hours. A script that already runs as another assumed role calls AssumeRole on it with --duration-seconds 14400. Will the call succeed, and why?
Show answer
No. Role chaining limits the new session to one hour, so 14400 seconds (4 hours) is rejected even though the role's maximum is 12 hours.