def get_region_from_metadata():
 global __Location__
 if (__Location__ == 'do-not-get-from-metadata'):
  LOG.debug('Previously failed to get AWS region from metadata. Not trying again.')
  return None
 if (__Location__ != ''):
  return __Location__
 try:
  result = requests.get('http://169.254.169.254/latest/dynamic/instance-identity/document', proxies={'http': ''}, timeout=AWS_METADATA_TIMEOUT)
 except requests.exceptions.RequestException:
  LOG.warning('Failed to get AWS region from instance metadata.', exc_info=True)
  __Location__ = 'do-not-get-from-metadata'
  return None
 try:
  region = result.json()['region']
  __Location__ = region
  return __Location__
 except (ValueError, KeyError):
  LOG.warning('Failed to decode JSON from instance metadata.')
  return None
 return None

def get_region_from_metadata ( ) : global __Location__ if ( __Location__ == 'do-not-get-from-metadata' ) : LOG . debug ( 'Previously failed to get AWS region from metadata. Not trying again.' ) return None if ( __Location__ != '' ) : return __Location__ try : result = requests . get ( 'http://169.254.169.254/latest/dynamic/instance-identity/document' , proxies = { 'http' : '' } , timeout = AWS_METADATA_TIMEOUT ) except requests . exceptions . RequestException : LOG . warning ( 'Failed to get AWS region from instance metadata.' , exc_info = True ) __Location__ = 'do-not-get-from-metadata' return None try : region = result . json ( ) [ 'region' ] __Location__ = region return __Location__ except ( ValueError , KeyError ) : LOG . warning ( 'Failed to decode JSON from instance metadata.' ) return None return None
try to get region from instance identity document and cache it .