Repository: localstack/terraform-local (language: python)

Issue:
s3 access problem using compose
With the following configuration:
```
#docker-compose.yml
version: "3.8"

services:
    localstack:
  #canonical configuration - https://github.com/localstack/localstack/blob/master/docker-compose.yml
  [...]
    setup-infrastructure:
      volumes:
          - ./terraform:/usr/infrastructure
       working_dir: /usr/infrastructure
       command:
           - /bin/sh
           - -c
           - |
             tslocal init #-upgrade
             tslocal workspace select -or-create localstack
             terraform apply -auto-approve -lock=false
```

the only way to ensure that S3 works properly is by setting  `s3_use_path_style=true` and all endpoints to `http://localstack:4566`  as following:
```
#./terraform/main.ts 
provider "aws" {
    region = "eu-west-1"
    access_key = "anaccesskey"
    secret_key = "asecretkey"
    skip_credentials_validation = true
    skip_requesting_account_id = true 
    skip_metadata_api_check = true
    s3_use_path_style = true
    
    endpoints {
        s3  = "http://localstack:4566"
  }
}

resource "random_string" "id" {
  length    =   "5"
  special   =   false
  upper     =   false
}

resource "aws_s3_bucket" "bucket" {
  bucket    =   "mybucket-s3-g2-${random_string.id.result}"
}
```

This combination does not seem achievable through tflocal, probably due to https://github.com/localstack/terraform-local/blob/main/bin/tflocal#L214C9-L217

I'm working with WSL2(Win OS), btw.

https://github.com/lsfera/localstack-testing for a quick review 





Discussion / hints:

Fix patch:
diff --git a/README.md b/README.md
index 141c256..6e98829 100644
--- a/README.md
+++ b/README.md
@@ -39,6 +39,7 @@ please refer to the man pages of `terraform --help`.
 
 ## Change Log
 
+* v0.13: Fix S3 automatic `use_s3_path_style` detection when setting S3_HOSTNAME or LOCALSTACK_HOSTNAME
 * v0.12: Fix local endpoint overrides for Terraform AWS provider 5.9.0; fix parsing of alias and region defined as value lists
 * v0.11: Minor fix to handle boolean values in S3 backend configs
 * v0.10: Add support for storing state files in local S3 backends
diff --git a/bin/tflocal b/bin/tflocal
index 0ffaa13..696d060 100755
--- a/bin/tflocal
+++ b/bin/tflocal
@@ -10,11 +10,12 @@ your TF config.
 """
 
 import os
-import re
 import sys
 import glob
 import subprocess
 
+from urllib.parse import urlparse
+
 PARENT_FOLDER = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
 if os.path.isdir(os.path.join(PARENT_FOLDER, '.venv')):
     sys.path.insert(0, PARENT_FOLDER)
@@ -212,9 +213,18 @@ def generate_s3_backend_config() -> str:
 # ---
 
 def use_s3_path_style() -> bool:
-    """Whether to use S3 path addressing (depending on the configured S3 endpoint)"""
-    regex = r"^[a-z]+://(localhost|[0-9.]+)(:[0-9]+)?$"
-    return bool(re.match(regex, get_service_endpoint("s3")))
+    """
+    Whether to use S3 path addressing (depending on the configured S3 endpoint)
+    If the endpoint starts with the `s3.` prefix, LocalStack will recognize virtual host addressing. If the endpoint
+    does not start with it, use path style. This also allows overriding the endpoint to always use path style in case of
+    inter container communications in Docker.
+    """
+    try:
+        host = urlparse(get_service_endpoint("s3")).hostname
+    except ValueError:
+        host = ""
+
+    return not host.startswith("s3.")
 
 
 def get_region() -> str:
diff --git a/setup.cfg b/setup.cfg
index 3e716c3..3c63bef 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,6 +1,6 @@
 [metadata]
 name = terraform-local
-version = 0.12
+version = 0.13
 url = https://github.com/localstack/terraform-local
 author = LocalStack Team
 author_email = info@localstack.cloud


Test patch:
diff --git a/tests/test_apply.py b/tests/test_apply.py
index cb25a3f..10e29d6 100644
--- a/tests/test_apply.py
+++ b/tests/test_apply.py
@@ -34,6 +34,16 @@ def test_use_s3_path_style(monkeypatch):
     import_cli_code()
     assert use_s3_path_style()  # noqa
 
+    # test the case where the S3_HOSTNAME could be a Docker container name
+    monkeypatch.setenv("S3_HOSTNAME", "localstack")
+    import_cli_code()
+    assert use_s3_path_style()  # noqa
+
+    # test the case where the S3_HOSTNAME could be an arbitrary host starting with `s3.`
+    monkeypatch.setenv("S3_HOSTNAME", "s3.internal.host")
+    import_cli_code()
+    assert not use_s3_path_style()  # noqa
+
 
 def test_provider_aliases(monkeypatch):
     queue_name1 = f"q{short_uid()}"