Sub topic 5
Identity-based policies
This chapter assumes you know what IAM users, groups, and roles are, can read JSON, and know what an action and an ARN look like. It covers identity-based policies: what they attach to, how they grant access, and the mistakes people make with them.
What an identity-based policy is
An identity-based policy is a JSON document attached to an IAM identity: a user, a group, or a role. It states what that identity may or may not do. Identity-based policies are one of the two main ways to grant permissions. The other is a resource-based policy, which the next chapter covers.
Because the policy is attached to the identity, the principal is implied. It is whoever the policy is attached to. For that reason an identity-based policy has no Principal element. If you add one, IAM rejects the policy.
A worked example
This policy lets its holder read and write objects in one bucket.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ReadWriteReports",
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": "arn:aws:s3:::example-reports-bucket/*"
}
]
}Read it field by field:
EffectisAllow, so the statement grants access.Actionlists two S3 API actions.Resourceends in/*, so it matches objects inside the bucket. It does not match the bucket itself.
That last point causes many errors. Actions such as s3:ListBucket act on the bucket ARN (arn:aws:s3:::example-reports-bucket), not on objects. This policy therefore lets the holder fetch an object whose key they already know, but not list the bucket's contents.
Kinds of identity-based policies
| Kind | Where it lives | Typical use |
|---|---|---|
| AWS managed policy | Created and updated by AWS | Common job functions, such as `ReadOnlyAccess` |
| Customer managed policy | Created by you, reusable | Your own least-privilege rules shared by many identities |
| Inline policy | Embedded in one user, group, or role | A one-off rule that must stay tied to that identity |
Prefer customer managed policies for anything you reuse. They are versioned, and you can see where each one is attached. An inline policy is deleted when its identity is deleted.
How the policies combine
An identity can carry several policies. It can have policies attached directly, policies inherited from groups (users only), and inline policies. For evaluation, AWS merges all of them into one set of statements. Two rules follow:
- If any statement allows the request and nothing denies it, the identity-based policies allow it.
- If any statement explicitly denies the request, it is denied. Another policy's
Allowcannot undo it.
Groups are not principals, so you cannot name a group in a Principal element. A group only passes its policies to the users inside it.
What "allowed" means here
An identity-based Allow is necessary but is not always enough. In the same account, it is usually sufficient by itself, as long as no boundary, SCP, session policy, or explicit deny blocks the request. Later chapters add those layers. With no allow at all, the request falls to the default: implicit deny.
Common errors
- Adding
Principal. Identity-based policies must not include it. - Using the bucket ARN for object actions.
s3:GetObjectneeds.../*.s3:ListBucketneeds the bucket ARN without it. - Assuming a group's policy applies to a role. Groups contain users only. A role gets permissions from its own attached policies.
- Forgetting the deny. A broad
Allowin one policy loses to a narrowDenyin another. - Editing a managed policy and expecting only one identity to change. Every identity the policy is attached to changes.
Exercise
A role has only the policy shown in this chapter. A script running as that role calls s3:ListBucket on example-reports-bucket and gets AccessDenied. Why, and how do you fix it?
Show answer
The policy allows only s3:GetObject and s3:PutObject, and its Resource matches objects (bucket/). ListBucket is not listed, so it is an implicit deny. Add a statement that allows s3:ListBucket with Resource arn:aws:s3:::example-reports-bucket (no /).