Sub topic 11
Assuming a role with AWS STS
This chapter assumes you know what a role and a trust policy are, and that the AWS CLI is configured with a user or other identity that can call sts:AssumeRole.
What assuming a role means
AWS Security Token Service (STS) issues temporary credentials. When you assume a role, you call STS and ask to act as that role. STS checks the role's trust policy. If the trust policy allows you, STS returns three values:
- An access key ID.
- A secret access key.
- A session token.
All three are required. Unlike a long-lived access key, these expire, by default after one hour. A request signed with them runs as the role, not as you.
Two checks must pass
Assuming a role needs permission on both sides:
- The role's trust policy must name the caller as a principal.
- For callers in the same account, either the trust policy names the caller's user or role ARN, or the caller's own identity-based policy allows
sts:AssumeRoleon the role's ARN. Across accounts, both sides must allow it.
Assume a role with the CLI
This script assumes the role, loads the credentials into environment variables, and confirms the new identity.
read -r AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN < <(
aws sts assume-role \
--role-arn arn:aws:iam::444455556666:role/ReadOnlyAuditor \
--role-session-name audit-session \
--query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]' \
--output text
)
export AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
aws sts get-caller-identityThe Arn in the output looks like arn:aws:sts::444455556666:assumed-role/ReadOnlyAuditor/audit-session. The last part is the session name you chose. It appears in CloudTrail logs, so pick a name that identifies the person or job.
To go back to your original identity, unset the three variables:
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKENSession duration and MFA
By default a session lasts one hour. Use --duration-seconds to request between 900 seconds and the role's maximum session duration, which you can set from 1 to 12 hours. If you assume a second role using credentials from an earlier assumed role (role chaining), the session is limited to one hour.
If the trust policy requires MFA, as in the cross-account example in this tutorial, pass your device with --serial-number and --token-code. Without them, the aws:MultiFactorAuthPresent condition fails.
Common errors
| Symptom | Likely cause |
|---|---|
| `AccessDenied` on `AssumeRole` | The trust policy does not name you, or your identity lacks `sts:AssumeRole`. |
| `ExpiredToken` | The session ended. Assume the role again. |
| Calls still run as your user | Old environment variables or a profile take precedence. Check `get-caller-identity`. |
| `AccessDenied` on a service call | Assuming worked, but the role's permissions policy does not allow that action. |
Exercise
You run aws sts assume-role and receive credentials, but aws s3 ls fails with AccessDenied. Is the trust policy the problem?
Show answer
No. The assume-role call succeeded, so the trust policy allowed you. The failure comes from the role's permissions policy, which does not allow the S3 action. You also need to have exported the access key, secret key, and session token so the call runs as the role.