One minute
Stop CloudFront caching some files in S3
AWS S3 can host static html files and CloudFront could be used in front of it to distribute files faster the regionalised users.
But sometimes, we may want CloudFront not cache some particular files because they are continously updated.
AWS has a good documentation on how the cache works between the CloudFront and origin (in our case, it’s S3)
Long story in short, CloudFront has its own TTL setting but you can specifcy it for a particular pattern. In addition, CloudFront also respects the cache-control
header from the content origin. Depends on the value of this header and its TTL setting for that file, it can behave differently. If you want CloudFront not cache a file in S3, you need to do these 2 steps:
- Set up a pattern for the file in CloudFront with all TTLS settings to 0
- In the S3, add a new metadata for that S3 object:
cache-control: max-age=0
The best part of AWS services is the integration between them. When the file in s3 is not changed (e.g ETAG is same), CloudFront will use the cache to serve the request. If the file is changed, CloudFront will know and serve the new content in the first request and cache for subsequent requests.