Sub topic 6
Assuming the role with AWS STS
This chapter assumes you know IAM users, roles, JSON policies, ARNs, and the basic AWS CLI. You have already seen the two policies that make cross-account access possible. Now you will use them by calling AWS Security Token Service (STS).
What AssumeRole does
AssumeRole is an STS API call. The caller names a role ARN. STS checks the permissions on both sides and, if both allow the call, returns temporary credentials for that role.
The two checks are:
- The trust policy on the role (account 222222222222) must allow the caller's principal.
- An identity policy in the caller's account (111111111111) must allow
sts:AssumeRoleon that role ARN.
If either side is missing, STS returns AccessDenied.
Calling AssumeRole from the CLI
This is the identity policy in the caller's account that allows the call:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::222222222222:role/ReadOnlyAuditor"
}
]
}With the caller's own credentials configured, run:
aws sts assume-role \
--role-arn arn:aws:iam::222222222222:role/ReadOnlyAuditor \
--role-session-name audit-sessionThe --role-session-name is required. It labels the session and appears in CloudTrail logs, so choose a name that identifies the person or job.
Reading the response
The response has two parts you will use.
| Field | Meaning |
|---|---|
| `Credentials.AccessKeyId` | Temporary access key ID |
| `Credentials.SecretAccessKey` | Temporary secret key |
| `Credentials.SessionToken` | Token that must accompany the key pair |
| `Credentials.Expiration` | UTC time when the credentials stop working |
| `AssumedRoleUser.Arn` | The identity of the session, in the form `arn:aws:sts::222222222222:assumed-role/ReadOnlyAuditor/audit-session` |
The role's permissions policy now defines what you can do. Your original permissions no longer apply while you use these credentials.
Using the credentials
Export all three values as environment variables. Leaving out the session token is a common error and causes an InvalidClientTokenId or similar failure.
export AWS_ACCESS_KEY_ID=ASIAEXAMPLEKEYID
export AWS_SECRET_ACCESS_KEY=exampleSecretAccessKey
export AWS_SESSION_TOKEN=exampleSessionToken
aws sts get-caller-identityKeys that begin with ASIA are temporary. The get-caller-identity call should show the assumed-role ARN in account 222222222222. Unset the three variables to return to your own identity.
Common errors
- AccessDenied: not authorized to perform sts:AssumeRole. Check both policies. The trust policy must name the caller's account or principal, and the caller's identity policy must list the exact role ARN.
- Wrong account ID or role name. A typo in the ARN yields the same
AccessDenied, because STS does not reveal whether a role exists. - Caller is a role, not a user. The trust policy
rootprincipal delegates trust to the account, but the caller still needs its ownsts:AssumeRolepermission.
Exercise
A user in account 111111111111 runs aws sts assume-role for a role in account 222222222222. The role's trust policy names account 111111111111, but the call returns AccessDenied. What is the most likely missing piece, and where do you add it?
Show answer
The user has no identity policy allowing sts:AssumeRole on the role's ARN. Add an Allow statement for that action and role ARN to the user, or to a group or role the user belongs to, in account 111111111111.