MCPcopy Create free account
hub / github.com/aws/aws-cli / retrieve_region

Method retrieve_region

awscli/utils.py:168–198  ·  view source on GitHub ↗

Get the current region from the instance metadata service. :rvalue: str :returns: The region the current instance is running in or None if the instance metadata service cannot be contacted or does not give a valid response. :rtype: None or str

(self)

Source from the content-addressed store, hash-verified

166 _URL_PATH = 'latest/meta-data/placement/availability-zone/'
167
168 def retrieve_region(self):
169 """Get the current region from the instance metadata service.
170
171 :rvalue: str
172 :returns: The region the current instance is running in or None
173 if the instance metadata service cannot be contacted or does not
174 give a valid response.
175
176 :rtype: None or str
177 :returns: Returns the region as a string if it is configured to use
178 IMDS as a region source. Otherwise returns ``None``. It will also
179 return ``None`` if it fails to get the region from IMDS due to
180 exhausting its retries or not being able to connect.
181 """
182 try:
183 region = self._get_region()
184 return region
185 except self._RETRIES_EXCEEDED_ERROR_CLS:
186 logger.debug(
187 "Max number of attempts exceeded (%s) when "
188 "attempting to retrieve data from metadata service.",
189 self._num_attempts,
190 )
191 except BadIMDSRequestError as e:
192 logger.debug(
193 "Failed to retrieve a region from IMDS. "
194 "Region detection may not be supported from this endpoint: "
195 "%s",
196 e.request.url,
197 )
198 return None
199
200 def _get_region(self):
201 token = self._fetch_metadata_token()

Calls 1

_get_regionMethod · 0.95