Search code examples
azureazure-active-directoryterraforminfrastructure-as-code

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


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!


Solution

  • 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]
    }