Last active
August 10, 2021 09:25
-
-
Save danielreiser/0ca40dc5efc93431bfa4033008ed460a to your computer and use it in GitHub Desktop.
Origin Request lambda@edge function for header evaluation
This file contains 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'; | |
const SOURCE_HEADER_NAME = 'x-myapp-origin'; | |
exports.handler = async event => { | |
console.group('[INFO] lambda@edge Origin Request') | |
const request = event.Records[0].cf.request; | |
if (request.headers[SOURCE_HEADER_NAME]) { | |
const headerValue = request.headers[SOURCE_HEADER_NAME][0].value; | |
const domainName = `web-bucket-${headerValue}.s3.eu-central-1.amazonaws.com`; | |
console.log(`[OVERWRITE] Using custom origin: ${domainName}`); | |
request.origin = { | |
s3: { | |
...request.origin.s3, | |
domainName, | |
}, | |
}; | |
request.headers['host'] = [{ key: 'Host', value: domainName }]; | |
console.log(request.origin.s3); | |
console.log(request.headers.host); | |
} | |
console.groupEnd() | |
return request; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment