8000 Give precedence to region override over config (#8997) · localstack/localstack@4548bf4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4548bf4

Browse files
Give precedence to region override over config (#8997)
1 parent 94483ab commit 4548bf4

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

localstack/aws/connect.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from localstack import config as localstack_config
2020
from localstack.constants import (
21+
AWS_REGION_US_EAST_1,
2122
INTERNAL_AWS_ACCESS_KEY_ID,
2223
INTERNAL_AWS_SECRET_ACCESS_KEY,
2324
MAX_POOL_CONNECTIONS,
@@ -465,6 +466,14 @@ def get_client(
465466
else:
466467
config = self._config.merge(config)
467468

469+
# Boto has an odd behaviour when using a non-default (any other region than us-east-1) in config
470+
# If the region in arg is non-default, it gives the arg the precedence
471+
# But if the region in arg is default (us-east-1), it gives precedence to one in config
472+
# Below: always give precedence to arg region
473+
if config and config.region_name != AWS_REGION_US_EAST_1:
474+
if region_name == AWS_REGION_US_EAST_1:
475+
config = config.merge(Config(region_name=region_name))
476+
468477
endpoint_url = endpoint_url or get_local_service_url(service_name)
469478
if service_name == "s3":
470479
if re.match(r"https?://localhost(:[0-9]+)?", endpoint_url):

tests/unit/aws/test_connect.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from unittest.mock import ANY, MagicMock, patch
22

33
import boto3
4+
import botocore
45
import pytest
56

67
from localstack.aws.api import RequestContext
@@ -426,3 +427,16 @@ def echo_request_handler(_: HandlerChain, context: RequestContext, response: Res
426427
clients.lambda_.list_functions()
427428

428429
assert test_params == expected_result
430+
431+
def test_region_override(self):
432+
# Boto has an odd behaviour when using a non-default (any other region than us-east-1) in config
433+
# If the region in arg is non-default, it gives the arg the precedence
434+
# But if the region in arg is default (us-east-1), it gives precedence to one in config
435+
# This test asserts that this behaviour is handled by client factories and always give precedence to arg region
436+
437+
factory = ExternalClientFactory()
438+
439+
config = botocore.config.Config(region_name="eu-north-1")
440+
441+
assert factory(region_name="us-east-1", config=config).s3.meta.region_name == "us-east-1"
442+
assert factory(region_name="us-west-1", config=config).s3.meta.region_name == "us-west-1"

0 commit comments

Comments
 (0)
0