Sub topic 5
Allowing callers to assume the role from their own account
This chapter assumes you know IAM users, roles, and JSON policies, and that you have read the chapter on the role's trust policy. Here the focus is the second half of the handshake: the identity policy in the caller's account.
Why the caller needs its own policy
Cross-account access needs permission on both sides.
- The trusting account (222222222222) owns the role. Its trust policy says who may assume it.
- The trusted account (111111111111) owns the caller. An identity policy there must allow the caller to call
sts:AssumeRole.
The trust policy in the previous chapter names account 111111111111 as a whole. It does not give any single user or role in that account the right to act. That right must be delegated by an administrator in account 111111111111. Without the delegation, the caller gets AccessDenied, even though the trust policy is correct.
The identity policy
Attach this policy to the IAM user or role that will make the call, in account 111111111111.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::222222222222:role/ReadOnlyAuditor"
}
]
}The Resource element is the ARN of the role in the other account. It is not an ARN in the caller's own account. Only the role named there can be assumed. The account ID and the role name must both match exactly. Role names in an ARN are case sensitive, and a path in the role's ARN, such as role/audit/ReadOnlyAuditor, must be included.
Keep the scope narrow
"Resource": "*" would let the caller attempt to assume any role in any account whose trust policy allows it. Prefer a specific ARN. If a team needs several roles, list them as an array of ARNs, or use a wildcard only over a naming convention you control, such as arn:aws:iam::222222222222:role/Audit-*.
Attaching the policy to a group is often easier than attaching it to individual users. Every user in the group then inherits the permission.
How the two policies combine
When the caller and the role are in different accounts, AWS evaluates both policies, and both must allow the request. An explicit Deny in either policy wins over any Allow. Other layers can also block the call, such as a service control policy or a permissions boundary. The guardrails chapter covers those.
A same-account case differs. When the caller and the role are in the same account, the trust policy alone can be enough if it names the specific user or role. That shortcut does not apply across accounts.
Check your work
Run this command as the caller to confirm which identity you are using before you try to assume the role.
aws sts get-caller-identityThe Account field in the output should read 111111111111, and the Arn field should be the user or role that has the policy attached. A mismatch usually means you are using the wrong profile or old credentials from environment variables.
Exercise
A developer's IAM user is in account 111111111111. The role ReadOnlyAuditor in account 222222222222 has a trust policy naming arn:aws:iam::111111111111:root. The developer calls sts:AssumeRole and receives AccessDenied. The developer's user has no policy that mentions sts:AssumeRole. What is missing, and what should the policy contain?
Show answer
The identity policy in account 111111111111 is missing. The trust policy only permits the account to delegate access. Attach a policy to the user (or to a group the user belongs to) that allows the action sts:AssumeRole with Resource set to arn:aws:iam::222222222222:role/ReadOnlyAuditor.