如何根据条件地形从当地人那里过滤数据?

How to filter data from locals based on condition terraform?

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"
}

我想根据本地数据块中的位置条件动态填充 nameresource_group_name

例子

如果我通过 location value L2 然后 name=W2resource_group_name=R2

如评论中所建议,您可以将 local.location_mapping 重新组织成映射,例如,调用 local.helper_map:

locals {
      helper_map = {for val in local.location_mapping:
                    val["location"]=>val}
}

然后在你的azurerm_log_analytics_workspace中你可以按如下方式使用它:

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"]
}