Home > database >  Add multiple users in ADD terraform/ create module to use in other env
Add multiple users in ADD terraform/ create module to use in other env

Time:04-01

Im currently looking for help with AAD and terraform. I need to create multiple users and make this as a modeule so I can use it in few environments. Does anyone did that and would be able to help?

variable.tf

variable "users" {
    type = map
    default = [
        [
        "user1",
        "user1display",
        "password119823"
        ],

        [
        "user2",
        "user2display",
        "password119823"
        ]
    ]
}

This variable return an error, that this wrong type. How should I declare it?

main.tf

resource "azuread_user" "team_user" {
  for_each = toset(var.users)  
  user_principal_name = "${each.value[0]}@${var.domain}"
  display_name = each.value[1]
  password = each.value[2]
  }

Is this loop done in good way? I'm kinda noob with creating multiple resources.

Thank you in advance!

CodePudding user response:

for_each can't iterate over list of list. So you can change it to a map of lists as follows:

resource "azuread_user" "team_user" {
  for_each = {for idx, user in var.users: idx=>user}  
  user_principal_name = "${each.value[0]}@${var.domain}"
  display_name = each.value[1]
  password = each.value[2]
}

CodePudding user response:

I think your data structure passing to for_each is not ideal. It would look much neater if you simply go for something like this.

---> sorry the edited working solution :)

variable "users" {
    type = map(any)
    default =  {
  user1 = {
    display = "user1display"
    password = "password119823"
  }
  user2 = {
    display = "user2display"
    password = "password119823"
  }
}

resource "azuread_user" "team_user" {
  for_each = var.users
  
  user_principal_name = "${each.key}@${var.domain}"
  display_name = each.value.display
  password = each.value.password
}
  • Related