Sub topic 8
Reaching other accounts with IAM roles
This chapter assumes you know IAM users, roles and JSON policies, and that you can run the AWS CLI. It also assumes you have an organization with at least one member account.
Why roles, not users
IAM users are scoped to one account. If you created a user in every member account, you would manage many long-lived access keys. Cross-account roles avoid this. A person or workload signs in once, then asks AWS Security Token Service (STS) for temporary credentials in the account it needs.
Two pieces of configuration must agree:
- Trust policy: attached to the role in the target account. It names who may assume the role.
- Identity policy: attached to the caller in the source account. It allows
sts:AssumeRoleon the role's ARN.
For cross-account access, both must allow the call. Missing either one gives AccessDenied.
The default administrative role
When you create a member account with create-account, Organizations adds a role named OrganizationAccountAccessRole. It trusts the management account and has administrator permissions. Accounts you invite do not get this role automatically. You must create it yourself.
From the management account, assume it like this:
aws sts assume-role \
--role-arn arn:aws:iam::111122223333:role/OrganizationAccountAccessRole \
--role-session-name admin-sessionThe response contains an AccessKeyId, a SecretAccessKey and a SessionToken. Credentials last one hour by default.
Copying these values into environment variables works, but a CLI profile does the same job and refreshes the credentials for you. Add this to ~/.aws/config:
[profile prod-app1-admin]
role_arn = arn:aws:iam::111122223333:role/OrganizationAccountAccessRole
source_profile = defaultThen run aws s3 ls --profile prod-app1-admin.
Writing your own role
The default role is too powerful for daily use. Create narrower roles in each account, for example a read-only auditor role. Its trust policy names the account that is allowed to assume it:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::999988887777:root" },
"Action": "sts:AssumeRole"
}
]
}Trusting an account's root principal delegates the decision to that account. Its administrators decide which of their own users and roles may assume the role, through identity policies. To restrict it further, name a specific role ARN as the principal.
Common errors
| Symptom | Likely cause |
|---|---|
| `AccessDenied` on `AssumeRole` | The trust policy does not name the caller, or the caller has no `sts:AssumeRole` permission |
| Role assumed, but actions fail | The role's permissions are too narrow, or an SCP in the target account denies the action |
| Role not found | Wrong account ID, or an invited account that never received `OrganizationAccountAccessRole` |
| Session expires mid-task | The one-hour default was reached. Role chaining is also capped at one hour |
Exercise
A role in account 111122223333 trusts account 999988887777, but a user in 999988887777 still gets AccessDenied when assuming it. Which two things should you check?
Show answer
Check that the user (or its group or role) has an identity policy allowing sts:AssumeRole on the role's ARN. Then check whether an SCP applied to either account denies sts:AssumeRole or the actions the role needs. The trust policy already allows the account, so it is not the problem.