Last active
March 9, 2023 11:15
-
-
Save brk3/8d90c1c3059fc70c96d07b1659b7c6dc to your computer and use it in GitHub Desktop.
Terraform nested looping and grouping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
terraform {} | |
variable "windows_web_apps" { | |
default = { | |
app1 = { | |
# ... | |
dynamic_app_settings = { | |
AZURE_CLIENT_ID = { | |
managed_identities = { | |
id_a = { | |
lz_key = "foo" | |
attribute_key = "client_id" | |
} | |
} | |
} | |
KEYVAULT_URL = { | |
keyvaults = { | |
vault_a = { | |
lz_key = "bar" | |
attribute_key = "auth_url" | |
} | |
} | |
} | |
} | |
} | |
app2 = { | |
# ... | |
dynamic_app_settings = { | |
AZURE_CLIENT_ID = { | |
managed_identities = { | |
id_b = { | |
lz_key = "foo" | |
attribute_key = "client_id" | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
locals { | |
_win_dynamic_settings_processed = { | |
for setting in flatten( | |
[ | |
for app, app_settings in var.windows_web_apps : [ | |
for setting_name, resources in app_settings.dynamic_app_settings : [ | |
for resource_type_key, resource in resources : [ | |
for object_id_key, object_attributes in resource : { | |
app = app | |
key = setting_name | |
#value = try(var.combined_objects[resource_type_key][object_attributes.lz_key][object_id_key][object_attributes.attribute_key], var.combined_objects[resource_type_key][var.client_config.landingzone_key][object_id_key][object_attributes.attribute_key]) | |
value = "..." | |
} | |
] | |
] | |
] | |
] | |
) : "${setting.app}" => { | |
"${setting.key}" = setting.value | |
} ... | |
} | |
win_dynamic_settings_processed = { | |
# The output from _win_dynamic_settings_processed looks like this: | |
# | |
# { | |
# "app1" = [ | |
# { | |
# "AZURE_CLIENT_ID" = "..." | |
# }, | |
# { | |
# "KEYVAULT_URL" = "..." | |
# }, | |
# ] | |
# } | |
# | |
# Merge the values so it looks like this: | |
# | |
# { | |
# "app1" = { | |
# "AZURE_CLIENT_ID" = "..." | |
# "KEYVAULT_URL" = "..." | |
# } | |
# } | |
# Ref: https://github.com/hashicorp/terraform/issues/22263#issuecomment-969549347 | |
for k, v in local._win_dynamic_settings_processed: k => merge(v...) | |
} | |
} | |
output "win_dynamic_settings_processed" { | |
value = local.win_dynamic_settings_processed | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment