Skip to content

Instantly share code, notes, and snippets.

@davebra
Created May 9, 2019 02:33
Show Gist options
  • Save davebra/8e04c7982c09a475e7abd44002e36906 to your computer and use it in GitHub Desktop.
Save davebra/8e04c7982c09a475e7abd44002e36906 to your computer and use it in GitHub Desktop.
Serverless Country Lookup with AWS (Lambda+CloudFront)
"use strict";
// countryLookup solves the Cloudfront website use-case
/*
Typical Cloudfront event structure to consume:
{
"Records": [
{
"cf": {
"request": {
"uri": "/test",
"headers": {
"cloudfront-viewer-country": [
{
key: "Cloudfromt-Viewer-Country",
value: "FR"
}
]
}
}
}
}
]
}
*/
exports.handler = (event, context, callback) => {
//console.log('EVENT', event);
//console.log('CONTEXT', context);
const request = event.Records[0].cf.request;
const headers = request.headers;
const countryCode = headers["cloudfront-viewer-country"][0].value;
const response = {
status: "200",
headers: {
["cache-control"]: [
{
key: "Cache-Control",
value: "no-cache, no-store, private"
}
]
},
body: JSON.stringify({
countryCode
})
};
callback(null, response);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment