Using Origin Access Identity to Secure Amazon S3 with CloudFront

Using Origin Access Identity to Secure Amazon S3 with CloudFront

In the world of static websites and media delivery on AWS, CloudFront and S3 pair up to provide fast, scalable content delivery. A common pattern is to keep S3 objects private and rely on CloudFront to serve content to end users. This is achieved through an origin access identity, or origin access identity. The origin access identity acts as a virtual user that CloudFront uses to retrieve objects from S3. With this setup, visitors access content through CloudFront, while direct access to the S3 bucket remains blocked. In this article, we explain what origin access identity is, how it works with S3 and CloudFront, and provide a practical, step‑by‑step guide to configure it. We’ll also cover common pitfalls and touch on the evolution toward newer origin access control while keeping OAI as a solid option for existing deployments.

What is an origin access identity?

An origin access identity, or origin access identity, is a dedicated CloudFront identity created to grant CloudFront permission to read private content in an Amazon S3 bucket. When you configure CloudFront with an origin access identity, the S3 bucket does not need to be publicly readable. CloudFront makes requests on behalf of end users using the identity, which keeps your bucket private while still allowing content to be delivered globally. The origin access identity itself is not a general access key for other AWS resources; its purpose is to authorize CloudFront to fetch objects from the bucket.

How origin access identity works with S3 and CloudFront

To visualize how origin access identity works, imagine a private library. The shelves exist, but direct access is restricted. CloudFront acts as a trusted courier that holds a special pass—the origin access identity—that permits it to fetch a book from the shelves when a reader requests it. The reader never touches the shelves directly. In AWS terms, CloudFront uses the origin access identity to request s3:GetObject from the S3 bucket, and the bucket policy grants this permission to the OAI’s canonical user ID. Direct requests to the S3 bucket without CloudFront are blocked by the bucket’s private configuration.

Key points to remember:

  • CloudFront with an origin access identity retrieves objects from S3 on demand.
  • The S3 bucket policy grants s3:GetObject to the OAI’s canonical user.
  • Direct access to the S3 bucket through its URL is blocked, ensuring privacy and control.

Step-by-step setup guide

  1. Identify or create an origin access identity in the CloudFront console. You will see a canonical user ID associated with this identity. Keep this ID handy, because you will use it in the bucket policy.
  2. Configure your S3 bucket to be private. Enable Block Public Access settings for the bucket to prevent public reads, unless you have a specific reason to expose certain objects publicly.
  3. Update the bucket policy to grant permission to the OAI. The policy should grant s3:GetObject to the OAI’s canonical user. A typical policy snippet looks like this:
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Principal": {
            "CanonicalUser": ""
          },
          "Action": "s3:GetObject",
          "Resource": "arn:aws:s3:::your-bucket-name/*"
        }
      ]
    }
    
  4. Attach the S3 bucket policy and verify that objects are accessible via the CloudFront URL but not via direct S3 URLs. You can test by requesting an object through the CloudFront domain and attempting a direct S3 link; the direct link should fail.
  5. Optionally, enable CloudFront caching behaviors and set appropriate TTLs to balance cost and content freshness. If you serve large media or large numbers of files, consider using longer TTLs for static assets and plan invalidation strategies for updates.
  6. Monitor access and logs. CloudFront access logs and S3 access logs help verify that only expected traffic is allowed. If you need per-user access, you can combine OAI with signed URLs or signed cookies to restrict to specific users or time windows.

Common pitfalls and troubleshooting

  • Forgetting to block public access at the bucket level. Without proper public access controls, someone could still fetch objects directly if the bucket policy is misconfigured.
  • Using the wrong OAI. If you create a new OAI and forget to update the bucket policy, CloudFront will fail to read objects from S3.
  • Mixing public and private assets in the same bucket. It is cleaner to keep private assets served via CloudFront with OAI separate from publicly accessible content.
  • Not validating the CloudFront behavior. Ensure your cache behavior uses the correct origin path and that the viewer protocol policy aligns with your security requirements (for example, redirect HTTP to HTTPS).

OAI vs. OAC: what to know for future-proofing

As CloudFront evolves, AWS introduced Origin Access Control, or OAC, to provide more secure and flexible access control for origins. OAC offers benefits such as improved permission semantics and finer-grained control. If you are starting a new project, it is worth evaluating OAC as a migration path from OAI. For existing deployments, OAI remains supported, and migrating to OAC can be planned as part of a broader modernization effort. This context is important when you plan long-term security and delivery architecture.

Best practices for using origin access identity

  • Document the OAI name and canonical user ID, along with the bucket policy details. This helps avoid misconfigurations during updates or when multiple environments exist.
  • Use private S3 buckets and block public access. This reduces the risk of accidental exposure and aligns with the principle of least privilege.
  • Leverage CloudFront to enforce HTTPS and add security headers. While origin access identity controls access to S3, your CloudFront distribution should enforce TLS and can be configured with headers such as Content-Security-Policy and X-Content-Type-Options for additional protection.
  • Cache thoughtfully. Configure TTLs to balance performance and content freshness. For dynamic assets, tailor TTLs and invalidate strategically when updates occur.
  • Automate policy management. When using infrastructure as code (Terraform, CloudFormation, or similar), centralize OAI references to minimize drift and simplify updates across environments.

Conclusion

Origin access identity provides a straightforward and effective way to secure Amazon S3 content behind CloudFront. By keeping the S3 bucket private and granting access only to the origin access identity, you prevent direct access while delivering fast, scalable content to end users. The combination of CloudFront, origin access identity, and optional signing mechanisms offers a robust security posture along with strong performance. While AWS evolves toward origin access control for new projects, OAI remains a practical choice for existing deployments. Following best practices—clear documentation, strict bucket policies, proper caching, and careful monitoring—enables a secure, maintainable, and high-performance content delivery workflow that serves users smoothly and safely.