title: Lambda@Edge Cache-Control header not present Cloudfront
body: <p>I've images stored on S3 and a lambda function to resize them on the fly. While doing it I add <code>CacheControl: 'max-age=31536000'</code> to the resized images and also add a Cache-Control header:</p>
  
  <pre><code>.then(buffer =&gt; {
    // save the resized object to S3 bucket with appropriate object key.
    S3.putObject({
      Body: buffer,
      Bucket: BUCKET,
      ContentType: 'image/jpg',
      CacheControl: 'max-age=31536000',
      Key: `uploads/${key}`,
    })
  
    // generate a binary response with resized image
    response.status = 200
    response.body = buffer.toString('base64')
    response.bodyEncoding = 'base64'
    response.headers['content-type'] = [{ key: 'Content-Type', value: 'image/jpg' }]
    response.headers['cache-control'] = [{ key: 'Cache-Control', value: 'public, max-age=31536000' }]
    callback(null, response)
  })
  </code></pre>
  
  <p>And if the thumbnail has already been generated, I just do:</p>
  
  <pre><code>if (!response.headers['cache-control']) {
    response.headers['cache-control'] = [{ key: 'Cache-Control', value: 'public, max-age=31536000' }]
  }
  callback(null, response)
  </code></pre>
  
  <p>On my Cloudfront distribution, I've the following settings:</p>
  
  <ul>
  <li>Cache Based on Selected Request Headers: None</li>
  <li>Object Caching: Use Origin Cache Headers</li>
  </ul>
  
  <p>The lambda works fine but when I look in the devtools, it seems that Chrome never cache the images. Here is the info I get:</p>
  
  <pre><code>General
    Request URL: https://aws.mydomain.com/478/dd123cd5-1636-47d0-b756-e6c6e9cb28c0/normal/pic.jpg
    Request Method: GET
    Status Code: 200 
    Remote Address: 52.84.31.149:443
    Referrer Policy: no-referrer-when-downgrade
    age: 382
  
  Response Headers
    content-length: 49192
    content-type: image/jpg
    date: Thu, 09 May 2019 20:41:42 GMT
    server: AmazonS3
    status: 200
    via: 1.1 261e801dca9c54ff576f39f96d80ede5.cloudfront.net (CloudFront)
    x-amz-cf-id: ZlheiBoNDuYDeuvZo0jBP6Zjpge5AonPGlCo_N2pHhHdGwV7DorKtA==
    x-amz-id-2: xkDxIB0qDJt5KMeHINq7/gaRII6EDBOsL3SlMuhMwJ84M/lak9E/tcRChv7vvYurD+2hYOT8kSI=
    x-amz-request-id: CAD9AE1E905BB13A
    x-cache: Hit from cloudfront
  
  Request Headers
    :authority: aws.mydomain.com
    :method: GET
    :path: /478/dd123cd5-1636-47d0-b756-e6c6e9cb28c0/normal/pic.jpg
    :scheme: https
    accept: */*
    accept-encoding: gzip, deflate, br
    accept-language: en-US,en;q=0.9
    user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36
  </code></pre>
  
  <p>Control-Cache is not present and I can't figure this out...</p>
  
  <p>When I invalidate all the images in the distribution, the only change is the value of x-cache: 'Error from cloudfront' on the first load (status 200 and image is loaded ok)</p>