Test Distribution Invalidations

Test Distribution Invalidations

CloudFront keeps copies of your files in its edge caches until they expire. If you update a file at the origin but keep the same URL, viewers can keep getting the old copy until the cache expires. An invalidation removes the cached copy, so the next request goes back to the origin and fetches the new version.

In this step, you invalidate the default root object (index.html) and confirm that CloudFront fetches it again from the S3 origin.

  1. Open the Invalidations tab of your distribution.
  • In the CloudFront console, choose Distributions, then choose your distribution.
  • Choose the Invalidations tab.
  • Choose Create invalidation.

CloudFront Lab

  1. Enter the object paths to invalidate.
  • For Selection method, keep By paths.
  • In Object paths to invalidate, enter /. Because index.html is the default root object, the path / points to it.
  • Choose Create invalidation.

CloudFront Lab

Enter each path on a separate line. A wildcard (*) is only valid at the end of a path, for example /images/*. To invalidate every object in the distribution, use /*.

  1. CloudFront shows the message Successfully created invalidation the cached copy has been removed from all edge locations.

CloudFront Lab

  1. Verify the result in the browser.
  • Open the distribution domain name in the browser.
  • Open the browser developer tools, choose the Network tab, and reload the page.
  • Select the request for / and check the Response Headers. The header X-Cache: Miss from cloudfront shows that CloudFront did not serve the object from its cache and fetched it from the origin (Server: AmazonS3).
  • If you reload the page again, the header changes to X-Cache: Hit from cloudfront because the new version is now cached.

CloudFront Lab

An invalidation usually completes within a few minutes. Until it finishes, some edge locations can still return the old version.

The first 1,000 invalidation paths each month are free. After that, CloudFront charges for each path. A wildcard path such as /* counts as one path. For files that change often, versioned file names (for example app.v2.js) are usually a better choice than repeated invalidations.