Main.tf
locals {
location_mapping = [
{
"url": "L1.tfe.com"
"location": "L1"
"resource_group_name": "R1"
"name_log_workspace_name": "W1"
},
{
"url": "L2.tfe.com"
"location": "L2"
"resource_group_name": "R2"
"name_log_workspace_name": "W2"
},
{
"url": "L3.tfe.com"
"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 and url
condition in data block from locals.
Example
if I pass location value L2
and url value L2.tfe.com
then I will get name=W2
and resource_group_name=R2
I think something as the following should be suited, assuming there can be only one match:
variable "location" {
default = "L2"
}
variable "url" {
default = "L2.tfe.com"
}
locals {
location_mapping = [
{
"url": "L1.tfe.com"
"location": "L1"
"resource_group_name": "R1"
"name_log_workspace_name": "W1"
},
{
"url": "L2.tfe.com"
"location": "L2"
"resource_group_name": "R2"
"name_log_workspace_name": "W2"
},
{name_log_workspace_name
"url": "L3.tfe.com"
"location": "L3"
"resource_group_name": "R3"
"name_log_workspace_name": "W3"
}
]
selected_mapping = lookup({for val in local.location_mapping:
0 => val if val.location == var.location &&
val.url == var.url}, 0,
{
"url": "default"
"location": "default_L"
"resource_group_name": "default_R"
"name_log_workspace_name": "default_W"
})
}
data "azurerm_log_analytics_workspace" "example" {
name = local.selected_mapping.name_log_workspace_name
resource_group_name = local.selected_mapping.resource_group_name
}