From 50c48d400e0944af190ef4512d43f70a401e56bd Mon Sep 17 00:00:00 2001 From: Gerasimos Alexiou Date: Wed, 18 Oct 2023 16:48:40 +0300 Subject: [PATCH 1/2] fix for issue #68 remove inits from cold start --- handler.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/handler.py b/handler.py index 83c156f..c799b94 100644 --- a/handler.py +++ b/handler.py @@ -21,21 +21,14 @@ print("boto3 version: ",boto3.__version__) # query active health API endpoint -health_dns = socket.gethostbyname_ex('global.health.amazonaws.com') -(current_endpoint, global_endpoint, ip_endpoint) = health_dns -health_active_list = current_endpoint.split('.') -health_active_region = health_active_list[1] -print("current health region: ", health_active_region) - -# create a boto3 health client w/ backoff/retry -config = Config( - region_name=health_active_region, - retries=dict( - max_attempts=10 # org view apis have a lower tps than the single - # account apis so we need to use larger - # backoff/retry values than than the boto defaults - ) -) +def get_health_active_region(): + health_dns = socket.gethostbyname_ex('global.health.amazonaws.com') + (current_endpoint, global_endpoint, ip_endpoint) = health_dns + health_active_list = current_endpoint.split('.') + health_active_region = health_active_list[1] + print("current health region: ", health_active_region) + return health_active_region + # TODO decide if account_name should be blank on error # Get Account Name @@ -868,6 +861,15 @@ def get_sts_token(service): SECRET_KEY = acct_b['Credentials']['SecretAccessKey'] SESSION_TOKEN = acct_b['Credentials']['SessionToken'] + # create a boto3 health client w/ backoff/retry + config = Config( + region_name=get_health_active_region(), + retries=dict( + max_attempts=10 # org view apis have a lower tps than the single + # account apis so we need to use larger + # backoff/retry values than than the boto defaults + ) + ) # create service client using the assumed role credentials, e.g. S3 boto3_client = boto3.client( service, @@ -900,4 +902,4 @@ def main(event, context): describe_org_events(health_client) if __name__ == "__main__": - main('', '') + main('', '') \ No newline at end of file From 90f3e4596e90a8107f24ba61d31acc881d2eed48 Mon Sep 17 00:00:00 2001 From: Gerasimos Alexiou Date: Tue, 24 Oct 2023 13:39:56 +0300 Subject: [PATCH 2/2] correct bugs on handler.py --- handler.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/handler.py b/handler.py index c799b94..6f2997e 100644 --- a/handler.py +++ b/handler.py @@ -29,6 +29,17 @@ def get_health_active_region(): print("current health region: ", health_active_region) return health_active_region +def create_config_file(): + # create a boto3 health client w/ backoff/retry + config = Config( + region_name=get_health_active_region(), + retries=dict( + max_attempts=10 # org view apis have a lower tps than the single + # account apis so we need to use larger + # backoff/retry values than than the boto defaults + ) + ) + return config # TODO decide if account_name should be blank on error # Get Account Name @@ -861,26 +872,17 @@ def get_sts_token(service): SECRET_KEY = acct_b['Credentials']['SecretAccessKey'] SESSION_TOKEN = acct_b['Credentials']['SessionToken'] - # create a boto3 health client w/ backoff/retry - config = Config( - region_name=get_health_active_region(), - retries=dict( - max_attempts=10 # org view apis have a lower tps than the single - # account apis so we need to use larger - # backoff/retry values than than the boto defaults - ) - ) # create service client using the assumed role credentials, e.g. S3 boto3_client = boto3.client( service, - config=config, + config=create_config_file(), aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY, aws_session_token=SESSION_TOKEN, ) print("Running in member account deployment mode") else: - boto3_client = boto3.client(service, config=config) + boto3_client = boto3.client(service, config=create_config_file()) print("Running in management account deployment mode") return boto3_client