Is it possible to send Cloudfront a header to have it look up the IP address in their database, rather than the IP address of the client?
For example, if a server wants to determine location based on an IP address, it could set an x-real-ip
and x-forwarded-for
which Cloudfront would then use for the IP to location look up.
As it stands, I've tried to add the following headers with no luck.
r.Header.Set("x-real-ip", user-ip)
r.Header.Set("x-forwarded-proto", user-ip)
r.Header.Set("x-forwarded-for", user-ip)
I then create a Cloudfront distribution that forwards CloudFront-Viewer-City
, and a Lambda@edge viewer request that does the following.
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
let body = "";
let responseStatus = "200";
if (headers["cloudfront-viewer-city"]) {
body = headers['cloudfront-viewer-city'][0].value;
}
else {
responseStatus = "500";
body = "";
}
const response = {
status: responseStatus,
statusDescription: 'OK',
headers: {
'content-type': [{
key: 'Content-Type',
value: 'application/json'
}],
'access-control-allow-origin': [{
key: "access-control-allow-origin",
value: "*"
}]
},
body: makeBody(body)
};
callback(null, response);
};
function makeBody(city) {
let obj = {
city: city,
};
return JSON.stringify(obj);
}
The actual result is that the server's IP address gets the lookup, not the forwarded IP.