title: Pass cookie to CloudFront origin but prevent from caching
body: <p>I am using CloudFront as cache in front of my Symfony web application. To get a cache based on a user's role (admin, customer,...) I generate a user role based hash in a Lambda@Edge Viewer Request trigger. I pass that hash on as a request header to my origin as <code>X-User-Context-Hash</code>.</p>
  
  <p>My problem is now that I need to pass the <code>PHPSESSID</code> cookie on to my origin to get the right response for caching, but I do not want to base the cache on the value of <code>PHPSESSID</code>. I do only need my cached response to be based on the value of <code>X-User-Context-Hash</code> but not on my session cookie.</p>
  
  <p>The image below should explain my problem in detail</p>
  
  <p>Is there any possibility to accomplish that?</p>
  
  <p>Would appreciate any help.</p>
  
  <p><a href="https://i.stack.imgur.com/RLC8D.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/RLC8D.jpg" alt="enter image description here"></a></p>
  
  <p>Here's my Lambda@Edge Viewer Request trigger:</p>
  
  <pre><code>'use strict';
  
  function parseCookies(headers) {
      const parsedCookie = {};
      if (headers.cookie) {
  
          console.log(`${headers.cookie[0].value}`);
  
          headers.cookie[0].value.split(';').forEach((cookie) =&gt; {
              if (cookie) {
                  const parts = cookie.split('=');
                  parsedCookie[parts[0].trim()] = parts[1].trim();
              }
          });
      }
      return parsedCookie;
  }
  
  exports.handler = (event, context, callback) =&gt; {
      const request = event.Records[0].cf.request;
      const headers = request.headers;
  
      const https = require('https');
  
      // Read session cookie
      const parsedCookies = parseCookies(headers);
      let cookie = '';
      if (parsedCookies) {
          if(parsedCookies['PHPSESSID']) {
              cookie = `PHPSESSID=${parsedCookies['PHPSESSID']}`;
          }
      }
  
      console.log(`Cookie: ${cookie}`);
  
      // Send request to origin host at /_fos_user_context_hash
      // passing the original session cookie
      const options = {
          hostname: `${request.headers.host[0].value}`,
          port: 443,
          path: '/_fos_user_context_hash',
          method: 'HEAD',
          headers: {
              'Cookie': cookie, 
              'Accept': 'application/vnd.fos.user-context-hash',
              'Vary' : 'Cookie'
          }
      };
  
      const req = https.request(options, (res) =&gt; {
        console.log('statusCode:', res.statusCode);
        console.log('headers:', res.headers);
  
        // Read the X-User-Context-Hash from the hash endpoint
        const headerName = 'X-User-Context-Hash';
        let hash = 'anonymous';
  
        if (res.headers[headerName.toLowerCase()]) {
          hash = res.headers[headerName.toLowerCase()];
        }
  
        // Append X-User-Context-Hash before passing request on to CF
        request.headers[headerName.toLowerCase()] = [{ key: headerName, value: hash }];  
  
        callback(null, request);
  
      }).on('error', (e) =&gt; {
        console.error(e);
        // Forward request anyway
        callback(null, request);
      });
  
      req.end();
  }
  
  
  ;
  </code></pre>