Sub topic 3
Defining what the role can do
This chapter assumes you know IAM users, roles, and JSON policy documents, and that you can read an ARN. It covers the permissions policy of a role in the account that owns it (the trusting account). You will write the policy before you write the trust policy, because the role's power comes from this document.
Two policies, two questions
A role has two separate policy types, and each answers a different question.
| Policy | Question it answers | Where it lives |
|---|---|---|
| Trust policy | Who may assume this role? | On the role, in the trusting account |
| Permissions policy | What can a session of this role do? | Attached to the role, in the trusting account |
Both live in the account that owns the role. The permissions policy is a normal identity policy. Anyone who assumes the role gets exactly what it allows, no matter who they were before. Their original permissions in their own account do not carry over into the session.
Choose a permissions policy
You can attach AWS managed policies or write a customer managed policy. Use a managed policy when it matches the job. For an auditor, the AWS managed policy ReadOnlyAccess is a common starting point. It is broad, so it is often better to write a narrow policy for the actual task.
Attach the managed policy with the CLI. Run this in the trusting account (222222222222), using credentials for that account. The role must already exist, which means you need its trust policy first. The next chapter covers that.
aws iam attach-role-policy \
--role-name ReadOnlyAuditor \
--policy-arn arn:aws:iam::aws:policy/ReadOnlyAccessWrite a least-privilege policy
A narrow policy limits the damage if the role is misused. This example lets sessions list one bucket and read its objects, and nothing else.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::example-audit-reports"
},
{
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example-audit-reports/*"
}
]
}Notice that the two actions use two different resource ARNs. s3:ListBucket applies to the bucket itself. s3:GetObject applies to the objects inside it, so its ARN ends in /*. Putting both actions on one ARN is a common mistake that leads to AccessDenied.
Save the document as audit-s3-read.json, then create it as an inline policy on the role.
aws iam put-role-policy \
--role-name ReadOnlyAuditor \
--policy-name AuditS3Read \
--policy-document file://audit-s3-read.jsonHow the permissions policy interacts with other limits
The permissions policy sets the most the role can do, but other controls can reduce it:
- A permissions boundary on the role caps what the policies can grant.
- A service control policy (SCP) in AWS Organizations can deny actions for the whole account.
- A session policy, passed when the role is assumed, can narrow a single session further.
Access is granted only when the permissions policy allows an action and none of these limits block it. An explicit Deny anywhere wins. Later chapters return to boundaries and SCPs.
Check your understanding
Exercise
A role in account 222222222222 has a permissions policy that allows only s3:GetObject on arn:aws:s3:::example-audit-reports/*. A user in account 111111111111 has full S3 access in their own account and assumes the role. Can they list the bucket example-audit-reports using the role session? Explain why or why not.
Show answer
No. Once the user assumes the role, the session has only the role's permissions. The user's own S3 access in account 111111111111 does not apply. The role allows s3:GetObject on objects, but not s3:ListBucket on the bucket, so the list call is denied. Adding s3:ListBucket on arn:aws:s3:::example-audit-reports to the permissions policy would fix it.