Wednesday, February 5, 2014

Setting TTL to Amazon CloudFront

Do you want to clear the CloudFront cache? Don't want to wait 24 hours to see the new file? Don't want to rename your files?

By default Amazon CloudFront checks only once in 24 hours if the source files in the "origin" have changed. It means that if you upload a new version of the file, for example "index.html", it may take up to 24 hours until the new file is visible for browsers and users. Meanwhile CloudFront keeps serving the old file.

There are several ways to speed up the update:
  1. using invalidation object
  2. renaming the files
  3. setting the time-to-live (TTL) value for distribution origin and Cache-Control value for each individual file
1. Using invalidation object
After you have uploaded a new version from your file, you can make an invalidation request. The documentation says that it takes 10-15 minutes to clear the CloudFront cache for the given file, but in practice invalidation request handling may take up to 30 minutes (or even longer!). You can read more about invalidation requests from:

2. Renaming the files
New files are pushed to the CloudFront automatically. So if you don't mind that the file name changes for example from index.html to index_2.html, you can always upload a file which has completely new filename. Those files are visible in the CloudFront immediately.

3. Setting the TTL value and using Cache-Control
The most flexible and I think also easiest method is to set up the TTL value for the distribution origin to 0 (zero) and then add Cache-Control header to all your files at the moment of upload.

Setting TTL value using AWS Management Console

Follow these steps to set the TTL value:
1. Login to the AWS Management Console
2. Select CloudFront
3. Tick the check box from the distribution you want to manage
4. Select "Distribution settings"
5. Select "Behaviours" tab
6. Tick the check box from the origin you want to manage
7. Click "edit"
8. Set "Minimum TTL" to 0 (zero) and select "Object caching" value to be "Use Origin Cache Headers"
9. Save changes by clicking the button "Yes, Edit"

Follow these steps to set the headers for the uploaded file:
1. From the AWS Management Console, choose "Services" -> "S3"
2. Choose the bucket you are going to upload your file or files
3. Click "Upload"->"Add files" and use the file browser to search your file to be uploaded
4. Click "Set details"
5. Click "Set permissions" -> tick the check box "Make everything public" (if you want so)
6. Click "Set metadata"
7. Click "Add more metadata"
8. For the key, select from the drop-down menu "Cache-Control"
9. For the value, type in: max-age=[SECONDS_1], s-maxage=[SECONDS_2], for example: max-age=60, s-maxage=60
10. Click "Upload"

Done! Browsers will now check every [SECONDS_1] if the file has changed in the CloudFront, and  CloudFront will now check every [SECONDS_2] if the file has changed in the origin.

Note: If the original file did not have the Cache-Control value set, you need to invalidate it first!

Read more about Cache-Control and file expiration from

No comments: