Abraham
Abraham

Reputation: 379

Terraform get element name from the list

Let's say I have a list of variables similar to the following:

"first" = {
    action = "trigger"
    env    = "prod"
}

I know that I can get the values of action and env variables in the foreach loop using ${each.value["action"]} and ${each.value["env"]}. How would I go around getting the "first" field (name of the element in a list)?

I would appreciate any help on this!

Upvotes: 0

Views: 810

Answers (1)

Marko E
Marko E

Reputation: 18103

Please understand first how for_each works and what each object [1] is. The for_each meta-argument can work with sets or maps. You are using maps. A map is represented by keys and values. Since you already know how to use values, the way to get a key in a map is with each.key. If your map has more than one key, you can get all of the keys with the built-in keys function [2].


[1] https://developer.hashicorp.com/terraform/language/meta-arguments/for_each

[2] https://developer.hashicorp.com/terraform/language/functions/keys

Upvotes: 2

Related Questions