Reputation: 1439
I have a cloudfront distribution with s3 as my origin. The bucket contains only an index.html file.
The cloudfront distribution has an origin policy to forward Cloudfront-Viewer-Country, Accept-Language, and all cookies from the viewer request.
With the aforementioned I am able to visit my test url without any problems. Everything loads fine.
When I create an origin request lambda@edge function that simply reads the CloudFront-Viewer-Country value and stores that in a cookie like so:
'use strict';
/* This is an origin request function */
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
const countryCode = headers['cloudfront-viewer-country'] ? headers['cloudfront-viewer-country'][0].value : 'CA';
const response = {
status: '200',
headers: {
'set-cookie': [{
key: 'set-cookie',
value: 'locale='+countryCode,
}]
},
};
callback(null, response);
};
Then I deploy the lambda, setup the cloudfront trigger, invalidate the cache and then try to navigate to my test url nothing is returned. The screen is just blank :(
Is there something wrong with my lambda function? If I disassociate the lambda origin request function from the cloudfront distribution then invalidate the cache, everything works again.
Upvotes: 1
Views: 1398
Reputation: 10744
The edge lambda is supposed to modify the request/response object and return it, not replace it with a new object with the rest of the properties absent.
Something like this:
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
const countryCode = headers['cloudfront-viewer-country'] ? headers['cloudfront-viewer-country'][0].value : 'CA';
headers['set-cookie'] = [
[{
key: 'set-cookie',
value: 'locale='+countryCode,
}]
];
callback(null, request);
};
Upvotes: 1