Sub topic 6
Identity-based policies: managed and inline
This chapter assumes you can read a JSON policy document (Version, Statement, Effect, Action, Resource) and know what an IAM user, group, and role are. It covers the three ways to attach an identity-based policy and when to pick each.
What an identity-based policy is
An identity-based policy is a permissions policy attached to an IAM user, group, or role. It answers the question "what can this identity do?" and has no Principal element, because the principal is the identity the policy is attached to.
The policy below allows listing one bucket and reading its objects. Bucket-level and object-level actions use different resource ARNs, so the policy needs two statements.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListTheBucket",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::amzn-s3-demo-bucket"
},
{
"Sid": "ReadObjects",
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*"
}
]
}The three forms
| Form | Who writes it | Reusable | Where it lives |
|---|---|---|---|
| AWS managed policy | AWS | Yes, attach to many identities | Standalone object in your account's view |
| Customer managed policy | You | Yes, attach to many identities | Standalone object in your account |
| Inline policy | You | No, one identity only | Embedded in the identity |
AWS managed policies
AWS creates and updates these. Examples are ReadOnlyAccess and AmazonS3ReadOnlyAccess. They are a fast starting point, but they are written to cover common tasks broadly, so they often grant more than one workload needs. You cannot edit them. AWS can change their contents when new services or actions appear.
Customer managed policies
You create these in your account. A single policy can be attached to many users, groups, and roles, and a change to the policy applies to every attached identity at once. Customer managed policies keep versions (up to five), so you can set a previous version as the default to roll back a bad change.
Inline policies
An inline policy is embedded directly in one user, group, or role. It is deleted when the identity is deleted and cannot be attached elsewhere. Use it when you need a strict one-to-one link between a policy and an identity, for example a permission that must never be reused by accident.
Choosing between them
- Start with a customer managed policy for permissions you want to reuse or version.
- Use an AWS managed policy when its scope matches your need, or as a template to copy and narrow.
- Use an inline policy only when the policy should live and die with one identity.
Prefer attaching policies to groups or roles instead of individual users. This keeps the permission model easy to audit.
Common errors
- Putting
s3:GetObjecton the bucket ARN without/*. The request is denied because object actions need the object ARN. - Expecting an AWS managed policy to stay fixed. AWS may update it.
- Adding
Principalto an identity-based policy. IAM rejects it. - Hitting size limits. Inline policies have aggregate size limits per identity, and managed policies have a per-policy size limit, so split large policies.
Exercise
A role needs the same S3 read permissions as two other roles, and the permissions will change over time. Which policy form fits best, and why?
Show answer
A customer managed policy. It can be attached to all three roles, one edit updates every role, and its version history allows rollback. An inline policy would need to be copied and edited three times.