Sub topic 6
Writing the bucket policy that trusts your distribution
This chapter assumes you know how Origin Access Control (OAC) works and that you have a distribution with an S3 REST endpoint origin. You should also be able to read a JSON IAM policy. Here you write the bucket policy that lets only your distribution read objects.
Why the policy is needed
OAC makes CloudFront sign each request to S3 with SigV4. Signing does not grant access. The bucket must contain a policy that allows the CloudFront service to read objects. Without that policy, S3 returns 403 Access Denied to CloudFront, and viewers see an error.
Because the policy grants access to a service and not to the public, you can leave S3 Block Public Access fully enabled. Block Public Access blocks public policies. A policy limited to a service principal and a specific source ARN is not public.
The policy
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowCloudFrontServicePrincipalReadOnly",
"Effect": "Allow",
"Principal": {
"Service": "cloudfront.amazonaws.com"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*",
"Condition": {
"StringEquals": {
"AWS:SourceArn": "arn:aws:cloudfront::111122223333:distribution/EDFDVBD6EXAMPLE"
}
}
}
]
}Each part has a job:
| Element | Purpose |
|---|---|
| `Principal` | Names the CloudFront service, not a user or role. |
| `Action` | `s3:GetObject` allows reading objects and nothing else. |
| `Resource` | The `/*` suffix applies the rule to objects in the bucket. |
| `Condition` | Limits the grant to one distribution, identified by its ARN. |
The condition matters most. Without it, any CloudFront distribution in any account could read your bucket through the same service principal. The AWS:SourceArn key pins access to the distribution whose account ID and distribution ID appear in the ARN.
Applying the policy
Replace the bucket name, account ID, and distribution ID with your own values. Save the JSON as policy.json, then apply it:
aws s3api put-bucket-policy --bucket amzn-s3-demo-bucket --policy file://policy.jsonThe console can generate this policy for you when you create the OAC. If you create the distribution with CloudFormation, you must write the policy yourself, and it must reference the distribution after the distribution exists.
Request flow
Common errors
- A wrong ARN. A typo in the account ID or distribution ID produces a 403 for every request. Copy the ARN from the distribution's detail page.
- A missing
/*in the Resource. Usingarn:aws:s3:::amzn-s3-demo-bucketalone covers the bucket, not its objects, soGetObjectis denied. - Missing
s3:ListBucket. With onlyGetObject, a missing key returns 403 and not 404. If you want a true 404, add a second statement that allowss3:ListBucketon the bucket ARN, with the same condition. - A website endpoint origin. OAC does not work with the S3 website endpoint. The policy above applies only to the REST endpoint.
- SSE-KMS objects. If the bucket uses a customer managed KMS key, the key policy must also allow the CloudFront service principal to decrypt.
Exercise
Your distribution returns 403 for every object, and the bucket policy has the right principal and action. Name two things in the policy to check.
Show answer
Check that the Resource ends in /* so it covers objects, and that the AWS:SourceArn value matches the distribution's account ID and distribution ID exactly. Also confirm the origin uses the REST endpoint, not the website endpoint.