
How to Serve a Private S3 Bucket Through CloudFront ?
Learn how to serve files from a private Amazon S3 bucket using CloudFront Origin Access Control. This guide explains the architecture, required permissions, bucket policy configuration, and common mistakes to avoid when setting up secure content delivery.
Introduction
Amazon S3 is a common choice for storing images, documents, videos, and other application assets. Amazon CloudFront can deliver these files through its global edge network, helping reduce latency for users in different locations.
But there is one important question: how can CloudFront access files in an S3 bucket without making the bucket publicly accessible?
A common mistake is to make the S3 bucket public just to allow CloudFront to retrieve its objects. This solves the access problem, but it also exposes the files through their direct S3 URLs.
A better approach is to keep the bucket private and use CloudFront Origin Access Control (OAC) to authorize requests from CloudFront to S3.
In this article, we will configure this setup and understand how each part contributes to security.
1. Understand the architecture
The setup uses three main components:
- Amazon S3, which stores the files and keeps them private.
- Amazon CloudFront, which receives requests from users and delivers the requested content.
- Origin Access Control (OAC), which allows CloudFront to make authenticated requests to the S3 origin.
The request flow looks like this:
User's Browser | | HTTPS request vAmazon CloudFront | | Signed origin request vPrivate S3 Bucket | | Requested object vAmazon CloudFront | vUser's BrowserCloudFront uses OAC to sign requests sent to S3. The S3 bucket policy grants the CloudFront service permission to read the objects for the specified distribution.
The bucket does not need public read access.
AWS recommends OAC for securing S3 origins instead of the older Origin Access Identity (OAI) approach.
Official documentation: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html
2. Create a private S3 bucket
Start by creating an S3 bucket to store your application files.
For example, you might use a bucket named
my-private-app-assets .Upload a file such as:
images/logo.pngdocuments/report.pdfvideos/tutorial.mp4Keep S3 Block Public Access enabled. For a standard private bucket, you should not need to grant public read permissions.
For a new bucket, the default Object Ownership setting, Bucket owner enforced, is suitable for OAC.
You can upload files through the AWS Console, AWS CLI, SDKs, or your application's backend.
For example, a Laravel application can upload a file to S3 using an IAM role or appropriately scoped AWS credentials without making the object publicly readable.
At this point, the files are stored in S3, but users cannot retrieve them unless an authorized identity or service has permission.
3. Create a CloudFront distribution
Open the CloudFront console and create a distribution.
Configure the origin to use your regular S3 bucket endpoint, such as:
my-private-app-assets.s3.us-east-1.amazonaws.comReplace the example bucket name and region with your actual values.
Important: Choose the regular S3 bucket origin, not the S3 static website endpoint. OAC does not support S3 website endpoints.
Next, configure the origin access settings.
Select Origin access control settings and create a new OAC if you do not already have one.
Use these settings:
Setting | Value |
|---|---|
Origin type | Amazon S3 |
Origin access | Origin access control |
Signing behavior | Sign requests |
Signing protocol | SigV4 |
The recommended signing behavior is Sign requests, which ensures CloudFront signs its origin requests.
For a typical read only content distribution, configure the allowed viewer methods to GET and HEAD unless your application needs other methods.
You can also configure HTTPS redirection, a custom domain, and a suitable cache policy.
Save the distribution configuration.
4. Allow CloudFront to read from S3
CloudFront needs explicit permission to retrieve objects from the bucket.
The S3 bucket policy can grant that permission to the CloudFront service principal while restricting access to a particular distribution.
Here is an example read only bucket policy:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowCloudFrontReadOnly", "Effect": "Allow", "Principal": { "Service": "cloudfront.amazonaws.com" }, "Action": "s3:GetObject", "Resource": "arn:aws:s3:::my-private-app-assets/*", "Condition": { "StringEquals": { "AWS:SourceArn": "arn:aws:cloudfront::123456789012:distribution/E123EXAMPLE" } } } ]}Replace the following example values:
-
my-private-app-assetswith your S3 bucket name.
-
123456789012with your AWS account ID.
-
E123EXAMPLEwith your CloudFront distribution ID.
Apply the policy under the bucket's Permissions section.
The policy grants
s3:GetObject only for objects in the specified bucket. The AWS:SourceArn condition restricts this permission to requests associated with the specified CloudFront distribution.If your application also needs to list the bucket or perform uploads and deletions, those operations require separate permissions. Do not add them unless they are needed.
AWS documentation: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html
5. Test the configuration
Once the CloudFront distribution has finished deploying, test access to one of your files.
Suppose the uploaded file is:
images/logo.pngYour CloudFront URL will look like this:
https://d123example.cloudfront.net/images/logo.pngReplace the example domain with your distribution's actual domain.
Open the URL in a browser. If the configuration is correct, CloudFront should return the image.
Now test the direct S3 URL:
https://my-private-app-assets.s3.us-east-1.amazonaws.com/images/logo.pngAn anonymous request should fail because the bucket does not grant public read access.
This test verifies two different things:
- CloudFront can retrieve the object through its authorized origin request.
- An unauthenticated user cannot bypass CloudFront by accessing the object directly through S3.
If the CloudFront request returns an error, check the bucket policy, the origin configuration, the object key, and the distribution deployment status.
6. Understand what OAC does and does not protect
This distinction matters when building production applications.
OAC protects the S3 origin. It does not automatically make CloudFront URLs private.
If someone knows the CloudFront URL for an object and the distribution allows public viewer requests, they may still retrieve that object through CloudFront.
For example, suppose your application stores invoices in S3 and serves them through CloudFront.
OAC prevents anonymous users from bypassing CloudFront to access S3 directly. However, it does not determine whether a particular application user is authorized to view a particular invoice.
For private user specific content, you can combine OAC with CloudFront signed URLs or signed cookies. Your backend authenticates the user and issues time limited access according to your application's authorization rules.
For example:
User logs in | vBackend checks permissions | vBackend issues a signed CloudFront URL | vUser accesses the permitted fileThis separates origin security from user authorization.
AWS documentation: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/PrivateContent.html
7. Common mistakes to avoid
Making the S3 bucket public
Do not enable public access simply to make CloudFront work. Use OAC and a bucket policy that grants the required permissions.
Using the S3 website endpoint
OAC works with a regular S3 bucket origin, not an S3 static website endpoint. If you need private content, configure CloudFront with the regular bucket endpoint.
Forgetting the distribution restriction
A bucket policy that grants CloudFront access without the appropriate distribution restriction may allow more access than intended. Include the
AWS:SourceArn condition for your distribution.Expecting OAC to authenticate application users
OAC authenticates CloudFront's requests to S3. It does not replace your application's login, roles, or resource authorization checks.
Ignoring cached content
CloudFront can continue serving an object from its cache after the object or its permissions have changed. When immediate removal is important, consider invalidating cached objects and reviewing how access is controlled. An invalidation alone is not a substitute for proper authorization.
Granting unnecessary permissions
For a content delivery distribution,
s3:GetObject is often enough. Avoid granting s3:* or write permissions when the distribution only needs to serve files.8. When should you use this architecture?
A private S3 bucket behind CloudFront is a good fit for:
- Images and static assets used by web applications.
- Publicly viewable downloads where direct S3 access should remain blocked.
- Media files delivered through a CDN.
- Documents that require additional access controls at the CloudFront layer.
- Application assets that need centralized delivery through a custom domain.
For content that requires per user authorization, use OAC together with an appropriate access mechanism, such as signed URLs or signed cookies, rather than relying on OAC alone.
Conclusion
Serving a private S3 bucket through CloudFront does not require making the bucket public.
With Origin Access Control, CloudFront signs its requests to S3, and a carefully scoped bucket policy grants the distribution permission to retrieve objects. This keeps the origin private while allowing content to be delivered through CloudFront.
The most important point is to understand the two security layers: OAC controls access from CloudFront to S3, while viewer authorization controls who can retrieve content through CloudFront.
Keeping those responsibilities separate makes it easier to build secure content delivery into web applications without exposing the underlying storage bucket.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article