Sub topic 5
Keeping the bucket private with Origin Access Control
This chapter assumes you know basic S3 concepts, can read IAM JSON policies, and can run AWS CLI commands. It also assumes you have a distribution that uses an S3 bucket's REST endpoint as its origin.
Why the bucket should stay private
If viewers can read your bucket directly, they can skip CloudFront. That bypasses your cache, your signed URLs, and your response headers. It also raises your S3 request costs.
The fix is to keep S3 Block Public Access on and let only your distribution read objects. Origin Access Control (OAC) does this.
What OAC does
OAC is a CloudFront setting that tells the distribution to sign each request it sends to S3 with Signature Version 4 (SigV4). S3 sees a request from the CloudFront service. It then checks the bucket policy to decide whether to allow it.
OAC works with the S3 REST endpoint, the one shaped like bucket.s3.region.amazonaws.com. It does not work with the S3 website endpoint. That endpoint only accepts public requests, so it cannot be private.
OAC replaces the older Origin Access Identity (OAI). Use OAC for new distributions. It supports all S3 regions, SSE-KMS encrypted objects, and requests that change objects, such as PUT and DELETE.
Creating the OAC
The CloudFormation excerpt below defines the OAC and attaches it to the origin. The bucket resource SiteBucket is assumed to be defined elsewhere.
Resources:
SiteOac:
Type: AWS::CloudFront::OriginAccessControl
Properties:
OriginAccessControlConfig:
Name: site-oac
OriginAccessControlOriginType: s3
SigningBehavior: always
SigningProtocol: sigv4
SiteDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Enabled: true
DefaultRootObject: index.html
Origins:
- Id: s3-origin
DomainName: !GetAtt SiteBucket.RegionalDomainName
OriginAccessControlId: !GetAtt SiteOac.Id
S3OriginConfig:
OriginAccessIdentity: ''
DefaultCacheBehavior:
TargetOriginId: s3-origin
ViewerProtocolPolicy: redirect-to-https
CachePolicyId: 658327ea-f89d-4fab-a63d-7e88639e58f6Three settings matter here:
SigningBehavior: alwayssigns every origin request. This is the recommended value.SigningProtocol: sigv4is the only protocol OAC supports.OriginAccessIdentity: ''stays empty because the origin uses OAC, not OAI.
The CachePolicyId is the managed CachingOptimized policy. A later chapter covers cache policies.
Granting access in the bucket policy
Creating the OAC does not grant access by itself. You must also add a bucket policy that trusts your distribution. The next chapter covers this policy in detail, so here is the short version: it allows the CloudFront service principal to call s3:GetObject, and it limits that permission to one distribution with an AWS:SourceArn condition.
{
"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"
}
}
}
]
}Common errors
| Symptom | Likely cause |
|---|---|
| 403 AccessDenied from CloudFront | The bucket policy is missing, or its `AWS:SourceArn` names a different distribution. |
| 403 for a key that does not exist | The policy grants only `s3:GetObject`. S3 returns 403 instead of 404 for missing keys unless the caller also has `s3:ListBucket`. |
| 403 on SSE-KMS objects | The KMS key policy does not allow the CloudFront service principal to use the key. |
| OAC has no effect | The origin points to the website endpoint instead of the REST endpoint. |
Exercise
Your distribution uses OAC, but requests return 403 for every object. The bucket policy allows s3:GetObject to cloudfront.amazonaws.com with an AWS:SourceArn condition. Name two things to check.
Show answer
Check that the AWS:SourceArn value exactly matches the ARN of the distribution that is sending the requests, including the account ID and distribution ID. Also check that the origin uses the S3 REST endpoint with the OAC attached, not the website endpoint. If the objects use SSE-KMS, also check that the KMS key policy allows CloudFront.