AWS cloudfront not updating on update of files in S3

40,376

Solution 1

Just ran into the same issue. At first I tried updating the cache control to be 0 and max-age=0 for the files in my S3 bucket I updated but that didn't work.

What did work was following the steps from @jpaljasma. Here's the steps with some screen shots.

First go to your AWS CloudFront service.

Then click on the CloudFront distrubition you want to invalidate. enter image description here

Click on the invalidations tab then click on "Create Invalidation" which is circled in red. enter image description here

In the "object path" text field, you can list the specific files ie /index.html or just use the wildcard /* to invalidate all. This forces cloudfront to get the latest from everything in your S3 bucket.

Once you filled in the text field click on "Invalidate", after CloudFront finishes invalidating you'll see your changes the next time you go to the web page.

enter image description here

Note: if you want to do it via aws command line interface you can do the following command

aws cloudfront create-invalidation --distribution-id <your distribution id> --paths "/*"

The /* will invalidate everything, replace that with specific files if you only updated a few.

To find the list of cloud front distribution id's you can do this command aws cloudfront list-distributions

Look at these two links for more info on those 2 commands:

https://docs.aws.amazon.com/cli/latest/reference/cloudfront/create-invalidation.html

https://docs.aws.amazon.com/cli/latest/reference/cloudfront/list-distributions.html

Solution 2

You should invalidate your objects in CloudFront distribution cache. Back in the old days you'd have to do it 1 file at a time, now you can do it wildcard, e.g. /images/*

https://aws.amazon.com/about-aws/whats-new/2015/05/amazon-cloudfront-makes-it-easier-to-invalidate-multiple-objects/

Solution 3

How to change the Cache-Control max-age via the AWS S3 Console:

  • Navigate to the file whose Cache-Control you would like to change.
  • Check the box next to the file name (it will turn blue)
  • On the top right click Properties
  • Click Metadata
  • If you do not see a Key named Cache-Control, then click Add more metadata.
  • Set the Key to Cache-Control set the Value to max-age=0 (where 0 is the number of seconds you would like the file to remain in the cache). You can replace 0 with whatever you want.

enter image description here

Solution 4

For immediate reflection of your changes, you have to invalidate objects in the Cloudfront - Distribution list -> settings -> Invalidations -> Create Invalidation.

This will clear the cache objects and load the latest ones from S3.

If you are updating only one file, you can also invalidate exactly one file.

It will just take few seconds to invalidate objects.

Distribution List -> settings -> Invalidations -> Create Invalidation

Distribution List - Invalidations Tab enter image description here

Solution 5

I also faced similar issues and found out its really easy to fix in your cloudfront distribution

Step 1.

Login To your AWS account and select your target distribution as shown in the picture below

enter image description here

Step 2.

Select Distribution settings and select behaviour tab

enter image description here

Step 3.

Select Edit and choose option All as per the below image

enter image description here

Step 4.
Save your settings and that's it

Share:
40,376
Jigar Tank
Author by

Jigar Tank

Love to code web apps !!

Updated on July 08, 2022

Comments

  • Jigar Tank
    Jigar Tank almost 2 years

    I created a distribution in cloudfront using my files on S3. It worked fine and all my files were available. But today I updated my files on S3 and tried to access them via Cloudfront, but it still gave old files.

    What am I missing ?

  • imperalix
    imperalix almost 9 years
    Forwarding all headers to your origin tells CloudFront not to cache anything. Why not, instead, set your Cache-Control headers to something like 5 minutes?
  • Jigar Tank
    Jigar Tank almost 9 years
    i've got hundreds of files inside 2 folders, and the option to change cache-control headers has to be done for each file. I didn't find any option to apply it in bulk to all. so i for the time being i chose the above option to send all request to origin.
  • jpaljasma
    jpaljasma about 7 years
    While this is definitely possible, it won't change behavior of already cached files. Furthermore, CloudFront has option to override minimum TTL.
  • James Dean
    James Dean almost 5 years
    It probably be a late comment but add metadata max-age=0 , adding 0 doesn't make any sense and not effective.
  • Sachin
    Sachin over 3 years
    This is a good way but I want to use the performance benefits of caching also. Can we call an API to set this Cache-control to 0 and then back to 1 day (or whatever it was earlier) after a day ?
  • Bjorn Liza
    Bjorn Liza almost 3 years
    Do note that once you add an invalidation you won't be able to delete that invalidation docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/…
  • netskink
    netskink about 2 years
    You rock the roll! I like this answer!