我使用下面的代码在Lambda Edge中设置会话头作为响应。但我总是得到这个错误。
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'headers' of undefined",
"stack": [
"TypeError: Cannot read property 'headers' of undefined",
" at Runtime.exports.handler (/var/task/index.js:118:30)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
}
我使用的代码是这样的
exports.handler = function (event, config, callback) {
const request = event.Records[0].cf.request;
console.log(event.Records[0].cf);
const response = event.Records[0].cf.response;
const headers = response.headers;
getConfigCached(request, function (err, config) {
if (err) {
callback(err, null);
}
else if (request.uri !== "/code" && !redirectIfNotAuthenticated(config, request, callback)) {
callback(null, request);
}
else if (request.uri == "/code") {
console.log('INSIDE CODE');
let access_token = accessTokenCallback(request, callback);
console.log(access_token);
headers['session-token'] = [{ key: 'Session-Token', value: access_token }];
callback(null, response);
}
});
};
CloudFront触发器设置为查看器请求
2条答案
按热度按时间x9ybnkn61#
请检查您在创建Lambda@Edge时配置的设置。它应该设置为
Response origin
,以便您能够设置响应头。顺便说一句,有一个创建这种Lambda@edge AWS蓝图的蓝图
ccrfmcuu2#
添加触发器时,您可以将其设置为附加到原始响应。请参见https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-edge-add-triggers.html