Can we set cookies to a viewer request by an edge lambda in a cloudfront and redirect to the same page?
I have given a sample code below written for edge lambda. Same code accepts for the Origin Request in the cloudfront events, but rejected by viewer request.
Status Code: 400; Error Code: InvalidLambdaFunctionAssociation; Request ID:xx
'use strict';
exports.handler = (event, context, callback) => {
var date = new Date();
// Get Unix milliseconds at current time plus 365 days
date.setTime(+ date + (365 * 86400000)); //24 \* 60 \* 60 \* 100
var cookieVal = Math.random().toString(36).substring(7); // Generate a random cookie string
var cookieString = "testCookie="+cookieVal+"; domain=dxxxxxxxx.cloudfront.net; path=/index.html; expires="+date.toGMTString()+";";
const response = {
status: '302',
statusDescription: 'Found',
headers: {
location: [{
key: 'Location',
value: 'https://dxxxxxxxxxxx.cloudfront.net/index.html',
}],
'set-cookie': [{
key: 'Set-Cookie',
value: cookieString
}],
},
};
callback(null, response);
};
Scenario: Setting cookies in the login page, if cookies are not set, set cookies to the login page with a redirection