Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS CloudFront Not Updating

Whenever I make a change to my S3 bucket my CloudFront doesn't update to the new content. I have to create an invalidation every time in order to see the new content. Is there another way to make CloudFront load the new content whenever I push content to my S3 bucket?

like image 263
Richard Trinh Avatar asked Sep 08 '25 03:09

Richard Trinh


2 Answers

Let me answer your questions inline.

Whenever I make a change to my S3 bucket my CloudFront doesn't update to the new content. I have to create an invalidation every time in order to see the new content.

Yes, this is the default behavior in CloudFront unless you have defined the TTL values to be zero (0).

Is there another way to make CloudFront load the new content whenever I push content to my S3 bucket?

You can automate the invalidation using AWS Lambda. To do this;

  1. Create an S3 event trigger to invoke a lambda function when you upload any new content to S3.
  2. Inside the Lambda function, write the code to invalidate the CloudFront distribution using AWS CloudFront SDK createInvalidation method.

Note: Make sure the Lambda function has an IAM Role with policy permission to trigger an invalidation in CloudFront.

like image 113
Ashan Avatar answered Sep 10 '25 00:09

Ashan


Cloudfront keeps cache at edge points for minimum of one hour.

What you can do, as suggested by the docs, you can use versioned files.

BUT :

New versions of the files for the most popular pages might not be served for up to 24 hours because CloudFront might have retrieved the files for those pages just before you replaced the files with new versions

So I guess your best bet is invalidation.

EDIT: you can prevent the caching behaviour of versioned files if you change their names.

like image 38
AlexK Avatar answered Sep 09 '25 23:09

AlexK