Created
May 20, 2022 12:22
-
-
Save ArthurKnoep/2ccb4a3612e1dbab137a54eb85f15c4d to your computer and use it in GitHub Desktop.
Redirecting Request by Country or Latency
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
# Need to install DNS package | |
import dns.resolver | |
def lambda_handler(event, context): | |
request = event['Records'][0]['cf']['request'] | |
res = dns.resolver.query("latency-cross-region.example.com", "txt") | |
buff = res[0].to_text().replace('"', '').split(";") | |
bucket_name = buff[0] | |
region = buff[1] | |
domainName = f"{bucket_name}.s3.{region}.amazonaws.com" | |
request['origin']['s3']['domainName'] = domainName | |
request['origin']['s3']['path'] = '' | |
request['origin']['s3']['region'] = region | |
request['headers']['host'] = [{'key':'Host', 'value': domainName}] | |
return request |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment