Sub topic 3
Reading a policy document
This chapter assumes you know what an AWS account, an IAM role, an API action, and an ARN are, and that you can read JSON. Every IAM policy is a JSON document with the same few parts. Once you can read those parts, you can read any policy in the rest of this tutorial.
The shape of a policy
A policy has a Version and a list of Statement objects. Each statement is one rule. AWS evaluates the statements together, not one at a time in order.
Versionshould be"2012-10-17". This is the current policy language version. An older version,2008-10-17, lacks features such as policy variables.Statementis a single object or an array of objects.Sidis an optional label for a statement. It helps you find the statement when you debug.
The elements of a statement
| Element | What it answers | Notes |
|---|---|---|
| `Effect` | Allow or deny? | Must be `Allow` or `Deny`. |
| `Action` | Which API operations? | Format is `service:Operation`, such as `s3:GetObject`. Wildcards like `s3:*` work. |
| `Resource` | On which objects? | Written as ARNs. Wildcards work. |
| `Principal` | Who? | Only in resource-based policies and role trust policies. |
| `Condition` | Under what circumstances? | Optional. Covered in a later chapter. |
Identity-based policies have no Principal. The principal is whoever the policy is attached to. Resource-based policies must name one.
Reading an identity-based policy
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ReadWriteReports",
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": "arn:aws:s3:::example-reports-bucket/*"
}
]
}Read it as one sentence: "Allow the attached identity to call s3:GetObject and s3:PutObject on any object in example-reports-bucket."
The /* at the end of the ARN matters. The ARN arn:aws:s3:::example-reports-bucket names the bucket itself. The ARN arn:aws:s3:::example-reports-bucket/* names the objects inside it. GetObject and PutObject act on objects, so the policy needs the second form.
Reading a resource-based policy
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowReportReaderRoleFromOtherAccount",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::222233334444:role/ReportReader" },
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example-reports-bucket/*"
}
]
}This bucket policy adds a Principal. It reads: "Allow the role ReportReader in account 222233334444 to call s3:GetObject on objects in this bucket." This policy lives on the bucket, in account 111122223333. Because the role is in a different account, the role's own identity policy must also allow the action. Later chapters on cross-account requests explain why.
Reading a deny statement
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyInsecureTransport",
"Effect": "Deny",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::example-reports-bucket",
"arn:aws:s3:::example-reports-bucket/*"
],
"Condition": { "Bool": { "aws:SecureTransport": "false" } }
}
]
}Here Effect is Deny, and the Condition limits when it applies. The statement reads: "Deny every S3 action on the bucket and its objects when the request does not use TLS." The Resource list has two ARNs because some S3 actions apply to the bucket and others to its objects.
A reading routine
Read each statement in this order:
Effect: is this a grant or a deny?Principal: if present, who is named? If absent, the attached identity.Action: which operations match?Resource: which ARNs match?Condition: when does the statement apply?
A statement applies to a request only if the action, the resource, and the condition all match. If any one fails, the statement does nothing for that request.
Common reading mistakes
- Putting a bucket-level ARN where an object-level ARN is needed, or the reverse.
- Expecting
Principalin an identity-based policy. IAM rejects it there. - Assuming a skipped statement denies the request. A statement that does not match has no effect. The next chapter explains what happens when nothing matches.
Exercise
A policy statement has Effect Allow, Action "s3:PutObject", and Resource "arn:aws:s3:::example-reports-bucket". A user with this policy calls PutObject on example-reports-bucket/2026/summary.csv. Does the statement apply to the request? Why or why not?
Show answer
No. The Resource names only the bucket, not the objects in it. PutObject acts on an object, so the Resource must be "arn:aws:s3:::example-reports-bucket/*" (or a matching object ARN). The Resource element does not match, so the statement is skipped.