Skip to content

Commit e6b43e1

Browse files
committed
First iteration of remote state endpoint injection
1 parent 8626b26 commit e6b43e1

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

bin/tflocal

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ terraform {
7575
}
7676
}
7777
"""
78+
TF_REMOTE_STATE_CONFIG = """
79+
data "terraform_remote_state" "<name>" {
80+
backend = "s3"
81+
config = {<configs>
82+
}
83+
}
84+
"""
7885
PROCESS = None
7986
# some services have aliases which are mutually exclusive to each other
8087
# see https://registry.terraform.io/providers/hashicorp/aws/latest/docs/guides/custom-service-endpoints#available-endpoint-customizations
@@ -215,6 +222,9 @@ def create_provider_config_file(provider_file_path: str, provider_aliases=None)
215222
# create s3 backend config
216223
tf_config += generate_s3_backend_config()
217224

225+
# create remote state config
226+
tf_config += generate_remote_state_config()
227+
218228
# write temporary config file
219229
write_provider_config_file(provider_file_path, tf_config)
220230

@@ -370,6 +380,61 @@ def generate_s3_backend_config() -> str:
370380
result = result.replace("<configs>", config_options)
371381
return result
372382

383+
def generate_remote_state_config() -> str:
384+
"""
385+
Generate configuration for terraform_remote_state data sources to use LocalStack endpoints.
386+
Similar to generate_s3_backend_config but for terraform_remote_state blocks.
387+
"""
388+
tf_files = parse_tf_files()
389+
if not tf_files:
390+
return ""
391+
392+
result = ""
393+
for filename, obj in tf_files.items():
394+
if LS_PROVIDERS_FILE == filename:
395+
continue
396+
397+
data_blocks = ensure_list(obj.get("data", []))
398+
for data_block in data_blocks:
399+
terraform_remote_state = data_block.get("terraform_remote_state")
400+
if not terraform_remote_state:
401+
continue
402+
403+
for data_name, data_config in terraform_remote_state.items():
404+
if data_config.get("backend") != "s3":
405+
continue
406+
407+
# Create override for S3 remote state
408+
config_attrs = data_config.get("config", {})
409+
if not config_attrs:
410+
continue
411+
412+
# Set up default configs
413+
configs = {
414+
"bucket": config_attrs.get("bucket", "tf-test-state"),
415+
"key": config_attrs.get("key", "terraform.tfstate"),
416+
"region": config_attrs.get("region", get_region()),
417+
"endpoint": get_service_endpoint("s3"),
418+
}
419+
420+
# Update with user-provided configs
421+
configs.update(config_attrs)
422+
423+
# Generate config string
424+
config_options = ""
425+
for key, value in sorted(configs.items()):
426+
if isinstance(value, bool):
427+
value = str(value).lower()
428+
elif isinstance(value, (str, int, float)):
429+
value = f'"{value}"'
430+
config_options += f'\n {key} = {value}'
431+
432+
# Create the final config
433+
remote_state_config = TF_REMOTE_STATE_CONFIG.replace("<name>", data_name)
434+
remote_state_config = remote_state_config.replace("<configs>", config_options)
435+
result += remote_state_config
436+
437+
return result
373438

374439
def check_override_file(providers_file: str) -> None:
375440
"""Checks override file existance"""

0 commit comments

Comments
 (0)