Put CloudFront in front of the bucket and turn on origin access control (OAC). OAC means CloudFront signs its requests to S3, so only CloudFront can read the files.
Steps:
- Create a CloudFront distribution with the S3 bucket as its origin (the place CloudFront fetches files from).
- Enable OAC on that origin.
- In the bucket policy, allow
s3:GetObjectfor thecloudfront.amazonaws.comservice principal. Add a condition thatAWS:SourceArnmatches your distribution, so no other distribution can use it. - Keep Block Public Access on. Users can then reach files only through CloudFront.
What CloudFront adds:
- It caches files at edge locations near users.
- It serves over HTTPS with your certificate from ACM (AWS Certificate Manager).
- It reduces the number of requests to S3.
For the React SPA (single-page app):
- Set the default root object to
index.html. - Map 403 and 404 errors to
/index.html, so client-side routing works on deep links. - Give built files content-hashed names with long cache times.
- Give
index.htmla short TTL (cache lifetime), or invalidate it on each deploy.
OAC replaces the older origin access identity (OAI). OAC also supports buckets encrypted with SSE-KMS.
- Use the S3 bucket as the origin, with origin access control turned on.
- The bucket policy allows cloudfront.amazonaws.com, limited to your distribution by AWS:SourceArn.
- Block Public Access stays on, so files are reachable only through CloudFront.
- CloudFront caches at the edge and serves HTTPS with an ACM certificate.
- The SPA falls back to index.html, and hashed assets get long TTLs.
Read more