Vidya
Vidya

Reputation: 199

terraform get index value from the list

How to assign index value to terraform variable

lets say:

my example input looks, 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]
}

I am expecting default_rg_index to assign 3, but it is not working

Upvotes: 0

Views: 2085

Answers (1)

Marcin
Marcin

Reputation: 238249

rg, index should be opposite. You can also make it simpler:

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

Upvotes: 2

Related Questions