AWS cloudfront not updating on update of files in S3

后端 未结 9 509
无人共我
无人共我 2021-02-01 14:12

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 Clou

相关标签:
9条回答
  • 2021-02-01 14:36

    The main advantage of using CloudFront is to get your files from a source (S3 in your case) and store it on edge servers to respond to GET requests faster. CloudFront will not go back to S3 source for each http request.

    To have CloudFront serve latest fiels/objects, you have multiple options:

    Use CloudFront to Invalidate modified Objects

    You can use CloudFront to invalidate one or more files or directories manually or using a trigger. This option have been described in other responses here. More information at Invalidate Multiple Objects in CloudFront. This approach comes handy if you are updating your files infrequently and do not want to impact the performance benefits of cached objects.

    Setting object expiration dates on S3 objects

    This is now the recommended solution. It is straight forward:

    • Log in to AWS Management Console
    • Go into S3 bucket
    • Select all files
    • Choose "Actions" drop down from the menu
    • Select "Change metadata"
    • In the "Key" field, select "Cache-Control" from the drop down menu.
    • In the "Value" field, enter "max-age=300" (number of seconds)
    • Press "Save" button The default cache value for CloudFront objects is 24 hours. By changing it to a lower value, CloudFront checks with the S3 source to see if a newer version of the object is available in S3.

    I use a combination of these two methods to make sure updates are propagated to an edge locations quickly and avoid serving outdated files managed by CloudFront.

    AWS however recommends changing the object names by using a version identifier in each file name. If you are using a build command and compiling your files, that option is usually available (as in react npm build command).

    0 讨论(0)
  • 2021-02-01 14:42

    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.

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

    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.

    0 讨论(0)
  • 2021-02-01 14:43

    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.

    0 讨论(0)
  • 2021-02-01 14:45

    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

    Step 2.

    Select Distribution settings and select behaviour tab

    Step 3.

    Select Edit and choose option All as per the below image

    Step 4.
    Save your settings and that's it

    0 讨论(0)
  • 2021-02-01 14:51

    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/

    0 讨论(0)
  • 2021-02-01 14:51

    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

    0 讨论(0)
提交回复
热议问题