diff --git a/handler.py b/handler.py index 83c156f..6f2997e 100644 --- a/handler.py +++ b/handler.py @@ -21,21 +21,25 @@ 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 + +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 @@ -871,14 +875,14 @@ def get_sts_token(service): # 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 @@ -900,4 +904,4 @@ def main(event, context): describe_org_events(health_client) if __name__ == "__main__": - main('', '') + main('', '') \ No newline at end of file