title: CloudFront / S3 ETag: Possible for CloudFront to send updated S3 Object before the CF TTL has expired?
body: <p>I have a question in regard to how CloudFront will use an S3 object's <em>ETag</em> to determine if it needs to send a refreshed object or not.</p>
  <p>I know that the ETag will be part of the Request to the CloudFront distribution, in my case I'm seeing the &quot;weak&quot; (shortened) version:</p>
  <pre><code>if-none-match: W/&quot;eabcdef4036c3b4f8fbf1e8aa81502542&quot;
  </code></pre>
  <p>If this ETag being sent does not match the S3 Object's current ETag value, then the CloudFront will send the latest version.</p>
  <p>I'm seeing this work as expected, but only after the CloudFront's cache policy has been reached.  In my case it's been set to 20 mins.</p>
  <p><strong>CloudFront with a Cache Policy</strong>:</p>
  <ul>
  <li>Minimum TTL: 1</li>
  <li>Maximum TTL: 1200 &lt;-- (20 mins)</li>
  <li>Default TTL: 900</li>
  <li>Origin Request Policy is not set</li>
  </ul>
  <p><strong>S3 Bucket</strong>:</p>
  <ul>
  <li>Set to only allow access via its corresponding CloudFront
  distribution above.</li>
  <li>Bucket and objects not public</li>
  <li>The test object (index.html) in this case has only one header set:
  Content-Type = text/html</li>
  <li>While I am using the CloudFront's Cache Policy, I've also tested
  using the S3 Object header of Cache-Control = max-age=6000</li>
  <li>This had no affect on the refresh of the &quot;index.html&quot; object in
  regard to the ETag check I'm asking about.</li>
  </ul>
  <p><strong>The Scenario</strong>:</p>
  <p>Upon first &quot;putObject&quot; to that S3 bucket, the &quot;index.html&quot; file has an ETag of:</p>
  <pre><code>eabcdef4036c3b4f8fbf1e8aa81502542
  </code></pre>
  <p>When I hit the URL (GET) for that &quot;index.html&quot; file, the cache of 20 mins is effectively started.</p>
  <p>Subsequent hits to the &quot;index.html&quot; URL (GET) has the Request with the value</p>
  <pre><code>if-none-match: W/&quot;eabcdef4036c3b4f8fbf1e8aa81502542&quot;
  </code></pre>
  <p>I also see &quot;<em>x-cache: Hit from cloudfront</em>&quot; in the Response coming back.</p>
  <p>Before the 20 mins is up, I'll make a change to the &quot;index.html&quot; file and re-upload via a &quot;putObject&quot; command in my code.</p>
  <p>That will then change the ETag to:</p>
  <pre><code>exyzcde4099c3b4f8fuy1e8aa81501122
  </code></pre>
  <p>I would expect then that the next Request to CloudFront, <em>before</em> the 20-minute TTL and with the old &quot;<em>if-none-match</em>&quot; value, would then prompt the CloudFront to see the ETag is different and send the latest version.</p>
  <p>But in all cases/tests it doesn't.  CloudFront will seem to ignore the ETag difference and continue to send the older &quot;index.html&quot; version.</p>
  <p>It's only after the 20 mins (cache TTL) is up that the CloudFront sends the latest version.</p>
  <p>At that time the ETag in the Request changes/updates as well:</p>
  <pre><code>if-none-match: W/&quot;exyzcde4099c3b4f8fuy1e8aa81501122&quot; 
  </code></pre>
  <p><strong>Question</strong> (<em>finally</em>, huh?):</p>
  <p>Is there a way to configure CloudFront to listen to the incoming ETag, and if needed, send the latest Object without having to wait for the Cache Policy TTL to expire?</p>
  <p><strong>UPDATE</strong>:</p>
  <p>Kevin Henry's response explains it well:</p>
  <p>&quot;<em>CloudFront doesn't know that you updated S3. You told it not to check with the origin until the TTL has expired. So it's just serving the old file until the TTL has expired and it sees the new one that you uploaded to S3. (Note that this doesn't have anything to do with ETags).</em>&quot;</p>
  <p>So I decided to test how the ETag would be used if I turned the CloudFront Caching Policy to a TTL of 0 for all three CloudFront settings.  I know that this defeats the purpose, and one of the strengths, of CloudFront, but I'm still wrapping my head around certain key aspects of CDN caching.</p>
  <p>After setting the cache to 0, I'm seeing a continual &quot;<em>Miss from CloudFront</em>&quot; in the Response coming back.</p>
  <p>I expected this, and in the first response I see a HTTP status of 200.  Note the file size being returned is 128KB for this test.</p>
  <p>Subsequent calls to this same file return a HTTP status of 304, with a file size being returned around 400B.</p>
  <p>As soon as I update the &quot;index.html&quot; file in the S3 bucket, and call that same URL, the status code is 200 with a file size of 128KB.</p>
  <p>Subsequent calls return a status of 304, again with an average of 400B in file size.</p>
  <p>Looking again at the definition of an HTTP status of 304:</p>
  <p><a href="https://httpstatuses.com/304" rel="nofollow noreferrer">https://httpstatuses.com/304</a></p>
  <p>&quot;<em>A conditional GET or HEAD request has been received and would have resulted in a 200 OK response if it were not for the fact that the condition evaluated to false.</em></p>
  <p><em>In other words, there is no need for the server to transfer a representation of the target resource because the request indicates that the client, which made the request conditional, already has a valid representation; the server is therefore redirecting the client to make use of that stored representation as if it were the payload of a 200 OK response.</em>&quot;</p>
  <p><strong>So am I correct in thinking that I'm using the Browser's cache at this point?</strong></p>
  <p>The calls to the CloudFront will now pass the requests to the Origin, where the ETag is used to verify if the resource has changed.</p>
  <p>As it hasn't, then a 304 is returned and the Browser kicks in and returns its stored version of &quot;index.html&quot;.</p>
  <p>Would this be a correct assumption?</p>
  <p>In case you're wondering, I can't use the invalidation method for clearing cache, as my site could expect several thousand invalidations a day. I'm hosting a writing journal site, where the authors could update their files daily, therefore producing new versions of their work on S3.</p>
  <p>I would also rather not use the versioning method, with a timestamp or other string added as a query to the page URL.  SEO reasons for this one mainly.</p>
  <p>My ideal scenario would be to serve the same version of the author's work until they've updated it, at which time the next call to that same page would show its latest version.</p>
  <p>This research/exercise is helping me to learn and weigh my options.</p>
  <p>Thanks again for the help/input.</p>
  <p>Jon</p>