Main.tf
locals {
location_mapping = [
{
"location": "L1"
"resource_group_name": "R1"
"name_log_workspace_name": "W1"
},
{
"location": "L2"
"resource_group_name": "R2"
"name_log_workspace_name": "W2"
},
{
"location": "L3"
"resource_group_name": "R3"
"name_log_workspace_name": "W3"
}
]
}
data "azurerm_log_analytics_workspace" "example" {
# Populate name and resource group based on var.location(L2) condition if location matches in locals
name = "W2"
resource_group_name = "R2"
}
I want to populate name and resource_group_name dynamically based on the location condition in data block from locals.
Example
if I pass location value L2
then name=W2
and resource_group_name=R2
As suggested in the comments, you can re-organize your local.location_mapping
into mapping, e.g., called local.helper_map
:
locals {
helper_map = {for val in local.location_mapping:
val["location"]=>val}
}
Then in your azurerm_log_analytics_workspace
you can use it as follows:
data "azurerm_log_analytics_workspace" "example" {
# Populate name and resource group based on var.location(L2) condition if location matches in locals
name = local.helper_map["L2"]["name_log_workspace_name"]
resource_group_name = local.helper_map["L2"]["resource_group_name"]
}