Skip to content

How do I change my configurations so that CloudFront doesn't cache certain files?

3 minute read
0

I want Amazon CloudFront to pass certain files through and not cache the files at edge locations.

Resolution

Configuration on the origin

Note: If you use legacy cache settings, then update your CloudFront distribution's cache behavior to set object caching as Use Origin Cache Headers. If you use a cache policy, then CloudFront uses the origin Cache-Control settings by default.

On your custom origin web server application, add Cache-Control no-cache, no-store, or private directives to the objects that you don't want CloudFront to cache. Or, add the Expires directive to the objects that you don't want CloudFront to cache.

For Amazon Simple Storage Service (Amazon S3) origins, use object metadata to add the Cache-Control no-store directive to objects that CloudFront shouldn't cache. If you can't add object metadata to each object, then use the AWS Lambda Lambda@Edge function for the Origin response function. Use the Lambda@Edge function to update the value of the Cache-Control header based on the value of the content-type header or other response parameters.

Configuration on the distribution

You can control the amount of time that CloudFront keeps an object in the cache before it sends another request to the origin. Set the minimum, maximum, and default time-to-live (TTL) values in a CloudFront distribution's cache behavior. You can set these values in a cache policy attached to the cache behavior, or in the legacy cache settings.

To use a cache policy to edit the configuration on the distribution, complete the following steps:

  1. Open the CloudFront console.
  2. Select the distribution that you want to modify.
  3. Choose the Behaviors tab.
  4. If you already have a cache behavior for the objects that you don't want CloudFront to cache, then select the cache behavior and choose Edit. To create a new cache behavior, choose Create Behavior.
  5. For Cache and origin request settings, select Cache policy and origin request policy.
  6. For Cache policy, select Managed - CachingDisabled.
    Note: If you use legacy cache settings, then choose Customize for Object caching and set the minimum, maximum, and default TTL values to 0.
  7. Choose Save changes.

Note: If you don't use an Amazon S3 bucket as your origin, then set a specific cache behavior to forward all headers to the origin. Update the cache behavior, and then set Cache Based on Selected Request Headers to All.

Related information

Specify the amount of time that CloudFront caches objects

Add, remove, or replace content that CloudFront distributes