Skip to content

Instantly share code, notes, and snippets.

@ArthurKnoep
Created May 20, 2022 12:22
Show Gist options
  • Save ArthurKnoep/2ccb4a3612e1dbab137a54eb85f15c4d to your computer and use it in GitHub Desktop.
Save ArthurKnoep/2ccb4a3612e1dbab137a54eb85f15c4d to your computer and use it in GitHub Desktop.
Redirecting Request by Country or Latency
# 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