Last active
July 26, 2018 08:31
-
-
Save jakubknejzlik/ebfa1049d15e7578bac1c1ce92fbee6c to your computer and use it in GitHub Desktop.
CloudFront Lambda request/origin event handler
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
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