Sub topic 10
Trust policies: who may assume a role
This chapter assumes you know what an IAM role is, how an identity-based policy is structured (Effect, Action, Resource, Condition), and that you can run the AWS CLI. It covers the policy that controls who may use a role: the trust policy.
Two policies on every role
A role has two separate kinds of policy, and they answer different questions.
- The permissions policy says what the role can do once someone is using it. It is an ordinary identity-based policy.
- The trust policy says who may assume the role. It is a resource-based policy attached to the role itself.
Assuming a role means calling AWS STS to receive temporary credentials for it. The trust policy is checked at that call. The permissions policy is checked later, on each request made with those credentials.
A role is useful only when both are right. A broad permissions policy with a trust policy that names nobody gives a role that no one can use. A narrow trust policy with an empty permissions policy gives a role that can be assumed but can do nothing.
Anatomy of a trust policy
A trust policy uses the same JSON grammar as other policies, with some differences:
- It has a
Principalelement, which names who is allowed or denied. - It has no
Resourceelement. The resource is the role it is attached to. - The
Actionis normally one of the STS assume actions, such assts:AssumeRole.
Here is a trust policy for a role used by an application on Amazon EC2:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "ec2.amazonaws.com" },
"Action": "sts:AssumeRole"
}
]
}The principal is the EC2 service. Instances launched with an instance profile that contains this role can then obtain temporary credentials. Chapter material on service roles covers instance profiles in more detail.
Kinds of principal
The Principal element accepts several forms.
| Principal form | Example | Who it trusts |
|---|---|---|
| AWS service | `"Service": "ec2.amazonaws.com"` | An AWS service acting on your behalf |
| AWS account | `"AWS": "arn:aws:iam::111122223333:root"` | The account, delegating the decision to that account's own IAM policies |
| IAM user or role | `"AWS": "arn:aws:iam::111122223333:role/Deployer"` | One specific identity |
| Federated identity | `"Federated": "..."` | Users from an external identity provider (for example SAML or OIDC) |
The account form is the one that most often surprises people. Naming arn:aws:iam::111122223333:root does not mean only the root user. It means the account as a whole. The trust policy lets that account decide which of its identities may assume the role. For the assume to succeed, an identity in that account also needs its own identity-based policy that allows sts:AssumeRole on the role. That second requirement applies when the caller is in a different account from the role.
Naming a specific user or role ARN is tighter. Only that identity can assume the role, though a cross-account caller still needs its own permission to call sts:AssumeRole.
Adding conditions
Conditions narrow a trust policy in the same way they narrow any policy. This trust policy lets principals in account 111122223333 assume the role only when 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" }
}
}
]
}Useful condition keys in trust policies include:
aws:MultiFactorAuthPresentto require MFA.sts:ExternalIdto guard against the confused deputy problem when a third party assumes a role in your account.aws:SourceIpto limit where the call comes from.aws:PrincipalOrgIDto allow any principal in your AWS Organization without listing every account.
Assume actions
sts:AssumeRole is the common action, but the action in the trust policy must match how the caller assumes the role.
| Action | Used when |
|---|---|
| `sts:AssumeRole` | An IAM identity or service assumes the role directly |
| `sts:AssumeRoleWithSAML` | A user signs in through a SAML identity provider |
| `sts:AssumeRoleWithWebIdentity` | An application uses an OIDC provider token |
| `sts:TagSession` | The caller passes session tags while assuming |
| `sts:SetSourceIdentity` | The caller sets a source identity for auditing |
If the action in the trust policy does not match the call, the assume fails even when the principal is correct. Session tags are a frequent case. A caller that passes tags needs sts:TagSession allowed in the trust policy as well as sts:AssumeRole.
Assuming a role from the CLI
With a matching trust policy in place, a caller assumes the role like this:
aws sts assume-role \
--role-arn arn:aws:iam::444455556666:role/ReadOnlyAuditor \
--role-session-name audit-session
# The response contains a Credentials object. Export its values:
export AWS_ACCESS_KEY_ID=<AccessKeyId from the response>
export AWS_SECRET_ACCESS_KEY=<SecretAccessKey from the response>
export AWS_SESSION_TOKEN=<SessionToken from the response>
aws sts get-caller-identityThe get-caller-identity output should show an ARN containing assumed-role/ReadOnlyAuditor/audit-session. If it still shows your original user, the environment variables were not applied.
Common errors
AccessDeniedonAssumeRole, with a correct principal. Check the caller's own side. A same-account caller can be allowed by the trust policy alone when the trust policy names the user or role directly. When it names the account root, or the caller is in another account, the caller also needs an identity-based policy allowingsts:AssumeRole.- Condition not met. An MFA condition fails when the caller used long-term access keys without an MFA-authenticated session.
- Wrong action. A web identity caller using
sts:AssumeRolein the trust policy instead ofsts:AssumeRoleWithWebIdentity. - Wrong principal for a service. Using the wrong service principal, such as
ec2.amazonaws.comfor a Lambda function. Lambda needslambda.amazonaws.com. - Overly broad trust.
"Principal": "*"without a restricting condition lets any AWS principal attempt to assume the role. Always pair a wildcard principal with conditions such asaws:PrincipalOrgID. - Role chaining limits. When a role assumes another role, the session is limited to one hour, whatever the maximum session duration of the second role.
Exercises
Exercise
A role's trust policy names arn:aws:iam::111122223333:root as the principal. A user in account 111122223333 tries to assume the role and gets AccessDenied. The trust policy is otherwise correct. What is the most likely missing piece?
Show answer
The user's own identity-based policy (or one from a group or role) does not allow sts:AssumeRole on the role. Naming the account root delegates the decision to that account, so the account's identities must be granted permission to assume the role.
Exercise
You write a trust policy with "Principal": { "Service": "ec2.amazonaws.com" } and a permissions policy that is empty. An instance uses the role. Can the instance assume the role, and can it read S3 objects?
Show answer
Yes, the instance can assume the role because the trust policy allows the EC2 service. No, it cannot read S3 objects, because the permissions policy grants nothing and the request ends in an implicit deny.