-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow for s3 backend path style #53
Changes from all commits
249d8d2
f4a5750
6ce1ff6
149b08d
95eab83
954726c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# Custom | ||
.envrc | ||
.env | ||
.venv | ||
tmp/ | ||
|
||
.DS_Store | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -60,6 +60,7 @@ terraform { | |||
bucket = "<bucket>" | ||||
key = "<key>" | ||||
dynamodb_table = "<dynamodb_table>" | ||||
use_path_style = "<use_path_style>" | ||||
|
||||
access_key = "test" | ||||
secret_key = "test" | ||||
|
@@ -220,6 +221,7 @@ def generate_s3_backend_config() -> str: | |||
"key": "terraform.tfstate", | ||||
"dynamodb_table": "tf-test-state", | ||||
"region": get_region(), | ||||
"use_path_style": "false", | ||||
"endpoints": { | ||||
"s3": get_service_endpoint("s3"), | ||||
"iam": get_service_endpoint("iam"), | ||||
|
@@ -242,11 +244,19 @@ def generate_s3_backend_config() -> str: | |||
backend_config["endpoints"] = { | ||||
k: backend_config["endpoints"].get(k) or v | ||||
for k, v in configs["endpoints"].items()} | ||||
# If the endpoint is configured in the backend we use that endpoint | ||||
# otherwise we will use the default created for the provider. Note that the user | ||||
# can still override this value with `use_path_style`in backend tf config | ||||
if use_s3_path_style(backend_config.get("endpoints", {}).get("s3")): | ||||
backend_config["use_path_style"] = "true" | ||||
configs.update(backend_config) | ||||
if not DRY_RUN: | ||||
get_or_create_bucket(configs["bucket"]) | ||||
get_or_create_ddb_table(configs["dynamodb_table"], region=configs["region"]) | ||||
result = TF_S3_BACKEND_CONFIG | ||||
if is_tf_legacy: | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice backward compatibility proofing. I believe we should introduce the same logic for the override file too if we address this here. Fancy to add it somewhere here? Line 136 in 07643c4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked at that, and if I understand properly, you are suggesting to change the configuration of the aws provider from It can be quite the complex problem to define which version of a provider will be used as all modules are looked at by terraform before deciding on the providers version. Let me know if I am missing something 🤔 |
||||
result = result.replace("use_path_style", "force_path_style") | ||||
configs["force_path_style"] = configs.pop("use_path_style") | ||||
for key, value in configs.items(): | ||||
if isinstance(value, bool): | ||||
value = str(value).lower() | ||||
|
@@ -289,15 +299,17 @@ def check_override_file(providers_file: str) -> None: | |||
# AWS CLIENT UTILS | ||||
# --- | ||||
|
||||
def use_s3_path_style() -> bool: | ||||
def use_s3_path_style(endpoint: str = "") -> bool: | ||||
""" | ||||
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 | ||||
if endpoint: | ||||
endpoint = add_http_protocol(endpoint) | ||||
host = urlparse(endpoint or get_service_endpoint("s3")).hostname | ||||
except ValueError: | ||||
host = "" | ||||
|
||||
|
@@ -343,15 +355,20 @@ def deactivate_access_key(access_key: str) -> str: | |||
return "L" + access_key[1:] if access_key[0] == "A" else access_key | ||||
|
||||
|
||||
def add_http_protocol(url: str) -> str: | ||||
"""Add the http:// protocal if not found in the url""" | ||||
if "://" not in url: | ||||
return f"http://{url}" | ||||
return url | ||||
|
||||
|
||||
def get_service_endpoint(service: str) -> str: | ||||
"""Get the service endpoint URL for the given service name""" | ||||
# allow configuring a custom endpoint via the environment | ||||
env_name = f"{service.replace('-', '_').upper().strip()}_ENDPOINT" | ||||
env_endpoint = os.environ.get(env_name, "").strip() | ||||
if env_endpoint: | ||||
if "://" not in env_endpoint: | ||||
env_endpoint = f"http://{env_endpoint}" | ||||
return env_endpoint | ||||
return add_http_protocol(env_endpoint) | ||||
|
||||
# some services need specific hostnames | ||||
hostname = LOCALSTACK_HOSTNAME | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This must be added dynamically, see my other comment below why.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To keep transparency and a similar style, I left it here, but changed the default to false.