Sub topic 4
Writing the trust policy in the role's account
This chapter assumes you can read IAM JSON policies, know what an ARN is, and have used the AWS CLI. It also assumes you have seen the trusted and trusting account model from the earlier chapters. Here you write the document that decides who may assume a role: the trust policy. It lives in the trusting account, which is the account that owns the role.
What a trust policy is
Every IAM role has two kinds of policy:
- The permissions policy says what the role can do once someone is using it.
- The trust policy says who may become the role.
The trust policy is a resource-based policy attached to the role. It is the only resource-based policy a role supports, and AWS calls it the role's assume role policy document. That name appears in the CLI and API.
Because the policy is attached to the role, it has no Resource element. The resource is always the role itself. Its statements use Principal, Action, Effect, and optionally Condition.
The minimal cross-account trust policy
Suppose account 222222222222 owns a role named ReadOnlyAuditor. Account 111111111111 is the trusted account. This trust policy is attached to the role:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111111111111:root" },
"Action": "sts:AssumeRole"
}
]
}Each element does a specific job:
| Element | Value | Meaning |
|---|---|---|
| `Effect` | `Allow` | The statement grants trust. |
| `Principal` | account `111111111111` | Who is being trusted. |
| `Action` | `sts:AssumeRole` | The only thing the principal may do with this role: request temporary credentials for it. |
What `:root` means
The :root suffix does not name the account's root user. It is the ARN form that stands for the whole account. The statement says: "Account 111111111111 may delegate sts:AssumeRole permission to its own principals."
The trust policy does not grant access to every user in that account. It only opens the door. Administrators in account 111111111111 still decide which of their users and roles may walk through, using an identity policy (next chapter).
Creating a role with a trust policy
Save the JSON above as trust.json. Then, using credentials for account 222222222222, create the role:
aws iam create-role \
--role-name ReadOnlyAuditor \
--assume-role-policy-document file://trust.json
aws iam attach-role-policy \
--role-name ReadOnlyAuditor \
--policy-arn arn:aws:iam::aws:policy/ReadOnlyAccessThe first command writes the trust policy. The second attaches a permissions policy. They are separate: changing one never changes the other.
To change trust later, replace the whole document:
aws iam update-assume-role-policy \
--role-name ReadOnlyAuditor \
--policy-document file://trust.jsonChoosing the principal: account or specific identity
The Principal element decides how wide the trust is. These are the common choices:
| Principal value | Who can be allowed in |
|---|---|
| `arn:aws:iam::111111111111:root` | Any principal in that account, if its own identity policy permits. |
| `arn:aws:iam::111111111111:role/CiDeployer` | Only that one role. |
| `arn:aws:iam::111111111111:user/alice` | Only that one user. |
| `"*"` | Any AWS principal anywhere. Safe only with a strong condition. |
Trusting the account versus trusting one identity
The difference matters for cross-account access.
When you name the account root, the caller needs an identity policy that allows sts:AssumeRole. The trust policy alone is not enough.
When you name one specific user or role from another account, the caller still needs an identity policy for sts:AssumeRole. That is the rule for cross-account calls: the request needs an allow on both sides. Naming a specific principal in the trust policy narrows the trust. It does not remove the need for the caller-side policy.
Inside a single account the rule is different. A trust policy that names a specific principal is enough on its own, and no identity policy is needed. Do not carry that shortcut over to cross-account setups.
Specific ARNs are stored as unique IDs
When you save a trust policy that names a user or role, IAM converts the ARN to the principal's internal unique ID. In the console you may then see a value like AIDA... or AROA... instead of the ARN. This protects you. If someone deletes role/CiDeployer and creates a new role with the same name, the new role has a different ID, so the old trust does not transfer to it.
The same behavior has a cost. If you recreate a legitimate principal, you must update the trust policy too.
No wildcards in the principal
The Principal element does not support partial wildcards. A value such as arn:aws:iam::111111111111:user/* is invalid. The only wildcard allowed is "*" for everyone. To trust a set of identities by name pattern, trust the account and filter with a condition:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111111111111:root" },
"Action": "sts:AssumeRole",
"Condition": {
"StringLike": { "aws:PrincipalArn": "arn:aws:iam::111111111111:role/audit-*" }
}
}
]
}Now only roles whose names start with audit- in account 111111111111 can assume the role, even if other identities in that account have a broad sts:AssumeRole permission. StringLike is used because it supports wildcards. StringEquals does not.
Trusting a whole organization
The spine's pattern for organization-wide access uses a "*" principal with a condition:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "*" },
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": { "aws:PrincipalOrgID": "o-exampleorgid" }
}
}
]
}The aws:PrincipalOrgID condition key matches the AWS Organization of the caller's account. New accounts that join the organization are covered automatically, so you do not edit the policy for each one.
Callers still need their own sts:AssumeRole permission. The trust policy only says "anyone from this organization is acceptable."
<br>
Other actions you may need to allow
sts:AssumeRole is the standard action. Two related actions appear in trust policies for specific features:
sts:TagSessionis required if the caller passes session tags when assuming the role.sts:SetSourceIdentityis required if the caller sets a source identity, which helps you trace who is behind a session in audit logs.
If the caller passes tags or a source identity and the trust policy does not allow these actions, the call fails with AccessDenied, even though plain sts:AssumeRole is allowed. Add them to the same statement's Action list only when you need the feature.
Common mistakes
- Putting the trust policy in the wrong account. It belongs to the role, so create it in
222222222222, not in the account of the caller. - Using a wrong or nonexistent principal. IAM rejects the policy with
MalformedPolicyDocument: Invalid principal in policy. Check the account ID and the spelling of the ARN. A principal that was deleted also triggers this. - Confusing the two policies. Putting
sts:AssumeRoleallowed underPrincipalin the caller's identity policy does nothing. Identity policies useResource, and trust policies usePrincipal. - Expecting the trust policy to grant permissions. It only controls who may assume the role. What the session can do comes from the role's permissions policy.
- Exceeding the size limit. Trust policies are limited to 2,048 characters by default. You can request a higher quota, up to 4,096. Large lists of principals are better handled with a condition such as
aws:PrincipalOrgID. - Editing a trust policy and expecting open sessions to change. Sessions that already exist keep working until they expire. Changing the trust policy affects only new
AssumeRolecalls.
Checking your work
After writing the trust policy, read it back as the account that owns the role:
aws iam get-role --role-name ReadOnlyAuditor \
--query 'Role.AssumeRolePolicyDocument'Check three things: the account ID in Principal is the trusted account, the Action is sts:AssumeRole, and any Condition matches how the callers will actually authenticate. The next chapter adds the matching identity policy on the caller's side.
Exercise
Account 222222222222 owns the role ReadOnlyAuditor with a trust policy that names arn:aws:iam::111111111111:root as the principal. A user in account 111111111111 has no identity policy mentioning sts:AssumeRole and calls the role. What happens, and why? What would you change in the trust policy so that only roles named audit-* in that account could ever assume it?
Show answer
The call fails with AccessDenied. For a cross-account call, both sides must allow it: the trust policy allows the account, but the user's own account has not granted the user sts:AssumeRole, so the request is denied. To restrict the trust, keep the account root as the principal and add a Condition with StringLike on aws:PrincipalArn set to arn:aws:iam::111111111111:role/audit-*. Wildcards cannot go in the Principal element itself.