SangminKim
SangminKim

Reputation: 9136

Why does CloudFront cache s3 object even though there aren't any cache settings?

I have a CloudFront distribution having s3 bucket as an origin.

The distribution is using Use Origin Cache Headers setting.

The additional setting is like the below.

enter image description here

After that, I upload a file through S3 console and try to get the file using curl.(of course, there is no Cache-Control metadata)

enter image description here

$ curl -X GET https://xxxxxxxxxxx/file -v
.
.
X-Cache: Miss from cloudfront
.
.
.
$ curl -X GET https://xxxxxxxxxxx/file -v
.
.
X-Cache: Hit from cloudfront.
.
.

I expected that all requests should return cache-miss because there is no setting for the cache anywhere(CloudFront and S3 object metadata). However, as you can see, the second request is returning cache-hit.

Why does CloudFront cache it?

Is there any hidden default cache settings?

Upvotes: 0

Views: 607

Answers (1)

Michael - sqlbot
Michael - sqlbot

Reputation: 179144

The way the console presents this is admittedly very confusing and arguably not even "correct."

CloudFront always uses the origin cache headers and always uses a Min/Default/Max TTL.

Use Origin Cache Headers actually means "Use Origin Cache Headers with standard CloudFront default and bounding values."

Customize actually means "Use Origin Cache Headers with custom CloudFront default and bounding values."

So objects with no Cache-Control are cached for up to Default TTL -- 86400 seconds, by default. If you don't want this behavior, switch to Customize and set Default TTL to the value you want CloudFront to use as an object TTL if no Cache-Control value is privided in the headers.

If Cache-Control is present, CloudFront ignores Default TTL and instead uses the bounding values Minimim TTL and Maximum TTL to determine whether it should use the origin's Cache-Control values internally, or override them. If out of range, CloudFront adjusts its internal timer so that the object's TTL is in the range >= Minimum TTL (default 0 seconds) and <= Maximum TTL (1 year) reducing too-large values to max and increasing too-small values up to min. If the Cache-Control value is already within that range, the value from Cache-Control is used as is. Either way, CloudFront does not actually modify the Cache-Control header itself.

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html

Upvotes: 2

Related Questions