Search code examples
amazon-web-servicesamazon-s3amazon-cloudfront

Is it possible to set Amazon CloudFront cache to far-future expiry?


How do I change the cache expiry in CloudFront on AWS? I can't see a way to do it and I think I saw an old post of a few years ago on here was somebody said it couldn't be done.

I've gone through every option in S3 and CloudFront and every option on the outer folder and on the file, but nothing.

Can it be done now, or is there any alternative? I really want to set the cache to 6 months or a year if I can.

AWS is hard work.


Solution

  • You can, but its not exactly obvious how this works.

    You can store custom http headers with your s3 objects. If you look at the console, this is under the metadata section for an object. With this you can set a far future expires header.

    Cloudfront will take the existing headers and pass them on. If cloudfront is already caching the object, you will need to invalidate it to see the headers after you set them.