aa-sum
aa-sum

Reputation: 1

Terraform: How to iterate over multiple values in object

Im trying to create a resource with a name and a description. Both are strings

VARIABLES.TF

variable "leaf_int_prof_group"{

type = object({
    name = string
    description = string
})

}

TERRAFORM.TFVARS

leaf_int_prof_group = {

{
   name = "Leaf_101"
   description = "This is a Leaf101"
}
{
   name = "Leaf_102"
   description = "This is a Leaf102"
}
{
   name = "Leaf_103"
   description = "This is a Leaf103"
}

}

MAIN.TF

How would i define the variables in main.tf (below doesnt seem to work??)

resource "aci_leaf_interface_profile" "LN_group" {

for_each = var.leaf_int_prof_group
    name = var.leaf_int_prof_group.name
    description = var.leaf_int_prof_group.description

}

Upvotes: 0

Views: 2923

Answers (1)

Marcin
Marcin

Reputation: 238209

Your code is not a valid TF code. Perhaps you want a list of objects:

variable "leaf_int_prof_group"{
    type = list(object({
        name = string
        description = string
    })) 
}

then

leaf_int_prof_group =  [
    {
       name = "Leaf_101"
       description = "This is a Leaf101"
    },
    {
       name = "Leaf_102"
       description = "This is a Leaf102"
    },
    {
       name = "Leaf_103"
       description = "This is a Leaf103"
    }   
]

and finally:

resource "some_resource" "some_name" {
    for_each    = {for idx, val in var.leaf_int_prof_group: idx => val}
    name        = each.value.name
    description = each.value.description
}

Upvotes: 2

Related Questions