How to make CloudFront never cache index.html on S3 bucket

s3 cache-control
s3 no-cache
cloudfront invalidate cache
aws cloudfront create-invalidation
s3 cache busting
cloudfront browser caching
cache-control: no-cache
cloudfront cache behavior

I have a React app hosted on an S3 bucket. The code is minified using yarn build (it's a create-react-app based app). The build folder looks something like:

├── asset-manifest.json
├── favicon.ico
├── images
│   ├── map-background.png
│   └── robot-icon.svg
├── index.html
├── js
│   ├── fontawesome.js
│   ├── packs
│   │   ├── brands.js
│   │   ├── light.js
│   │   ├── regular.js
│   │   └── solid.js
│   └──
├── service-worker.js
└── static
    ├── css
    │   ├── main.bf27c1d9.css
    │   └──
    └── js
        ├── main.8d11d7ab.js

I never want index.html to be cached, because if I update the code (causing the hex suffix in main.*.js to update), I need the user's next visit to pick up on the <script src> change in index.html to point to the updated code.

In CloudFront, I can only seem to exclude paths, and excluding "/" doesn't seem to work properly. I'm getting strange behavior where I change the code, and if I hit refresh, I see it, but if I quit Chrome and go back, I see very outdated code for some reason.

I don't want to have to trigger an invalidation on every code release (via CodeBuild). Is there some other way? I think one of the challenges is that since this is an app using React Router, I'm having to do some trickery by setting the error document to index.html and forcing an HTTP status 200 instead of 403.

If you never want index.html to be cached, set the Cache-Control: max-age=0 header on that file only. CloudFront will make a request back to your origin S3 bucket on every request, but it sounds like this is desired behavior.

If you're wanting to set longer expiry times and invalidate the CloudFront cache manually, you can use a * or /* as your invalidation path (not / as you have mentioned). This can take up to 15 minutes for all CloudFront edge nodes around the world to reflect the changes in your origin however.

How do I prevent CloudFront from caching certain files?, Note: Be sure to update your CloudFront distribution's cache On your custom origin web server application, add Cache-Control no-cache, no-store, Note: If you aren't using an Amazon S3 bucket as your origin, you can set  If you already have a cache behavior for the objects that you don't want CloudFront to cache, select the cache behavior and choose Edit. To create a new cache behavior, choose Create Behavior. In the cache behavior's settings, enter the following to prevent caching: For Object Caching, select Customize. For Minimum TTL, enter 0. For Maximum TTL, enter 0. Choose Create to save the changes that you made.

How do you invalidate cache of index.html for a static site hosted on , These two commands can do it. To deploy the code: aws s3 sync ./ s3://bucket-​name-here/ --delete. Create an invalidation in CloudFront:. So I have hosted my angular app on s3 with a CloudFront dist. I do file revision-ing (using grunt filerev) to make sure that I never get stale content. But, how should I version the index.html file? It's required because all other files are referenced inside index.html. I have configured my bucket to be used as a static site.

Here is the command I ran to set cache-control on my index.html file after uploading new files to s3 and invalidating Cloudfront:

aws s3 cp s3://bucket/index.html s3://bucket/index.html --metadata-directive REPLACE --cache-control max-age=0

Trouble with static sites and Cloudfront Caching : aws, Everything gets synced up to an S3 bucket with a structure like this: I don't want Cloudfront to try and cache the index.html files at all, because "Cache-Control: no-cache" will make CloudFront revalidate (check if the file changed in S3) on  Create the S3 bucket with default settings and upload an index.html file (index.html will not be accessible directly from S3). Create a CloudFront distribution with the S3 bucket as its origin

It's much better to run an invalidation for index.html on every release than to defeat Cloudfront's purpose and serve it (what is basically an entrypoint for your app) from S3 every single time.

This is How I Reduced My CloudFront Bills by 80% - FAUN, How Long Objects Stay in a CloudFront Edge Cache ? In many cases, some files on your S3 bucket are not updated at all or updated rarely, this is why Say we have a video in the header of a static website, we will never update it. Amazon S3 maintains an index of object key names in each AWS region. CloudFront is Amazon’s CDN, but it is also great to keep your S3 bucket private and only serve your pages through CloudFront. So to create a CloudFront go back to the main AWS page by clicking on the AWS logo in the top, and this time search for “CloudFront” and click on it.

Cache Invalidation and Amazon S3, So you have an Amazon S3 hosted site with CloudFront caching in front of it? like an an index.html file, where changing the name isn't practical. Using the S3 interface it's easy to control cache headers, you just need to go into your bucket, find the file Going boldly where no product manager belongs. CloudFront does not return the default root object even if a copy of index.html appears in the install directory. If you configure your distribution to allow all of the HTTP methods that CloudFront supports, the default root object applies to all methods.

No Cache-Control Header for files from AWS CloudFront with S3 , I've hit a few pages in Google where you can set the Header in S3 for individual objects. That's really not a productive way to do it specially since in my case we  I'm using Amazon S3 webhosting for my static html,js,css (etc..) files. After replacing my index.html file, I still get the old version when consuming via the browser. I would like to set a default ttl to the bucket (and not to specific objects in it).

[PDF] Hosting Static Websites on AWS, Controlling How Long Amazon S3 Content Is Cached by Amazon CloudFront 36 the cost of operation is insignificant, 2) there is little or no management example, if a static website consists of HTML documents displaying images, it create a bucket, you select the AWS Region in which the files will be index.html​/. What I need is the ability to take all url requests and serve up the root index.html in my S3 bucket, rather than just doing a full redirect. Then my javascript application could parse the URL and serve the proper page. Is there any way to tell S3 to serve the index.html for all URL requests instead of doing redirects?

  • And are you talking about adding that header in the S3 object metadata? And correct, regardless of the URL path, I never want index.html cached. I'm more concerned about caching the related files (JS files, CSS, images).
  • Yes: it's referred to as 'System-Defined Metadata' in the AWS documentation:…
  • Great, this is helpful! I've set up my deploy process to use aws s3 cp --cache-control max-age=0 when copying over the index file. Works like a charm.
  • I know these are old Q&A, but I've accomplished the same by setting Cache-Control: no-store header (as metadata in S3 file) for index.html.
  • Hi @seza443 does this work for index.html files in inner directories, or just for the one in root directory?
  • @TumainiMosha not sure about this. I would say only root directory. And to match index.html for sub-directories as all, try *index.html ?
  • Thanks for feedback. I can confirm the initial rule (index.html) works for inner directories. I checked the cloudfront documentation, basically, their rules match file names, even in inner directories, not just root directory.
  • In case anyone else was curious: achieving this from the AWS CLI is not very convenient.
  • This is the way to go if you're using CI/CD solutions and your index.html gets updated every time. IMHO it should be accepted as the correct answer.
  • Life saver hack.
  • Thank you! However, I needed to add --region eu-central-1 to make it work. :)
  • The problem with that is once the browser sees an index.html without cache control header set for not caching, then the browser won't even go to the server on subsequent calls. As a result, Cloudfront invalidation won't do a thing for the existing users.
  • @UğurDinç "The problem with that is once the browser sees an index.html without cache control header set for not caching, then the browser won't even go to the server on subsequent calls." It actually does, at least Chrome. Still, to make it more deterministic, you can add the header to S3 objects. E.g. if you set minimum TTL > 0 and greater than max-age, Cloudfront will cache the objects for the value of the TTL (while the browser will use max-age). See:…