Skip to content

Commit 149b08d

Browse files
committed
Set use_path_style default to false and reuse use_s3_path_style to determine if s3 path style is required
1 parent 6ce1ff6 commit 149b08d

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

bin/tflocal

+18-6
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def generate_s3_backend_config() -> str:
221221
"key": "terraform.tfstate",
222222
"dynamodb_table": "tf-test-state",
223223
"region": get_region(),
224-
"use_path_style": "true",
224+
"use_path_style": "false",
225225
"endpoints": {
226226
"s3": get_service_endpoint("s3"),
227227
"iam": get_service_endpoint("iam"),
@@ -244,6 +244,11 @@ def generate_s3_backend_config() -> str:
244244
backend_config["endpoints"] = {
245245
k: backend_config["endpoints"].get(k) or v
246246
for k, v in configs["endpoints"].items()}
247+
# If the endpoint is configured in the backend we use that endpoint
248+
# otherwise we will use the default created for the provider. Note that the user
249+
# can still override this value with `use_path_style`in backend tf config
250+
if use_s3_path_style(backend_config.get("endpoints", {}).get("s3")):
251+
backend_config["use_path_style"] = "true"
247252
configs.update(backend_config)
248253
if not DRY_RUN:
249254
get_or_create_bucket(configs["bucket"])
@@ -294,15 +299,17 @@ def check_override_file(providers_file: str) -> None:
294299
# AWS CLIENT UTILS
295300
# ---
296301

297-
def use_s3_path_style() -> bool:
302+
def use_s3_path_style(endpoint: str = "") -> bool:
298303
"""
299304
Whether to use S3 path addressing (depending on the configured S3 endpoint)
300305
If the endpoint starts with the `s3.` prefix, LocalStack will recognize virtual host addressing. If the endpoint
301306
does not start with it, use path style. This also allows overriding the endpoint to always use path style in case of
302307
inter container communications in Docker.
303308
"""
304309
try:
305-
host = urlparse(get_service_endpoint("s3")).hostname
310+
if endpoint:
311+
endpoint = add_http_protocol(endpoint)
312+
host = urlparse(endpoint or get_service_endpoint("s3")).hostname
306313
except ValueError:
307314
host = ""
308315

@@ -348,15 +355,20 @@ def deactivate_access_key(access_key: str) -> str:
348355
return "L" + access_key[1:] if access_key[0] == "A" else access_key
349356

350357

358+
def add_http_protocol(url: str) -> str:
359+
"""Add the http:// protocal if not found in the url"""
360+
if "://" not in url:
361+
return f"http://{url}"
362+
return url
363+
364+
351365
def get_service_endpoint(service: str) -> str:
352366
"""Get the service endpoint URL for the given service name"""
353367
# allow configuring a custom endpoint via the environment
354368
env_name = f"{service.replace('-', '_').upper().strip()}_ENDPOINT"
355369
env_endpoint = os.environ.get(env_name, "").strip()
356370
if env_endpoint:
357-
if "://" not in env_endpoint:
358-
env_endpoint = f"http://{env_endpoint}"
359-
return env_endpoint
371+
return add_http_protocol(env_endpoint)
360372

361373
# some services need specific hostnames
362374
hostname = LOCALSTACK_HOSTNAME

0 commit comments

Comments
 (0)