-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambda_function.py
59 lines (41 loc) · 1.7 KB
/
lambda_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
from config import FALLBACK_REGION, REGIONS, BASE_NAME
def find_best_region(current_region):
"""
Find the geographical nearest region for a given source region
"""
if current_region == FALLBACK_REGION:
return FALLBACK_REGION
# if current_region is eu-central-1
# search_options become: [ eu-central-1, eu-central, eu ]
# This way the options get broader gradualy and find the "best" bucket
a, b, _ = current_region.split("-")
search_options = [current_region, f"{a}-{b}", a]
for option in search_options:
for r in REGIONS:
if r.startswith(option):
return r
# Return the fallback region if we didn't found a match with our algorithm
return FALLBACK_REGION
def build_bucket_name(region):
"""
Take a region name and build a FQDN for the S3 bucket
"""
if region == FALLBACK_REGION:
# Don't append the region suffix for the default bucket
bucket_name = f"{BASE_NAME}"
else:
bucket_name = f"{BASE_NAME}-{region}"
return f"{bucket_name}.s3.{region}.amazonaws.com"
def lambda_handler(event, context):
request = event["Records"][0]["cf"]["request"]
# Fallback to FALLBACK_REGION if we can't get a region
current_region = os.environ.get("AWS_REGION", FALLBACK_REGION)
best_region = find_best_region(current_region)
print(f"{current_region} got routed to {best_region}")
domainName = build_bucket_name(best_region)
# Rewrite request to the newly found bucket
request["origin"]["s3"]["region"] = best_region
request["origin"]["s3"]["domainName"] = domainName
request["headers"]["host"] = [{"key": "host", "value": domainName}]
return request