terraform 从列表中获取索引值

terraform get index value from the list

如何将索引值分配给 terraform 变量

让我们说:

我的示例输入看起来, terraform.tfvars.json:

{
    "resource_groups": [
        {
            "name_suffix": "AI",
            "location": "westus2",
            "is_default": false
        },
        {
            "name_suffix": "Montoring",
            "location": "westus2",
            "is_default": false
        },
        {
            "name_suffix": "Base",
            "location": "westus2",
            "is_default": false
        },
        {
            "name_suffix": "Core",
            "location": "westus2",
            "is_default": true
        }

    ]
}

main.tf

 locals {
 # I tried like 

 default_rg_index = [for rg, index in var.resource_groups: index if try(rg.is_default, false) == true]
}

我希望 default_rg_index 分配 3,但它不起作用

rg, index应该是相反的。你也可以让它更简单:

default_rg_index = [for index, rg in var.resource_groups: index if rg.is_default]