How to Secure an Amazon S3 Website with CloudFront Origin Access Control (OAC)
Learn how to serve a static website securely using Amazon S3 and Amazon CloudFront. This step-by-step guide explains private S3 buckets, Origin Access Control (OAC), bucket policies, HTTPS delivery, and common configuration mistakes.
Introduction
When hosting a static website on AWS, Amazon S3 provides durable object storage, while Amazon CloudFront delivers content through a global content delivery network (CDN).
However, making an S3 bucket publicly accessible is not necessary when CloudFront can securely access the content on behalf of website visitors.
In this tutorial, we'll explore how to configure a static website using Amazon S3 and Amazon CloudFront with Origin Access Control (OAC). The goal is to keep the S3 bucket private while allowing visitors to access the website through CloudFront.
1. Understanding the Architecture
The architecture uses three main components:
- Amazon S3: Stores website files such as HTML, CSS, JavaScript, and images.
- Amazon CloudFront: Caches and delivers website content to users.
- Origin Access Control (OAC): Allows CloudFront to authenticate requests to a private S3 origin.
How does the request flow work?
- A visitor opens the CloudFront distribution URL.
- CloudFront checks whether the requested content is available in its cache.
- If the content is not cached, CloudFront sends an authenticated request to the S3 origin.
- S3 evaluates the bucket policy and returns the permitted object.
- CloudFront delivers the response to the visitor over HTTPS.
This approach separates public website delivery from direct public access to the storage bucket.
2. Prerequisites
Before starting, prepare the following:
- An AWS account with permission to configure S3 and CloudFront.
- A static website containing an
index.htmlfile.
- Basic knowledge of AWS regions, IAM permissions, and bucket policies.
For cost control, review current AWS pricing before creating resources. CloudFront requests, data transfer, and S3 storage or requests may incur charges.
3. Create an S3 Bucket
- Sign in to the AWS Management Console.
- Search for Amazon S3.
- Select Create bucket.
- Enter a globally unique bucket name.
- Choose the required AWS Region.
- Keep Block all public access enabled.
- Create the bucket.
- Open the bucket and select Upload.
- Upload
index.htmland your website's supporting assets.
For this architecture, use the regular S3 bucket origin rather than configuring the S3 static website endpoint as the CloudFront origin. OAC is designed for supported S3 origins, not S3 website endpoints.
4. Create a CloudFront Distribution
- Open the Amazon CloudFront console.
- Select Create distribution.
- Choose the S3 bucket as the origin.
- Select the bucket's S3 REST endpoint.
- Under Origin access, select Origin access control settings.
- Create a new OAC if one does not already exist.
- Configure CloudFront to sign origin requests using SigV4.
- Configure the default root object as
index.html.
- Select an appropriate viewer protocol policy, such as redirecting HTTP requests to HTTPS.
- Create the distribution.
Wait until the distribution is deployed before testing it.
5. Configure the S3 Bucket Policy
CloudFront needs explicit permission to retrieve objects from the private bucket.
After creating the distribution, review the OAC setup instructions in the CloudFront console. If AWS prompts you to update the bucket policy, use the generated policy or adapt the official example.
The policy should grant
s3:GetObject on the relevant objects to the CloudFront service principal, restricted to your specific distribution using the AWS:SourceArn condition.Use the actual distribution ARN and bucket ARN. Avoid granting public access through an unrestricted
"Principal": "*" policy.For stronger access control, grant only the actions and resources required by the distribution.
6. Test the Website
Once the distribution is deployed:
- Open the distribution details in CloudFront.
- Copy its distribution domain name.
- Open the domain in a browser using HTTPS.
- Verify that the page and its supporting assets load correctly.
- Test direct access to an S3 object without authorization.
The expected result is that the website loads through CloudFront while direct anonymous access to private S3 objects is denied.
If the site returns an error, check the distribution status, origin configuration, default root object, bucket policy, object names, and CloudFront cache.
Document your actual test results and include screenshots from your own deployment.
7. Security Best Practices
A secure deployment should follow these principles:
- Keep S3 Block Public Access enabled.
- Use OAC instead of relying on public bucket access.
- Limit the bucket policy to the intended CloudFront distribution.
- Use HTTPS for viewer connections.
- Never publish AWS access keys, account credentials, or other secrets.
- Review logs, monitoring, and costs as appropriate for the workload.
CloudFront OAC protects access to the S3 origin; it does not replace application-level authorization when a website contains private user data.
8. Common Problems and Troubleshooting
403 Forbidden: Check the bucket policy, OAC association, object permissions, and whether the requested object exists.
Access Denied for the root URL: Verify that the default root object is configured as
index.html and that the file exists at the expected S3 key.Changes are not appearing: Check CloudFront caching and invalidate the relevant paths when appropriate.
The S3 URL does not work publicly: This is expected when the bucket is private. Test through the CloudFront distribution instead.
9. Conclusion
Amazon S3 and CloudFront provide a practical foundation for delivering static websites. Using Origin Access Control helps keep the S3 origin private while allowing CloudFront to retrieve and distribute permitted content.
The important lesson is that cloud security should be part of the architecture from the beginning, rather than an additional step after deployment.
If you implement this architecture, include your own screenshots, verification results, and lessons learned to make the tutorial more useful to the AWS community.
References
- Amazon S3 documentation: https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html
- CloudFront Origin Access Control: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html
- CloudFront documentation: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Introduction.html
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article