Sub topic 6
Identity-based policies: managed and inline
This chapter assumes you can read a policy document (Version, Statement, Effect, Action, Resource) and know what IAM users, groups, and roles are. It also assumes you can run AWS CLI commands against a test account.
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 answers the question "what may this identity do?" The policy has no Principal element, because the principal is whichever identity the policy is attached to.
Resource-based policies work the other way round. They attach to a resource such as a bucket and name who may use it. A later chapter covers them.
Identity-based policies come in three forms. They differ in who owns them, whether you can reuse them, and what happens when you delete the identity.
The three forms
| Form | Created and maintained by | Reusable across identities | Editable | Lifecycle |
|---|---|---|---|---|
| AWS managed policy | AWS | Yes | No | Exists independently. AWS may update it. |
| Customer managed policy | You | Yes | Yes, with versioning | Exists independently until you delete it. |
| Inline policy | You | No, it belongs to one identity | Yes, with no versions | Deleted together with its identity. |
AWS managed policies
AWS writes and owns these policies. Their ARNs contain aws where an account ID would normally appear, for example arn:aws:iam::aws:policy/ReadOnlyAccess. You can attach them, but you cannot change them.
They are a fast starting point. The job-function policies, such as ViewOnlyAccess, PowerUserAccess, and AdministratorAccess, cover common roles.
Their permissions are often broader than a single workload needs. AdministratorAccess allows every action on every resource. AWS can also add permissions to a managed policy when it launches new features, so an identity may gain access without any change on your side.
Customer managed policies
You write these policies, and they live in your account. One policy can be attached to many identities, so a single edit updates all of them.
A customer managed policy keeps up to five versions. Exactly one version is the default, and the default is the one that is evaluated. You can add a new version and make it the default. You can also set an older version as the default to roll back, without rewriting the document.
If you already have five versions, delete one before creating another. A managed policy cannot be deleted while it is attached to any identity. You must detach it everywhere first.
Inline policies
An inline policy is embedded in one user, group, or role. It has no ARN of its own and cannot be shared. Deleting the identity deletes the policy.
Use inline policies when the permissions are tied to one identity and should never be attached elsewhere. In most other cases AWS recommends managed policies. They can be reused, versioned, and audited in one place.
A worked example
This policy allows listing one bucket and reading its objects:
{
"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 policy has two statements because the two actions apply to different resource types. s3:ListBucket acts on the bucket, so its ARN has no path. s3:GetObject acts on objects, so its ARN ends in /*. Putting both actions in one statement with one ARN would leave one of them without a matching resource, and that action would be implicitly denied.
Save the document as s3-read.json. The next sections attach it as a customer managed policy and then as an inline policy.
Attaching a policy
First, create the customer managed policy and attach it to a group:
aws iam create-policy \
--policy-name BucketReadOnly \
--policy-document file://s3-read.json
aws iam attach-group-policy \
--group-name Analysts \
--policy-arn arn:aws:iam::111122223333:policy/BucketReadOnlyEvery member of Analysts now has these permissions. To attach an AWS managed policy, use the same command with its ARN:
aws iam attach-role-policy \
--role-name ReadOnlyAuditor \
--policy-arn arn:aws:iam::aws:policy/ReadOnlyAccessAn inline policy uses a put-*-policy command instead, and you give it a name that is unique within that identity:
aws iam put-user-policy \
--user-name dev-user \
--policy-name BucketReadInline \
--policy-document file://s3-read.jsonTo change a customer managed policy, create a new version and make it the default:
aws iam create-policy-version \
--policy-arn arn:aws:iam::111122223333:policy/BucketReadOnly \
--policy-document file://s3-read-v2.json \
--set-as-defaultTo roll back, run aws iam set-default-policy-version with the earlier version ID, for example v1.
Limits that affect design
Quotas change over time, so check the current IAM quotas page before you rely on them. At the time of writing:
- A user, group, or role can have 10 managed policies attached by default. You can raise this quota to 20.
- A customer managed policy document can be up to 6,144 characters, not counting whitespace.
- Inline policies have a combined size limit per identity, and the limit differs for users, groups, and roles.
When a policy gets large, split it by purpose, such as one policy for storage and one for logging. Splitting also makes each policy easier to review. Avoid replacing specific actions with * just to save space.
How multiple policies combine
An identity can have several policies at once: managed and inline ones attached directly, plus those inherited from groups. AWS does not merge them into one document. It evaluates all applicable policies together, and an Allow in any of them can grant access unless an explicit Deny applies. A later chapter covers the full evaluation order.
In practice this means you can add permissions by attaching another policy. To remove a permission, you must find and remove it from every policy that grants it.
Common errors
- Using one ARN for both bucket and object actions. The bucket-level action fails or the object action fails, depending on which ARN you chose.
- Editing an AWS managed policy. You cannot. Copy its content into a customer managed policy and change the copy.
- Expecting an inline policy to survive its identity. It is deleted with the user, group, or role. Recreating the identity does not bring it back.
- Trying to delete an attached managed policy. The delete call fails until you detach the policy from every user, group, and role.
- Treating a managed policy as fixed. An AWS managed policy can gain permissions, and a customer managed policy can change when any administrator sets a new default version. Review changes in CloudTrail.
- Reaching for
AdministratorAccessto get unblocked. It works, but it removes the benefit of scoped permissions.
Exercise
A policy contains one statement with "Action": ["s3:ListBucket", "s3:GetObject"] and "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*". Users report that they can read objects but cannot list the bucket. Why, and how do you fix it?
Show answer
s3:ListBucket is a bucket-level action, and its resource is the bucket ARN arn:aws:s3:::amzn-s3-demo-bucket. The ARN ending in /* matches only objects, so the list request has no matching Allow and is implicitly denied. Add a second statement that allows s3:ListBucket on arn:aws:s3:::amzn-s3-demo-bucket, and leave s3:GetObject on the /* ARN.
Exercise
You need the same permissions for 30 users and expect to adjust them often. Which form of policy fits best, and what do you do to roll back a bad change?
Show answer
A customer managed policy attached to a group that contains the users. One edit then applies to everyone. To roll back, set a previous policy version as the default with aws iam set-default-policy-version. A managed policy keeps up to five versions.