Sub topic 4
Writing the trust policy in the role's account
This chapter assumes you know IAM roles, JSON policy documents, ARNs, and the basic AWS CLI. It covers the trust policy, the first of the two permissions a cross-account call needs.
What the trust policy does
Every IAM role has two kinds of policy. The permissions policy says what the role can do. The trust policy says who can assume the role. The trust policy is a resource-based policy attached to the role itself, so it lives in the trusting account, the account that owns the role.
If the trust policy does not name the caller, the call to sts:AssumeRole fails, whatever the caller's own policies say.
A minimal cross-account trust policy
This policy is attached to a role in account 222222222222. It lets account 111111111111 delegate the right to assume the role.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111111111111:root" },
"Action": "sts:AssumeRole"
}
]
}Each element has a job:
| Element | Meaning here |
|---|---|
| `Effect` | `Allow` grants the trust. |
| `Principal` | Who is trusted. This is the account in `AWS`. |
| `Action` | `sts:AssumeRole` is the API call being permitted. |
What the root principal means
The ARN ending in :root does not mean the root user. It means the whole account 111111111111. Naming it does not let every identity in that account assume the role. It only says the trusting account hands the decision to the trusted account's administrators.
For a principal in the trusted account to succeed, its own identity policy must also allow sts:AssumeRole on this role. The next chapter covers that policy. Both sides must allow the call, so cross-account access needs two policies.
You can also name a specific principal instead of the account:
- A user:
arn:aws:iam::111111111111:user/alice - A role:
arn:aws:iam::111111111111:role/DevOps
A specific principal is stricter. In that case the principal is trusted directly, and the trusted account's administrators can no longer control access through their own policies alone.
Creating the role with the CLI
Run this in account 222222222222. Save the policy above as trust-policy.json, then create the role:
aws iam create-role \
--role-name ReadOnlyAuditor \
--assume-role-policy-document file://trust-policy.jsonThe parameter is called --assume-role-policy-document because the trust policy is stored as the role's assume role policy. To change trust later, use aws iam update-assume-role-policy with the same kind of document.
Common mistakes
- Putting permissions such as
s3:GetObjectin the trust policy. Those belong in the permissions policy. - Adding a
Resourceelement. In a trust policy the resource is the role itself, so you leave it out. - Using the wrong account ID in
Principal, which is a frequent cause ofAccessDenied. - Deleting the trusted user or role and recreating it. A trust policy that names a specific principal stores a unique ID, so the recreated principal no longer matches.
Exercise
A trust policy in account 222222222222 names arn:aws:iam::111111111111:root as the principal. A user in account 111111111111 has no identity policy mentioning sts:AssumeRole. Can the user assume the role, and why?
Show answer
No. The trust policy only delegates the decision to account 111111111111. The user's identity policy must also allow sts:AssumeRole on the role's ARN, so without it the call returns AccessDenied.