I’m using Amazon CloudFront to serve objects stored in Amazon Simple Storage Service (Amazon S3). I updated my objects in S3, but my CloudFront distribution is still serving the previous versions of those files. Why isn’t my Amazon S3 content updating on CloudFront? How can I fix this?

Short Description

By default, CloudFront caches a response from Amazon S3 for 24 hours (Default TTL of 86,400 seconds). If your request lands at an edge location that served the Amazon S3 response within 24 hours, CloudFront uses the cached response even if you updated the content in Amazon S3.

Use one of the following ways to push the updated S3 content from CloudFront:

Resolution

Invalidate the S3 objects

You can invalidate an S3 object to remove it from the CloudFront distribution’s cache. After the object is removed from the cache, the next request retrieves the object directly from Amazon S3.

Before you run an invalidation, consider the following:

  • You can run an invalidation only on a web distribution. You can’t invalidate a Real Time Messaging Protocol (RTMP) distribution.
  • You can’t invalidate specific versions of an object that uses cookies or headers to vary the response. CloudFront invalidates all versions of the object in this case.
  • Each AWS account is allowed 1,000 free invalidation paths per month. For the price per invalidation path over 1,000 per month, see Invalidation Requests in Amazon CloudFront Pricing.

When you create an invalidation, be sure that the object paths meet the following requirements:

  • The object paths must be for individual objects or the paths must end with the wildcard character (*). For example, you can’t run an invalidation on a path similar to /images/*.jpeg because the path isn’t for an individual object, and it doesn’t end in a wildcard.
  • The specified path must exactly match the capitalization of the object’s path. Invalidation requests are case-sensitive.
  • To remove specific versions of an object based on a query string, include QueryString in the invalidation path.

Object invalidations typically take from 60 to 300 seconds to complete. You can check the status of an invalidation by viewing your distribution from the CloudFront console.

Use object versioning

If you update content frequently, we recommend that you use object versioning to clear the CloudFront distribution’s cache. For frequent cache refreshes, using object versioning might cost less than using invalidations.

Use one of these ways to add versioning to your objects:

  • Add the version number to the end of the object name.
  • Whitelist a query string with the object version, similar to the following query string:

Consider the following advantages and disadvantages for each method of object versioning:

  • If you add the version to the end of the object name, you can revert changes because the previous version of the object remains in Amazon S3 under the previous name. If you whitelist a query string with the object version, you can’t revert changes because the object is overwritten.
  • Because previous versions are stored, adding the version to the object name requires more Amazon S3 storage.