Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions awscli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def _create_fetcher(self):


class InstanceMetadataRegionFetcher(IMDSFetcher):
_URL_PATH = 'latest/meta-data/placement/availability-zone/'
_URL_PATH = 'latest/meta-data/placement/region'

def retrieve_region(self):
"""Get the current region from the instance metadata service.
Expand Down Expand Up @@ -204,9 +204,7 @@ def _get_region(self):
retry_func=self._default_retry,
token=token,
)
availability_zone = response.text
region = availability_zone[:-1]
return region
return response.text


def split_on_commas(value):
Expand Down
10 changes: 4 additions & 6 deletions tests/functional/test_clidriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ def test_imds_region_is_used_as_fallback_wo_v2_support(self):
# First response should be from the IMDS server for security token
# if server supports IMDSv1 only there will be no response for token
self.add_response(None)
# Then another response from the IMDS server for an availability
# zone.
self.add_response(b'us-mars-2a')
# Then another response from the IMDS server for the region.
self.add_response(b'us-mars-2')
# Once a region is fetched form the IMDS server we need to mock an
# XML response from ec2 so that the CLI driver doesn't throw an error
# during parsing.
Expand All @@ -84,9 +83,8 @@ def test_imds_region_is_used_as_fallback_with_v2_support(self):
# First response should be from the IMDS server for security token
# if server supports IMDSv2 it'll return token
self.add_response(b'token')
# Then another response from the IMDS server for an availability
# zone.
self.add_response(b'us-mars-2a')
# Then another response from the IMDS server for the region.
self.add_response(b'us-mars-2')
# Once a region is fetched form the IMDS server we need to mock an
# XML response from ec2 so that the CLI driver doesn't throw an error
# during parsing.
Expand Down
24 changes: 23 additions & 1 deletion tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def setUp(self):
self._send = self._urllib3_patch.start()
self._imds_responses = []
self._send.side_effect = self.get_imds_response
self._region = 'us-mars-1a'
self._region = 'us-mars-1'
self.environ = {}
self.environ_patch = mock.patch('os.environ', self.environ)
self.environ_patch.start()
Expand Down Expand Up @@ -555,6 +555,28 @@ def test_400_response_returns_none(self):
).retrieve_region()
self.assertEqual(result, None)

def test_retrieve_region_local_zone_instance(self):
_imds_responses = {
'api/token': b'my-token',
'placement/region': b'us-east-1',
'placement/availability-zone': b'us-east-1-atl-2a',
}

def get_response(request):
for path, body in _imds_responses.items():
if path in request.url:
return botocore.awsrequest.AWSResponse(
url=request.url,
status_code=200,
headers={},
raw=RawResponse(body),
)
raise ValueError(f'Unexpected IMDS request: {request.url}')

self._send.side_effect = get_response
result = InstanceMetadataRegionFetcher().retrieve_region()
self.assertEqual(result, 'us-east-1')


class TestIMDSRegionProvider(BaseIMDSRegionTest):
def assert_does_provide_expected_value(
Expand Down