Provision Multiple Users in Azure Ad With Infrastructure as Code, Using Terraform

A couple of days ago I wrote a post on how you can get started with Terraform, and how you can use it to provision users and groups in your Azure Active Directory Tenant. In that post, I showed how you can hardcode a user’s information inside the Terraform main.tf file. Although you could create a terraform module and keep the user’s information in that module, it might be easier to use something like a CSV file.
So in this post, I will show how you can advance some of the Terraform I showed in the last post, and how you could create a setup for provisioning your users in a bit more production-ready setup.
If you haven’t read the last post you might want to check it out here: Managing Azure Ad Users and Groups With Infrastructure as Code Using Terraform - ScriptingChris
You will be able to find all code used in this post on my github: BlogContent/IaC/Terraform/tf_aad_users_and_groups02 at main · ScriptingChris/BlogContent (github.com)
Setting up the main.tf file and adding the Azure AD Provider
Just like in the last post I will create a new project folder, in which I will create a main.tf file. I will then add the provider-specific information for adding Azure AD as a provider.
|
|
Reading and looping through the users.csv
I have created a csv file named users.csv looking similar to the below and placed it inside my project folder.
first_name,last_name,department,job_title,domain_name
Max,Mayfield,HR,HR Assistant,scriptingchris.tech
Will,Byers,Finance,Accountant,scriptingchris.tech
Dustin,Henderson,IT,IT Supporter,scriptingchris.tech
In this example, I have provided the user attributes: first_name, last_name, department, and job_title. I will also provide a user_principal_name, a display_name, and a password, but these attributes will be created dynamically with terraform functions.
For the attributes, you can provide all of the user’s attributes you are used to from Azure AD. If you want to read more about it, you can have a look at the Azure AD Provider documentation: azuread_user | Resources | hashicorp/azuread | Terraform Registry.
For reading the csv file I will add the following code block to my main.tf file.
|
|
I will then create a foreach loop, which will loop through all the users in the csv file and provision them in Azure AD. This foreach loop will run inside the resource “azuread_user”
to create the loop add the following code block:
|
|
Now inside the foreach loop, I will start generating the user’s user_principal_name. To do this I can use a built-in Terraform function called format(). This can be used for combining multiple strings into a single string. You can read more about the function here.
for creating the user_principal_name I will add the following code block:
|
|
The format() function will start by defining that for the following lines it should convert to string and insert the strings characters. This is done by the verb “%s”. For this formatting it should format it like below:
first-character-of-first_name + . + lastname + @ + domain
example:
After the user_principal_name has been generated I will need to generate a password for the user. I do this with a code block very similar to creating the user_principal_name.
|
|
Because I am creating a very easy password I will set the attribute force_password_change to force the user to change their password the first time they login
|
|
Then after i have created a password i will need to create a display_name, and i can to this with the following code block
|
|
After creating the display name I will just add the rest of the attributes provided in the csv file.
|
|
and my complete main.tf file looking similar to below:
|
|
Provisioning the users in Azure Active Directory
Now to provision the users in Azure Active Directory you will need to start logging into azure-cli
|
|
After you have logged into azure cli you can start initializing your terraform project by running the command:
|
|
If the initialization was successful you validate your terraform file by running the command:
|
|
If your validation was successful you can create a terraform plan by running the command:
|
|
If the terraform plan was successful you should now be provided with an output showing you what exactly will be provisioned in Azure Active Directory.
Here is an example of one user
|
|
Now that you have reviewed your terraform plan, you are ready to provision the users in Azure AD. To do this run the command:
|
|
if everything went well you should now be able to see your new users in your Azure Active Directory
Now if you want to remove the resources you have created in your Azure Active Directory Tenant you can run the command:
|
|