@@ -121,6 +121,7 @@ Resources:
121121 import uuid
122122 import logging
123123 import jmespath
124+ import socket
124125 from datetime import date, datetime, timedelta, timezone
125126
126127 import boto3
@@ -284,6 +285,26 @@ Resources:
284285 event_details_per_affected.append(merged_dict)
285286 return event_details_per_affected
286287
288+ def get_active_health_region():
289+ """
290+ Get the active AWS Health region from the global endpoint
291+ See: https://docs.aws.amazon.com/health/latest/ug/health-api.html#endpoints
292+ """
293+
294+ default_region = "us-east-1"
295+
296+ try:
297+ (active_endpoint, _, _) = socket.gethostbyname_ex("global.health.amazonaws.com")
298+ except socket.gaierror:
299+ return default_region
300+
301+ split_active_endpoint = active_endpoint.split(".")
302+ if len(split_active_endpoint) < 2:
303+ return default_region
304+
305+ active_region = split_active_endpoint[1]
306+ return active_region
307+
287308 def lambda_handler(event, context): #pylint: disable=unused-argument
288309 """ this lambda collects AWS Health Events data
289310 and must be called from the corresponding Step Function to orchestrate
@@ -308,6 +329,7 @@ Resources:
308329 )['Credentials']
309330 health_client = boto3.client(
310331 'health',
332+ region_name=get_active_health_region(),
311333 aws_access_key_id=creds['AccessKeyId'],
312334 aws_secret_access_key=creds['SecretAccessKey'],
313335 aws_session_token=creds['SessionToken'],
0 commit comments