Terraform - Add a specific description for each group

Viewed 22

I need to put a specific description of a csv file to each group I created. On my csv, I have 2 columns: 1 for the group name and another for the description.

locals {
  Right_Groups   = [for x in csvdecode(file("${path.module}/_RightGroups.csv")) : x.droits_groups]
}

I create each group with :

resource "azuread_group" "Terra-Aad-Group-Right" {
  for_each = toset(local.Right_Groups)

  display_name     = lower(each.value)
  security_enabled = true
  description      = each.value
}

With this, the description is equal to the group name. If I set "each.value.description" to "description", it doesn't work.

The csv is like this :

droits_groups,description
con-inf-dev01,dev01
con-axw-rec01,rec01

Anyone have an idea ? Thank you.

1 Answers

I would suggest creating a map instead of list which you are converting to a set. This is because of the fact that sets will have the same keys and values [1]:

each.value — The map value corresponding to this instance. (If a set was provided, this is the same as each.key).

To convert the data to a map you could try:

Right_Groups = {for x in csvdecode(file("${path.module}/_RightGroups.csv")) : x.droits_groups => x.description}

This will create the following map:

> local.Right_Groups
{
  "con-axw-rec01" = "rec01"
  "con-inf-dev01" = "dev01"
}

Then, in the resource you would do:

resource "azuread_group" "Terra-Aad-Group-Right" {
  for_each = local.Right_Groups

  display_name     = lower(each.key)
  security_enabled = true
  description      = each.value
}

[1] https://www.terraform.io/language/meta-arguments/for_each#the-each-object

Related