Skip to content

Commit a2c2010

Browse files
author
Graham Hargreaves
committed
Correct the lookup of the health endpoint to avoid caching when the lambda is warm.
1 parent 3a54879 commit a2c2010

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

handler.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,28 @@
1818
get_message_for_teams, get_org_message_for_teams, get_message_for_email, get_org_message_for_email, \
1919
get_org_message_for_eventbridge, get_message_for_eventbridge
2020

21-
# query active health API endpoint
22-
health_dns = socket.gethostbyname_ex('global.health.amazonaws.com')
23-
(current_endpoint, global_endpoint, ip_endpoint) = health_dns
24-
health_active_list = current_endpoint.split('.')
25-
health_active_region = health_active_list[1]
26-
print("current health region: ", health_active_region)
27-
28-
# create a boto3 health client w/ backoff/retry
29-
config = Config(
30-
region_name=health_active_region,
31-
retries=dict(
32-
max_attempts=10 # org view apis have a lower tps than the single
33-
# account apis so we need to use larger
34-
# backoff/retry values than than the boto defaults
21+
22+
boto3_config = None
23+
24+
25+
# Create a boto3 config object
26+
def create_boto3_config():
27+
# query active health API endpoint
28+
health_dns = socket.gethostbyname_ex('global.health.amazonaws.com')
29+
(current_endpoint, global_endpoint, ip_endpoint) = health_dns
30+
health_active_list = current_endpoint.split('.')
31+
health_active_region = health_active_list[1]
32+
print("current health region: ", health_active_region)
33+
# create a boto3 health client w/ backoff/retry
34+
return Config(
35+
region_name=health_active_region,
36+
retries=dict(
37+
max_attempts=10 # org view apis have a lower tps than the single
38+
# account apis so we need to use larger
39+
# backoff/retry values than than the boto defaults
40+
)
3541
)
36-
)
42+
3743

3844
# Get Account Name
3945
def get_account_name(account_id):
@@ -844,20 +850,22 @@ def get_sts_token(service):
844850
# create service client using the assumed role credentials, e.g. S3
845851
boto3_client = boto3.client(
846852
service,
847-
config=config,
853+
config=boto3_config,
848854
aws_access_key_id=ACCESS_KEY,
849855
aws_secret_access_key=SECRET_KEY,
850856
aws_session_token=SESSION_TOKEN,
851857
)
852858
print("Running in member account deployment mode")
853859
else:
854-
boto3_client = boto3.client(service, config=config)
860+
boto3_client = boto3.client(service, config=boto3_config)
855861
print("Running in management account deployment mode")
856862

857863
return boto3_client
858864

859865
def main(event, context):
860866
print("THANK YOU FOR CHOOSING AWS HEALTH AWARE!")
867+
global boto3_config
868+
boto3_config = create_boto3_config()
861869
health_client = get_sts_token('health')
862870
org_status = os.environ['ORG_STATUS']
863871
#str_ddb_format_sec = '%s'

requirements-dev.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
boto3==1.18.57
2+
pytest==6.2.5
3+
pytest-mock==3.6.1

test_create_boto3_config.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import handler
2+
import pytest
3+
4+
5+
@pytest.mark.parametrize('region', ['us-east-1', 'us-east-2'])
6+
def test_boto3_config_object(region, mocker):
7+
"""When the DNS record changes for the health API the config should be updated correctly"""
8+
mock_gethostname = mocker.patch('handler.socket.gethostbyname_ex')
9+
mock_gethostname.return_value = (f'health.{region}.amazonaws.com', ['global.health.amazonaws.com'], ['52.94.233.29'])
10+
assert handler.create_boto3_config().region_name == region

0 commit comments

Comments
 (0)