Search code examples
azureterraformterraform-provider-azureinfrastructure-as-code

Terraform issue generating random password in for each


I am trying to generate a password when creating users in azuread, I have a sample code below. I get the following error: password is required when creating a new user. Any help is highly appreciated. Don't worry about the vars.tf, I have a variable users and a json file with user information which is used in the for_each loop.

terraform {
    required_providers {
        azuread = {
            source  = "hashicorp/azuread"
            version = "~> 2.0.0"
        }
    }
}

provider "azuread" {}

resource "random_password" "example" {
    length  = 16
    special = true
    lower   = true
    upper   = true
    numeric = true
}

resource "azuread_user" "users" {
    for_each              = var.users
    
    user_principal_name   = each.value.user_principal_name
    display_name          = each.value.display_name
    given_name            = each.value.given_name
    surname               = each.value.surname
    job_title             = each.value.job_title
    mail_nickname         = each.value.mail_nickname
    company_name          = each.value.company_name
    password              = random_password.example.result

    force_password_change = true
}

I also want to generate a unique password for each user creation, but not sure if this mechanism is catched in the for_each. The code do work if I type the string manually, e.g. password = "123".


Solution

  • The users was created without password.

    Passwords was required for the azuread version used.

    Bumped the version to avoid this.