MCPcopy Index your code
hub / github.com/aws/aws-cli / InstanceMetadataRegionFetcher

Class InstanceMetadataRegionFetcher

awscli/utils.py:165–209  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

163
164
165class InstanceMetadataRegionFetcher(IMDSFetcher):
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()
202 response = self._get_request(
203 url_path=self._URL_PATH,
204 retry_func=self._default_retry,
205 token=token,
206 )
207 availability_zone = response.text
208 region = availability_zone[:-1]
209 return region
210
211
212def split_on_commas(value):

Calls

no outgoing calls