Controlling Cloudfront

Cloudfront honors the source Cache-Control headers by default. That meant the easiest resolution to my Cloudfront caching issue was to set the Cache-Control headers in S3. After an hour or two of research, I decided that s3cmd was the right answer for me (I use s3cmd to sync files up to s3).

Let’s start with my front page.

1
s3cmd modify --add-header="Cache-Control: public, max-age=3600" s3://ideoplex.com/index.html

That seemed to do the trick (the first request is from s3 directly and the second request is via cloudfront).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ curl -I http://ideoplex.com.s3-website-us-east-1.amazonaws.com/index.html
HTTP/1.1 200 OK
x-amz-id-2: 4tfynzLtAyu1K0Pp3YqOEr7nlLwtuvNPX15PVmdXRa3f/7eDVF2yFRs4AJAnBCb2BjnawP5Aa3E=
x-amz-request-id: 71222B1E11C31C1E
Date: Sun, 13 Sep 2015 19:41:19 GMT
x-amz-meta-s3cmd-attrs: ...
Cache-Control: public, max-age=3600
Last-Modified: Sun, 13 Sep 2015 18:41:16 GMT
ETag: "358f032aa7f83bf14914430b80817c83"
Content-Type: text/html
Content-Length: 30702
Server: AmazonS3
$ curl -I http://ideoplex.com/
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 30702
Connection: keep-alive
Date: Sun, 13 Sep 2015 18:41:27 GMT
x-amz-meta-s3cmd-attrs: ...
Cache-Control: public, max-age=3600
Last-Modified: Sun, 13 Sep 2015 18:41:16 GMT
ETag: "358f032aa7f83bf14914430b80817c83"
Server: AmazonS3
X-Cache: RefreshHit from cloudfront
Via: 1.1 dbfc7fdca19a1a429546608a6a58a3d2.cloudfront.net (CloudFront)
X-Amz-Cf-Id: 2cgjVuUtuB0WKNa4A8NBjEF3Lwep5LZI_Y1_6of6yO75MYH2WjG2rw==

Unfortunately, the headers do not persist when the underlying file is updated. That complicates things - more to come.