How to Integrate S3 with CloudFront for Secure Delivery

Step-by-step S3 and CloudFront integration with origin access control, bucket policy, SSE-KMS, signed URLs, testing and OAI to OAC migration.

Aug 11, 2025 - 10:16
Nov 12, 2026 - 20:23
 0  44
TOPICDevOps BasicsPractical guide
How to Integrate S3 with CloudFront for Secure Delivery

To integrate S3 with CloudFront securely, keep the bucket private, create a CloudFront distribution with the bucket's REST endpoint as the origin, attach an origin access control (OAC) that signs every request, and add a bucket policy that lets only that distribution read objects. Then force HTTPS for viewers and add signed URLs if files are private.

This guide walks through that setup in the order you would build it, explains why each piece exists, and covers the mistakes that most often leave a bucket exposed or a distribution returning 403 errors. All settings were checked against the Amazon CloudFront and Amazon S3 documentation in November 2026. For a broader view of where CloudFront sits among other AWS services, see our list of AWS services DevOps engineers use daily.

Key takeaways

  • Use origin access control, not origin access identity. AWS documents OAI as legacy and recommends OAC for all new setups.
  • OAC supports buckets in every AWS Region, SSE-KMS encrypted objects and dynamic requests such as PUT and DELETE. OAI does not support these, or needs workarounds.
  • The bucket policy grants access to the service principal cloudfront.amazonaws.com and limits it with an AWS:SourceArn condition that names your distribution.
  • An S3 website endpoint cannot use OAC. If you need S3 website features, CloudFront treats the bucket as a custom origin.
  • OAC controls who can reach the bucket. Signed URLs or signed cookies control which viewers can reach CloudFront. Private content usually needs both.
  • If objects use SSE-KMS, the KMS key policy must also allow the distribution, or viewers get access errors even when the bucket policy is correct.

Why put CloudFront in front of S3 at all

You can serve files straight from an S3 bucket, so it is fair to ask what CloudFront adds. The first answer is control. When users fetch files from the bucket directly, the bucket has to allow public reads, and anything you upload by mistake becomes downloadable. With CloudFront in front, the bucket stays private and only the distribution can read from it. The second answer is delivery. CloudFront caches objects at edge locations close to users, so repeat requests do not travel back to the Region that holds the bucket. The third is features that S3 does not offer on its own, such as custom TLS certificates on your own domain, signed URLs with expiry times, geographic restrictions, and AWS WAF integration.

The trade-off is that you now manage two services and the trust between them. Most broken setups come from that trust relationship: a bucket policy that names the wrong distribution, a leftover public-read policy, or a KMS key that the distribution cannot use. The rest of this guide is about getting that relationship right.

How the pieces fit together

A request goes through three checks. The viewer connects to CloudFront over HTTPS. CloudFront decides whether this viewer may have the file, which is where signed URLs, signed cookies, geographic restrictions and WAF rules apply. If the file is not in the edge cache, CloudFront signs a request to S3 with SigV4 using the OAC. S3 checks that signature against the bucket policy and, for encrypted objects, KMS checks the key policy. Only when all of these pass does the object come back.

LayerWhat it protectsWhere you configure it
S3 Block Public AccessStops the bucket being made public by accidentS3 bucket or account settings
Origin access controlMakes CloudFront sign requests to S3CloudFront, Origin access
Bucket policyAllows only your distribution to readS3 bucket, Permissions tab
KMS key policyAllows the distribution to decrypt SSE-KMS objectsAWS KMS
Viewer protocol policyForces HTTPS between users and CloudFrontCloudFront cache behavior
Signed URLs or cookiesLimits which viewers can fetch private filesCloudFront trusted key groups

Step 1: Prepare a private bucket

Create the bucket in the Region closest to where your content is produced, not necessarily closest to users, because CloudFront handles the user side. Leave S3 Block Public Access turned on. You do not need public access for this design, and keeping the block on means a later mistake in a bucket policy cannot open the bucket to the internet.

Check Object Ownership. AWS states that OAC with an S3 origin requires Object Ownership set to Bucket owner enforced, which is the default for new buckets. If you have a legacy workload that still depends on ACLs, the documented alternative is Bucket owner preferred, so objects uploaded through CloudFront stay under your control.

Do not turn on static website hosting if you plan to use OAC. A bucket configured as a website endpoint has to be added to CloudFront as a custom origin, and the CloudFront documentation is clear that OAC and OAI cannot be used with it. If you need index documents for subdirectories, handle that with a CloudFront Function that rewrites paths, and keep the REST endpoint as the origin.

Step 2: Create the distribution

In the CloudFront console, create a distribution and pick your bucket from the origin domain list. Choose the REST endpoint (the one that looks like bucket-name.s3.region.amazonaws.com), not the website endpoint. Including the Region in the origin domain name also matters later: AWS notes that if you delete a distribution that has an OAC attached, you should delete the distribution before the bucket, or include the Region in the origin domain name.

Set the viewer protocol policy to Redirect HTTP to HTTPS or HTTPS only. Choose a cache policy that suits static files. The managed caching policy is fine for most assets, and you can add a separate behavior for paths that should not be cached for long, such as an index.html that changes on every release.

If you use your own domain, request or import a certificate in AWS Certificate Manager in the US East (N. Virginia) Region, which is where CloudFront reads certificates from, and add the domain as an alternate domain name. Our guide on configuring CloudFront with SSL and custom domains covers that part in detail.

Step 3: Create and attach the origin access control

Under Origin access in the CloudFront console, choose Create control setting, give it a name, set the origin type to S3, and keep the default signing behavior, Sign requests (recommended). Then edit the S3 origin in your distribution, choose Origin access control settings (recommended), and select the OAC you created.

The signing setting has three options, and the choice matters more than it looks. With always, CloudFront signs every origin request and, according to AWS, communication between CloudFront and S3 is always over HTTPS. With never, CloudFront does not sign anything, so the bucket must be public, which defeats the point. With no-override, CloudFront passes along a viewer's own Authorization header when there is one and signs only when there is not. That last option is for narrow cases, and it requires adding the Authorization header to the cache policy.

If you manage infrastructure as code, the same resource in CloudFormation looks like this:

Type: AWS::CloudFront::OriginAccessControl
Properties:
  OriginAccessControlConfig:
    Name: assets-oac
    Description: OAC for the assets bucket
    OriginAccessControlOriginType: s3
    SigningBehavior: always
    SigningProtocol: sigv4

Teams using Terraform have an equivalent aws_cloudfront_origin_access_control resource. Whichever tool you use, keep the OAC, the distribution and the bucket policy in the same change, so the three never drift apart.

Step 4: Write the bucket policy

The bucket policy is where most of the security comes from. It allows the CloudFront service principal to call s3:GetObject, but only when the request comes from your specific distribution. Without the condition, any CloudFront distribution in any account could read the bucket through OAC.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowCloudFrontServicePrincipalReadOnly",
      "Effect": "Allow",
      "Principal": { "Service": "cloudfront.amazonaws.com" },
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::example-assets-bucket/*",
      "Condition": {
        "StringEquals": {
          "AWS:SourceArn": "arn:aws:cloudfront::111122223333:distribution/EDFDVBD6EXAMPLE"
        }
      }
    }
  ]
}

Replace the bucket name, account ID and distribution ID with your own. AWS suggests adding this policy after you create the distribution but before you attach the OAC, so there is no window where CloudFront signs requests that S3 refuses. Grant only s3:GetObject unless you need uploads through CloudFront. If you do, add s3:PutObject, and also check which HTTP methods the cache behavior allows, because forwarding DELETE without a matching policy decision can let viewers remove files you meant to keep.

Leave s3:ListBucket out unless you have a reason to include it. Without it, a request for a missing object returns 403 rather than 404, which hides which keys exist. If you want friendly 404 pages, configure CloudFront custom error responses instead.

Step 5: Allow KMS if objects are encrypted with SSE-KMS

All new S3 objects are encrypted at rest. With the default SSE-S3, nothing extra is needed. If your bucket uses SSE-KMS with a customer managed key, CloudFront also needs permission to use that key. Add a statement to the key policy that allows the cloudfront.amazonaws.com principal to call kms:Decrypt, with the same AWS:SourceArn condition that names the distribution. AWS's example also includes kms:Encrypt and kms:GenerateDataKey*, which matter when CloudFront writes objects. If you only serve files, decrypt is the action that counts.

This is the step people miss. The symptom is an access denied error from S3 for every encrypted object, while unencrypted test files work, which sends people hunting through the bucket policy when the problem is in KMS.

Step 6: Restrict viewers with signed URLs or signed cookies

Everything up to this point stops users from going around CloudFront. It does nothing to stop anyone with the CloudFront URL from downloading a file. For paid downloads, internal reports or per-user files, add signed URLs or signed cookies.

A signed URL carries a policy and signature in the query string, so it suits single files such as one invoice or one installer. A signed cookie suits many files at once, such as every segment of a video stream or every asset behind a members area. AWS recommends trusted key groups for signing. You upload a public key to CloudFront, put it in a key group, attach the key group to the cache behavior, and sign URLs in your application with the matching private key. The older method using CloudFront key pairs requires the root user, so key groups are the better choice for teams that avoid root credentials.

Keep expiry times short. A link that lasts a few minutes is enough for a download that starts immediately. If you need to understand the S3 side of temporary access too, our post on signed URLs for temporary S3 object access explains S3 presigned URLs, which are a separate mechanism from CloudFront signed URLs. The differences between OAC and signed URLs are covered in origin access control versus signed URLs.

Step 7: Test the setup

Test from outside your AWS account, because a test run with admin credentials can hide a policy gap. Three checks cover most problems:

  1. Request a file through the CloudFront domain. It should return 200.
  2. Request the same file through the S3 URL. It should return 403. If it returns 200, something still grants public access.
  3. For private paths, request a file without a signature. It should return 403. Then request it with a valid signed URL and confirm 200, and with an expired one and confirm 403.
curl -I https://d111111abcdef8.cloudfront.net/images/logo.png
curl -I https://example-assets-bucket.s3.ap-south-1.amazonaws.com/images/logo.png

Turn on CloudFront standard logging or real-time logs and S3 server access logs during testing, so you can see which side refused a request. A 403 generated by CloudFront and a 403 passed through from S3 look the same to the viewer.

Migrating from OAI to OAC without downtime

Many older distributions still use an origin access identity. AWS documents a safe migration path. First, update the bucket policy so it has two statements: one that allows the OAI and one that allows the service principal with your distribution's source ARN. Then change the distribution origin from the OAI to the OAC. Wait until the distribution finishes deploying to all edge locations. Finally, remove the OAI statement from the bucket policy. Because both principals are allowed during the switch, CloudFront never loses access to the bucket.

If you change the distribution through the CLI, the documented approach is to export the config with aws cloudfront get-distribution-config, set the OriginAccessControlId field, clear the OriginAccessIdentity value, rename ETag to IfMatch, and apply it with aws cloudfront update-distribution.

Common mistakes and how to fix them

Using the website endpoint as the origin

If the origin is bucket.s3-website-region.amazonaws.com, OAC is not available and the bucket has to be public for CloudFront to read it. Switch to the REST endpoint and use a CloudFront Function for any path rewriting you relied on.

Forgetting the source ARN condition

A policy that allows cloudfront.amazonaws.com without a condition trusts every distribution, including other people's. Always include AWS:SourceArn.

Leaving an old public-read statement in the policy

When people move a public bucket behind CloudFront, they often add the new statement and forget to delete the old one. The S3 URL check in step 7 catches this.

Caching private responses for everyone

If personalised content goes through a shared cache behavior, one user's file can be served to another. Keep private content on its own path with signed URLs or cookies, and make sure the cache key includes what distinguishes one user's content from another's.

Stale files after a deploy

CloudFront serves the cached copy until it expires. Use versioned file names for assets, such as a hash in the file name, and reserve invalidations for files that must keep a fixed name.

Where this fits in a DevOps workflow

In a pipeline, the build step produces static files, a sync step uploads them to the bucket, and a final step invalidates the few fixed-name files. Because the bucket is private and only CloudFront can read it, the pipeline's IAM role needs write access to the bucket but nobody else needs read access. Define the bucket, OAC, distribution and policies in Terraform or CloudFormation so the security settings are reviewed like any other code change. If you are learning these services for a cloud role, our AWS Solutions Architect Associate training covers S3, CloudFront and IAM policies with hands-on labs, in our Kharadi, Pune classroom or live online.

For background on how CloudFront caches and routes content, read what Amazon CloudFront is and how it improves delivery.

Frequently asked questions

Is origin access identity deprecated?

AWS labels OAI as legacy and not recommended, and advises using origin access control instead. Existing OAI setups still work, but OAI does not support SSE-KMS, dynamic requests such as PUT and DELETE, or newer Regions, so new distributions should use OAC.

Can I use OAC with an S3 static website endpoint?

No. AWS states that a bucket configured as a website endpoint must be set up as a custom origin, which means OAC and OAI cannot be used. Use the REST endpoint as the origin and handle index documents or redirects with a CloudFront Function.

Why do I get 403 errors after adding OAC?

The usual causes are a bucket policy with the wrong distribution ARN, a missing KMS key policy statement for SSE-KMS objects, a request for an object that does not exist when the policy lacks s3:ListBucket, or an origin pointing at the website endpoint instead of the REST endpoint.

Do I still need signed URLs if I use OAC?

Only if the files are private. OAC stops people from going around CloudFront to reach the bucket. Signed URLs or signed cookies decide which viewers can download files through CloudFront. Public assets need only OAC, while paid or per-user files need both.

Should I use signed URLs or signed cookies?

Use signed URLs for individual files, such as a single report or installer. Use signed cookies when one user needs many files, such as all the segments of a video or all assets in a members area, because you set the cookie once instead of signing every URL.

Does traffic between CloudFront and S3 use HTTPS?

With OAC and the signing behavior set to always, AWS states that communication between CloudFront and S3 is always over HTTPS. With the other signing options, CloudFront uses the protocol set by your viewer protocol policy.

How do I move from OAI to OAC safely?

Add a bucket policy statement for the OAC distribution alongside the existing OAI statement, switch the distribution origin to the OAC, wait for the deployment to finish, and then remove the OAI statement. CloudFront keeps access to the bucket throughout.

References

Interested in learning more?Get a callback from our training advisors.

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Angry Angry 0
Sad Sad 0
Wow Wow 0
Mridul I am a passionate technology enthusiast with a strong focus on DevOps, Cloud Computing, and Cybersecurity. Through my blogs at DevOps Training Institute, I aim to simplify complex concepts and share practical insights for learners and professionals. My goal is to empower readers with knowledge, hands-on tips, and industry best practices to stay ahead in the ever-evolving world of DevOps.