Sub topic 6
Assuming the Role with AWS STS
This chapter covers the call that connects the two accounts: sts:AssumeRole. It assumes you already know IAM users, roles, JSON policies, ARNs, and the AWS CLI. It also assumes you have seen the trust policy and the caller policy from the earlier chapters. Here you will use them together, read the response, and handle the errors that appear when something is missing.
What AssumeRole does
AWS Security Token Service (STS) is the service that issues temporary security credentials. AssumeRole is the STS API operation that exchanges the identity you already have for a session in a role. The role can be in your own account or in another one.
When you call AssumeRole, STS does not change your original identity. It returns a new set of credentials. Requests signed with those credentials act as the role, with the role's permissions, until the credentials expire.
In the cross-account setup used in this tutorial:
- Account
111111111111is the trusted account. It holds the caller, such as an IAM user or role. - Account
222222222222is the trusting account. It holds the roleReadOnlyAuditor.
What STS checks
STS makes an authorization decision before it issues credentials. For a cross-account call, both of these must allow the request:
- The trust policy on the role in account
222222222222must name the caller (or its account) as a principal and allowsts:AssumeRole. - An identity policy in account
111111111111must allow the caller to performsts:AssumeRoleon that role's ARN.
Within a single account, one side is enough in many cases. Across accounts, an Allow on one side with nothing on the other results in a denial.
The two policies you need
This is the trust policy attached to the role in account 222222222222. It lets account 111111111111 delegate permission to assume the role to its own principals.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111111111111:root" },
"Action": "sts:AssumeRole"
}
]
}The :root principal means "the account". It does not mean the root user. It makes the trust decision for the whole account. Each principal in that account still needs its own permission.
This is the identity policy for the caller in account 111111111111. It allows only the named role.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::222222222222:role/ReadOnlyAuditor"
}
]
}Using the exact role ARN as the Resource keeps the caller from assuming any other role in that account, even if that role also trusts account 111111111111.
Calling AssumeRole from the CLI
Run this with the credentials of the caller in account 111111111111:
aws sts assume-role \
--role-arn arn:aws:iam::222222222222:role/ReadOnlyAuditor \
--role-session-name audit-sessionThe two required parameters are:
--role-arn: the ARN of the role to assume.--role-session-name: a label for this session. It appears in the assumed-role ARN and in CloudTrail logs. Choose something that identifies who or what is using the session, such as a person's name or a job name.
A successful call returns JSON like this. The values here are shortened examples.
{
"Credentials": {
"AccessKeyId": "ASIAEXAMPLEKEYID",
"SecretAccessKey": "example-secret-access-key",
"SessionToken": "example-long-session-token",
"Expiration": "2026-11-26T19:16:04+00:00"
},
"AssumedRoleUser": {
"AssumedRoleId": "AROAEXAMPLEROLEID:audit-session",
"Arn": "arn:aws:sts::222222222222:assumed-role/ReadOnlyAuditor/audit-session"
}
}Note three things in the response:
- The access key ID starts with
ASIA. That prefix marks temporary credentials. Long-term IAM user keys start withAKIA. - The
SessionTokenis required. A request signed with the access key and secret but without the token is rejected. - The
AssumedRoleUser.Arnis in account222222222222, in the formarn:aws:sts::ACCOUNT:assumed-role/ROLE/SESSION. Resource policies and logs refer to the session this way.
Using the credentials
The CLI and the SDKs read credentials from environment variables. Export the three values from the response:
export AWS_ACCESS_KEY_ID="ASIAEXAMPLEKEYID"
export AWS_SECRET_ACCESS_KEY="example-secret-access-key"
export AWS_SESSION_TOKEN="example-long-session-token"
aws sts get-caller-identityThe last command should return an assumed-role ARN in account 222222222222. If it still shows your original identity, the variables were not exported in that shell, or a higher-precedence source is overriding them.
You can avoid copying values by hand. This script requests the session and exports the values with --query and --output text:
creds=$(aws sts assume-role \
--role-arn arn:aws:iam::222222222222:role/ReadOnlyAuditor \
--role-session-name audit-session \
--query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" \
--output text)
export AWS_ACCESS_KEY_ID=$(echo "$creds" | awk '{print $1}')
export AWS_SECRET_ACCESS_KEY=$(echo "$creds" | awk '{print $2}')
export AWS_SESSION_TOKEN=$(echo "$creds" | awk '{print $3}')What the session can do
The session's permissions come from the role's permissions policy. Your own permissions from account 111111111111 do not carry over. For example, if your user can administer S3 in its own account but ReadOnlyAuditor only has read access, then while using the session you only have the role's read access.
You can also pass an optional session policy with --policy or --policy-arns. A session policy can only reduce the permissions of the session. The effective permissions are the intersection of the role's permissions policy and the session policy. It never adds permissions that the role lacks.
Optional parameters worth knowing
| Parameter | Purpose |
|---|---|
| `--duration-seconds` | Requests a session length. The default is 3600 seconds (one hour). |
| `--external-id` | Passes the external ID that a trust policy may require. A later chapter covers it. |
| `--serial-number` and `--token-code` | Pass an MFA device and its current code when the trust policy requires MFA. |
| `--policy` / `--policy-arns` | Scope the session down with an inline or managed session policy. |
| `--tags` | Attach session tags that policies can match on. |
A requested duration cannot exceed the role's maximum session duration. That setting is 1 hour by default and can be raised on the role up to 12 hours. If you ask for more than the role allows, the call fails with a validation error.
Common failures at this step
| Symptom | Likely cause |
|---|---|
| `AccessDenied` mentioning that the user is not authorized to perform `sts:AssumeRole` | The caller's identity policy in account `111111111111` is missing, or its `Resource` does not match the role ARN exactly. |
| `AccessDenied` on the role in the trusting account | The trust policy does not list the caller's account or principal, or a condition such as an external ID is not satisfied. |
| `ValidationError` on `DurationSeconds` | The requested duration exceeds the role's maximum session duration. |
| `InvalidClientTokenId` | The calling credentials are expired or invalid. |
| Role not found | The role ARN has a typo, or the role is in a different account than you expect. |
An explicit Deny in any applicable policy overrides every Allow. This includes service control policies and permissions boundaries, which a later chapter covers.
Exercise
A user in account 111111111111 runs aws sts assume-role for a role in account 222222222222. The role's trust policy names arn:aws:iam::111111111111:root. The call returns AccessDenied. What is the most likely missing piece, and how can you confirm the credentials you receive after the fix are the role's and not your own?
Show answer
The user probably has no identity policy in account 111111111111 allowing sts:AssumeRole on the role's ARN. The trust policy only covers the trust side, and a cross-account call also needs permission on the caller's side. After the fix, run aws sts get-caller-identity with the returned credentials set. The ARN should be arn:aws:sts::222222222222:assumed-role/ReadOnlyAuditor/<session-name>, and the access key ID should begin with ASIA.
Exercise
You want a session to have fewer permissions than the role normally grants, for one specific task. Which parameter lets you do that, and can it ever grant more than the role allows?
Show answer
Pass a session policy with --policy or --policy-arns. The session gets the intersection of the role's permissions policy and the session policy, so it can only reduce permissions and can never grant more than the role allows.