Sub topic 10
Trust policies: who may assume a role
This chapter assumes you know what an IAM role is and can read a policy document's Effect, Action, Resource, and Condition elements. A role has two separate kinds of policy. This chapter covers the one that controls who can use the role.
Two policies, two questions
Every role answers two questions with two different policies:
- The trust policy answers "who may assume this role?"
- The permissions policies answer "what can someone do once they have assumed it?"
A caller needs both. If the trust policy does not name you, you cannot get the role's credentials, whatever its permissions say. If you can assume the role but its permissions policies are empty, you get a session that can do almost nothing.
The trust policy is the only resource-based policy that IAM roles support. It is attached to the role itself, so it contains a Principal element, which identity-based policies do not.
Anatomy of a trust policy
A trust policy is a normal policy document. The statement uses Principal to say who is trusted and Action to say how they may assume the role. Resource is omitted, because the resource is the role itself.
This trust policy lets the Amazon EC2 service assume the role. An instance profile uses it to give applications on an instance temporary credentials.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "ec2.amazonaws.com" },
"Action": "sts:AssumeRole"
}
]
}The Principal can take several forms:
| Principal type | Example | Who it trusts |
|---|---|---|
| `Service` | `ec2.amazonaws.com` | An AWS service |
| `AWS` | `arn:aws:iam::111122223333:root` | An account, which delegates the decision to that account's own policies |
| `AWS` | `arn:aws:iam::111122223333:user/alice` | One specific user or role |
| `Federated` | A SAML or OIDC provider ARN | Identities from an external identity provider |
The Action must match how the caller assumes the role. Plain sts:AssumeRole is used for most cases. Federation uses sts:AssumeRoleWithSAML or sts:AssumeRoleWithWebIdentity.
Adding conditions
A trust policy supports Condition, which narrows who can assume the role. This policy trusts principals in account 111122223333, but only if they signed in with MFA.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:root" },
"Action": "sts:AssumeRole",
"Condition": {
"Bool": { "aws:MultiFactorAuthPresent": "true" }
}
}
]
}Trusting an account is only half the grant
Naming an account's root in Principal does not give every user in that account access. It means the trust policy accepts that account's decision. A principal there can assume the role only if its own identity-based policy also allows sts:AssumeRole on the role's ARN. When the caller is in a different account, both sides must allow the action.
Common errors
- AccessDenied on AssumeRole. Check that the trust policy names the caller and that the caller's own policy allows
sts:AssumeRole. - Wrong action. A trust policy that allows
sts:AssumeRoledoes not permit web identity federation, which needssts:AssumeRoleWithWebIdentity. - Wrong service principal. A role meant for Lambda that trusts
ec2.amazonaws.comcannot be used by Lambda. - Editing the wrong policy. Adding permissions to a role never changes who can assume it. Change the trust policy for that.
Exercise
A role's trust policy names account 111122223333 as the principal. A user in that account calls sts:AssumeRole and gets AccessDenied. Give one likely cause and the fix.
Show answer
The user's own identity-based policy probably does not allow sts:AssumeRole on the role's ARN. Add a statement to the user's policy (or a group policy) that allows sts:AssumeRole with the role's ARN as the Resource.