'How to remove Cloudfront Headers on Viewer Response (Set-Cookie)

I would like to remove the "ASP.NET_SessionId" Set-Cookie header from the Cloudfront response.

set-cookie: lang=en;
set-cookie: lang-favorite=en;path=/
set-cookie: gxplang=E;
set-cookie: ASP.NET_SessionId=sdfdsf


Solution 1:[1]

Add a Lambda @Edge Function to the Viewer Response Behaviour of Cloudfront:

'use strict';

const discardHeader = 'ASP.NET_SessionId=';

exports.handler = (event, context, callback) => {
    const response = event.Records[0].cf.response;
    const setCookieHeaders = response.headers['set-cookie'];

    if(setCookieHeaders && setCookieHeaders.length > 0)
    {
        response.headers['set-cookie'] = setCookieHeaders.filter( h => h.value.indexOf(discardHeader) < 0 );
    }      
    return callback(null, response);    
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gonzalo Gallotti