Skip to content

Instantly share code, notes, and snippets.

@jakubknejzlik
Last active July 26, 2018 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakubknejzlik/ebfa1049d15e7578bac1c1ce92fbee6c to your computer and use it in GitHub Desktop.
Save jakubknejzlik/ebfa1049d15e7578bac1c1ce92fbee6c to your computer and use it in GitHub Desktop.
CloudFront Lambda request/origin event handler
const path = require('path');
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const userAgent = getHeader(request, 'User-Agent');
console.log('before',userAgent,'=>',JSON.stringify(request));
const domain = getDomain(request);
if (!domain) {
return callback(null, request);
}
if(userAgent == 'Amazon CloudFront') {
// handle origin request
const bucketName = domain.replace('.s3.amazonaws.com','')
request.uri = request.uri.replace(`.${bucketName}/`,'/')
} else {
// handle viewer request
if(path.extname(request.uri) === '') {
request.uri = '/index.html'
}
request.uri = '/' + domain + request.uri;
}
console.log('after',userAgent,'=>',JSON.stringify(request));
callback(null, request);
};
function getDomain(request) {
const host = getHeader(request, 'Host');
const parts = host.split('.');
return parts.join('.');
}
function getHeader(request, header) {
const item = request.headers[header.toLowerCase()].find(item => item.key === header);
return item && item.value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment