Why is CloudFront serving outdated content from Amazon S3?

Last updated: 2020-08-06

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:

  • Store new versions of the object at the origin with the version number in the key name. For example, if you update /image_v1.png, then you store a new version of the object as /image_v2.png.
  • Update the object at the origin but cache based on a query string with the object version. For example, the query string updates from /image.png?ver=1 to /image.png?ver=2.
    Note: You can still request the previous version (/image.png?ver=1) while it's available in the CloudFront cache.

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

  • Storing new versions of the object at the origin (Amazon S3) allows you to revert changes because the previous versions are still available under the previous names. However, storing multiple versions of an object can increase your storage costs.
  • Updating the object at the origin but caching based on the query string can reduce your storage costs. However, to prepare for any rollbacks, it's a best practice to keep previous object versions offline.

Did this article help?


Do you need billing or technical support?