Skip to content
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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

cloutierMat
Copy link

Motivation

Using tflocal as part of a docker-compose configuration requires to use path style for s3 bucket. While the provider configuration is enabling path style for buckets created by terraform, this configuration did not extend to s3 backend. As reported on issue #25 and in the Community Slack, use/force_path_style configuration was not respected

Changes

Set the default when overriding the backend to use path style. This decision came as this is also seems to be what is enabled by default when encountering s3 services. use_path_style has been in use since tf v1.6, prior version used force_path_style.
There is a bit of a hacky way to switch. Maybe if more deprecation issue arise, a more elegant solution would be required to not turn this into spaghetti 😄

Not In Scope

There are many other attributes not currently supported such as the mentioned encrypt and acl. Those changes might require a bit more testing with LocalStack and Terraform to make sure it stays compatible, so were left out of scope

Testing

No new test was introduced as the backend is already tested with legacy and latest Terraform cli. If I am missing a testing opportunity, I am all ears!

@lakkeger lakkeger self-requested a review March 21, 2024 11:01
Copy link
Collaborator

@lakkeger lakkeger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great changes and kudos to tackle this issue🤘
But some other angles need to be considered regarding the changes.

(I agree that sooner or later tflocal needs a proper rewrite as it grows.)

bin/tflocal Outdated
@@ -220,6 +221,7 @@ def generate_s3_backend_config() -> str:
"key": "terraform.tfstate",
"dynamodb_table": "tf-test-state",
"region": get_region(),
"use_path_style": "true",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure everyone would be happy if we pin this option here or force them to add it explicitly with false when they don't need it.
The best option probably if we introduce a similar logic like this instead:

if use_s3_path_style():

(Sidenote: users use tflocal in combination with real s3 backends too, so we must not break those setups either.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I will work on that!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I set the default to false and I am reusing the use_s3_path_style method so it should be easier for a user to use different combinations of backend/s3 target

@@ -60,6 +60,7 @@ terraform {
bucket = "<bucket>"
key = "<key>"
dynamodb_table = "<dynamodb_table>"
use_path_style = "<use_path_style>"
Copy link
Collaborator

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.

Copy link
Author

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.

@@ -247,6 +249,9 @@ def generate_s3_backend_config() -> str:
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:
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

if use_s3_path_style():

Copy link
Author

Choose a reason for hiding this comment

The 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 s3_use_path_style supported in provider >4 to s3_force_path_style for providers version <4.

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 🤔

@cloutierMat cloutierMat linked an issue Mar 22, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tflocal overwrites or removes some S3 backend configurations
2 participants