CloudFront OAI

Difficulty: Intermediate

Question

What is a CloudFront Origin Access Identity (OAI), and how does it compare to the newer Origin Access Control (OAC)?

Answer

An Origin Access Identity (OAI) is a special CloudFront identity you grant read access to in an S3 bucket policy, so the bucket can stay private and users can fetch files only through CloudFront, not through direct S3 URLs. OAI is now the legacy approach: since 2022 AWS recommends Origin Access Control (OAC), which does the same job with SigV4-signed requests and supports more scenarios such as SSE-KMS encrypted objects.

Why you lock S3 behind CloudFront

If your React app's static build, product images or invoice PDFs sit in a public bucket, anyone can bypass CloudFront and hit bucket.s3.ap-south-1.amazonaws.com directly. That means:

  • No WAF protection, geo restriction or signed URL enforcement.
  • Higher S3 data transfer costs instead of cheaper CloudFront delivery.
  • No caching benefit and possible scraping.

Restricting the origin forces every request through the CDN, where your security and caching rules live.

How OAI works

  1. You create an OAI and attach it to the S3 origin in your distribution.
  2. The bucket policy grants s3:GetObject to the principal arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity <ID>.
  3. You keep Block Public Access on and remove any public statements.
  4. CloudFront fetches objects as that identity; direct requests from the internet get 403 AccessDenied.

How OAC improves on it

With OAC, CloudFront signs each origin request with SigV4 as the cloudfront.amazonaws.com service principal, and the bucket policy uses a condition on AWS:SourceArn to allow only your specific distribution.

FeatureOAI (legacy)OAC (recommended)
SSE-KMS encrypted objectsNot supportedSupported (grant the key to CloudFront)
Dynamic requests (PUT, POST, DELETE) to S3LimitedSupported
All Regions, including newer opt-in RegionsNoYes
Scope of accessAny distribution using that OAIPinned to one distribution ARN
Other origin typesS3 onlyS3, plus Lambda function URLs and some media origins

Common mistakes

  • Using the S3 website endpoint as the origin. Neither OAI nor OAC works with website endpoints, because those are treated as custom HTTP origins. Use the REST endpoint and handle SPA routing with a default root object and custom error responses, or a CloudFront Function.
  • Forgetting the KMS key policy when objects are encrypted with a customer managed key, which leads to 403s through CloudFront.
  • Leaving an old public bucket policy statement in place, which defeats the whole setup.
  • Migrating from OAI to OAC without updating the bucket policy first; add the OAC statement, switch the origin, then remove the OAI statement.

If you are asked about OAI in an interview, describe it, then say you would use OAC for anything new.

Private S3 origin behind CloudFront. Direct S3 access is denied, while CloudFront's signed request is allowed by the bucket policy.
  1. User → S3 bucket: GET object directly
  2. S3 bucket → User: 403 AccessDenied
  3. User → CloudFront: GET /assets/app.js
  4. CloudFront → S3 bucket: Signed request (OAC or OAI)
  5. S3 bucket → CloudFront: Object returned
  6. CloudFront → User: Cached response from edge

Code examples

Bucket policy for Origin Access Control

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowCloudFrontOACRead",
      "Effect": "Allow",
      "Principal": { "Service": "cloudfront.amazonaws.com" },
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::my-frontend-prod/*",
      "Condition": {
        "StringEquals": {
          "AWS:SourceArn": "arn:aws:cloudfront::111122223333:distribution/E2ABCDEF123456"
        }
      }
    }
  ]
}

Only requests signed by this specific distribution are allowed; any other CloudFront distribution or direct S3 request is denied.

Legacy OAI bucket policy statement

{
  "Sid": "AllowOAIRead",
  "Effect": "Allow",
  "Principal": {
    "AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity E1EXAMPLE123"
  },
  "Action": "s3:GetObject",
  "Resource": "arn:aws:s3:::my-frontend-prod/*"
}

OAI uses a special IAM-style principal; you will still see this in older stacks and should recognise it when migrating.

Key points

  • OAI lets CloudFront read a private S3 bucket so users cannot bypass the CDN.
  • OAC is the recommended replacement and signs requests with SigV4.
  • OAC supports SSE-KMS, all Regions, write methods and pins access to one distribution ARN.
  • Neither works with S3 static website endpoints; use the REST endpoint.
  • Keep S3 Block Public Access on when using OAI or OAC.

Common follow-up questions

  • How would you migrate a distribution from OAI to OAC without downtime?
  • How do you serve a single-page app from a private S3 bucket with client-side routing?
  • What are CloudFront signed URLs and signed cookies, and how do they differ from OAC?
  • Why do you get 403 errors through CloudFront for KMS-encrypted objects?

Interview tips

  • Answer the OAI question, then immediately mention OAC as the current best practice; it shows you are up to date.
  • Explain the security reason (bypassing WAF and geo rules), not just 'it makes the bucket private'.
  • Mention the website-endpoint gotcha; it is a very common real-world mistake.

Concepts covered

Origin Access Identity, Origin Access Control, S3 Bucket Policy, CloudFront, SigV4

More AWS & Cloud Fundamentals questions