Sub topic 5
Anatomy of a policy document
This chapter assumes you know what an IAM principal is, that policies are JSON documents, and that you can read basic JSON. You will learn each element of an identity-based policy and how the elements fit together.
The basic shape
An IAM policy is a JSON document with a Version and a Statement. The Statement holds one statement or an array of statements. Each statement is a separate permission rule.
{
"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 elements
| Element | Purpose |
|---|---|
| `Version` | The policy language version. Use `2012-10-17`. Older versions lack features such as policy variables. |
| `Statement` | The container for one or more statements. |
| `Sid` | An optional label for a statement. It must be unique within the policy. |
| `Effect` | `Allow` or `Deny`. |
| `Action` | The API operations the statement covers, written as `service:Operation`. Wildcards such as `s3:Get*` are allowed. |
| `Resource` | The resources the statement applies to, identified by ARN. |
| `Condition` | Optional tests that must be true for the statement to apply. |
| `Principal` | Who the statement applies to. It appears in resource-based and trust policies, not in identity-based policies. |
An identity-based policy has no Principal because the principal is whoever the policy is attached to.
Bucket ARNs and object ARNs
The example above uses two statements because S3 actions apply to different resource types. s3:ListBucket acts on the bucket, so its ARN is arn:aws:s3:::amzn-s3-demo-bucket. s3:GetObject acts on objects, so its ARN ends in /*.
A common error is putting both actions in one statement with only the object ARN. The listing then fails, because the bucket ARN is never matched. Always check the service's documentation for the resource type each action supports.
How a statement is read
AWS reads a statement as one sentence: "For this Effect, on these Actions, against these Resources, when the Condition is true." If any part does not match the request, the statement does not apply to it.
Variations you will meet
- Arrays.
ActionandResourceaccept arrays, for example"Action": ["s3:GetObject", "s3:PutObject"]. - NotAction and NotResource. These match everything except the listed items. They are easy to misread, so prefer plain
ActionandResourcewhen you can. - Policy variables. A value such as
${aws:username}is replaced at evaluation time. This needsVersion2012-10-17. - Conditions. A
Conditionblock names an operator, a condition key, and a value. The next chapters cover them in detail.
Common errors
- Misspelling an action name. Some tools flag it, but a policy can still save and then never match.
- Omitting
Version. The policy then falls back to the older2008-10-17language and policy variables are not substituted. - Using the wrong ARN type, as described above.
- Adding
Principalto an identity-based policy. IAM rejects it.
Exercise
A policy has one statement that allows s3:ListBucket and s3:GetObject with the single resource arn:aws:s3:::amzn-s3-demo-bucket/*. Users can read objects but listing the bucket fails. Why, and how do you fix it?
Show answer
s3:ListBucket applies to the bucket ARN, arn:aws:s3:::amzn-s3-demo-bucket, which the pattern ending in /* does not match. Add a second statement (or list both ARNs) that allows s3:ListBucket on the bucket ARN.