Created
May 9, 2019 02:33
-
-
Save davebra/8e04c7982c09a475e7abd44002e36906 to your computer and use it in GitHub Desktop.
Serverless Country Lookup with AWS (Lambda+CloudFront)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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