Sub topic 7
Controlling access with IAM, bucket policies and Block Public Access
This chapter assumes you know what an AWS account, an IAM user and an IAM role are. You should also be comfortable reading JSON and running AWS CLI commands. By the end you will know which of three access controls applies to a request and how they combine. You will also know how to lock a bucket down and how to debug an AccessDenied.
The three layers
S3 access is decided by several mechanisms that are evaluated together:
- Identity-based policies (IAM policies) attach to an IAM user, group or role. They say what that identity may do.
- Resource-based policies (bucket policies) attach to the bucket. They say who may do what to the bucket and its objects, including identities in other accounts and anonymous callers.
- Block Public Access (BPA) is a set of four switches that override policies and ACLs to stop public exposure. It can be set on a bucket or for a whole account.
Access control lists (ACLs) are a legacy fourth mechanism. New buckets have ACLs disabled through the Bucket owner enforced Object Ownership setting, and AWS recommends leaving them disabled. This chapter does not use ACLs.
How a request is evaluated
Every request starts as an implicit deny. The rules then run in this order:
- An explicit
Denyin any applicable policy ends evaluation with a denial. - Otherwise, an
Allowis needed from the right policy type. - If no policy allows the request, it stays denied.
For a caller in the same account as the bucket, an allow in either the IAM policy or the bucket policy is enough. For a caller in a different account, both sides must allow: the bucket policy in the bucket owner's account and an IAM policy in the caller's account.
BPA is drawn as its own step because it can turn a policy that grants public access into a policy that does nothing. It blocks public grants. It does not block access that a policy gives to specific named principals.
IAM policies: what an identity may do
An IAM policy names actions and resources. For S3 you must keep two resource types apart:
- The bucket ARN,
arn:aws:s3:::my-example-bucket-123456789012, is the target of bucket-level actions such ass3:ListBucket. - The object ARN,
arn:aws:s3:::my-example-bucket-123456789012/*, is the target of object-level actions such ass3:GetObjectands3:PutObject.
This policy lets an application read and write under one prefix and list only that prefix:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListOnlyReportsPrefix",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::my-example-bucket-123456789012",
"Condition": {
"StringLike": { "s3:prefix": ["reports/*"] }
}
},
{
"Sid": "ReadWriteReportObjects",
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": "arn:aws:s3:::my-example-bucket-123456789012/reports/*"
}
]
}The s3:prefix condition applies to listing, because a prefix is part of the list request and not an object. Without the condition the identity could list every key in the bucket.
Attach this to a role when the caller is an EC2 instance, a Lambda function or another AWS service. Roles give temporary credentials, so no long-lived access key sits in code or on disk. Use IAM users with access keys only when no role option exists.
Bucket policies: who may use this bucket
A bucket policy has the same JSON shape as an IAM policy, with one extra element: Principal. It names who the statement applies to. Bucket policies are limited to 20 KB, and a bucket has at most one.
Typical uses are:
- Granting another AWS account access.
- Granting an AWS service access, for example CloudFront or a log delivery service.
- Enforcing rules for everyone, such as requiring HTTPS.
This statement grants a role in another account read access to one prefix:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PartnerReadReports",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::210987654321:role/partner-reader"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::my-example-bucket-123456789012/reports/*"
}
]
}For this to work, the role in account 210987654321 also needs an IAM policy allowing s3:GetObject on the same resource. This is the cross-account rule from earlier.
Apply a policy file with the CLI:
aws s3api put-bucket-policy \
--bucket my-example-bucket-123456789012 \
--policy file://policy.jsonThe put-bucket-policy call replaces the whole policy. To add a statement, fetch the current policy with get-bucket-policy, edit it, and put the full document back.
The HTTPS-only policy from the earlier example in this tutorial is a good model for a guardrail: one Deny statement with a condition. Denies are useful for guardrails because nothing else can override them.
Block Public Access
BPA has four settings. Each one covers a different route to public exposure.
| Setting | What it does |
|---|---|
| `BlockPublicAcls` | Rejects requests that add a public ACL to a bucket or object. |
| `IgnorePublicAcls` | Ignores any public ACLs that already exist. |
| `BlockPublicPolicy` | Rejects a bucket policy that would make the bucket public. |
| `RestrictPublicBuckets` | Limits access through an existing public policy to AWS services and authorized users in the account. |
Set all four on every bucket unless you are deliberately hosting public content. You can also set them at the account level, which applies to every bucket in the account, including ones created later:
aws s3control put-public-access-block \
--account-id 123456789012 \
--public-access-block-configuration BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=trueAn effective setting is the stricter of the account and bucket values. A bucket cannot loosen an account-level block.
Choosing the right layer
| Need | Use |
|---|---|
| Give an application in your account access to specific prefixes | IAM policy on its role |
| Give another account access | Bucket policy plus an IAM policy in that account |
| Require HTTPS or encryption for all writes | Bucket policy with a `Deny` and a condition |
| Make sure nothing becomes public by accident | Block Public Access, ideally at account level |
| Let someone download one private object for a short time | Presigned URL (covered in a later chapter) |
A useful rule: put "what can this identity do" in IAM, and put "what must always be true of this bucket" in the bucket policy.
Common errors
AccessDeniedonListBucketbutGetObjectworks. The policy has the bucket ARN and object ARN mixed up. List needs the bucket ARN. Get needs the/*ARN.AccessDeniedwhen putting a bucket policy. WithBlockPublicPolicyon, a policy withPrincipal: "*"and no restricting condition is rejected. This is the setting working as intended.- Cross-account access still fails. Only one side was configured. Check both the bucket policy and the caller's IAM policy.
- A deny you cannot find. Look for a
Denyin the bucket policy, an IAM permissions boundary, a service control policy in AWS Organizations, or a session policy. Any of these can override an allow. - Objects written by another account cannot be read by the bucket owner. This happens on buckets with ACLs enabled. Keep Object Ownership at Bucket owner enforced to avoid it.
- Encrypted objects fail with
AccessDenied. If objects use a customer-managed KMS key, the caller also needs permission on that key. The S3 permission alone is not enough.
Verifying what you set
Check the BPA state and the policy status from the CLI:
aws s3api get-public-access-block \
--bucket my-example-bucket-123456789012
aws s3api get-bucket-policy-status \
--bucket my-example-bucket-123456789012get-bucket-policy-status returns IsPublic as true or false. IAM Access Analyzer for S3 goes further. It lists buckets that are reachable from outside your account and shows which policy grants that access. To test a specific identity, the IAM policy simulator evaluates a policy against an action and resource without making the call.
Exercise
A role in your account has an IAM policy allowing s3:GetObject on arn:aws:s3:::my-example-bucket-123456789012/*. The bucket policy has a Deny statement for s3:GetObject when aws:SecureTransport is false. The role calls GetObject over plain HTTP. Is the request allowed, and why?
Show answer
It is denied. An explicit Deny in any applicable policy overrides every Allow, so the IAM allow does not help. The same request over HTTPS would be allowed.
Exercise
You need a partner account to read objects under the exports/ prefix, and you want to guarantee that no policy you write later can make the bucket public. Which controls do you set up?
Show answer
Add a bucket policy statement that allows the partner's principal s3:GetObject on arn:aws:s3:::BUCKET/exports/*, and have the partner attach a matching IAM policy to their role. Then turn on all four Block Public Access settings, ideally at the account level. The named-principal grant is not public, so BPA does not block it.