az login
export ARM_SUBSCRIPTION_ID=$(az account show --query id | xargs)
export ARM_ACCESS_KEY="<insert storage account access key used for backend state configs>"
az ad sp create-for-rbac --name "gh-actions-runbooks-ad" --role owner \
--scopes /subscriptions/{subscription-id} \
--sdk-auth
# Replace {subscription-id} with the subscription details
# The command should output a JSON object similar to this:
{
"clientId": "<GUID>",
"clientSecret": "<GUID>",
"subscriptionId": "<GUID>",
"tenantId": "<GUID>",
(...)
}
ARM_SUBSCRIPTION_ID="<azure_subscription_id>"
ARM_TENANT_ID="<azure_subscription_tenant_id>"
ARM_CLIENT_ID="<service_principal_appid>"
ARM_CLIENT_SECRET="<service_principal_password>"
As of this writing I couldn’t find an efficient CLI method for applying Azure AD roles to SPN’s as Azure CLI is unsupported and Powershell cmdlets, which are still in preview mode, gave errors leaving the portal as the best option.
azuread-create-users.py
script to generate the terraform syntax for your new user: python scripts/azuread-create-users.py
azuread-users-groups-roles
directory and paste the terraform code for your new user into the main.tf
file using either of the existing Engineering or Art AD Groups or create a new group. For example:resource "azuread_user" "raybrown" {
user_principal_name = "raybrown@jennasrunbooks.com"
display_name = "Ray Brown"
department = "Art"
password = "Super$ecret01@!"
force_password_change = true
}
github-actions
bot output from your PR, specifically the terraform plan results, which will perform the following functions on your behalf using the Azure SPN (ie gh-actions-runbooks-ad
): azuread-users-groups-roles
directory, perform your terraform apply
to create the new Azure AD user.If you have a number of users that already exist in your Azure AD and are looking to start managing this part of your cloud estate using Terraform, you can run the scripts/azuread-import-users.py
script which will extract a list of your current Azure AD user's Display Names, Principal Names and Departments associated with the current Azure tenant you are logged into (az login
). The script runs an az ad query capturing the user details and copies them to a tsv file which is then read by python and converted into Terraform syntax. Once you have your list of users, follow the Procedure above starting at step 8.
python scripts/azuread-import-users.py
# For much larger lists of users, save the python output to either a txt or tf file
python scripts/azuread-import-users.py > users.tf
Upon completion of the above procedure, you should now have a basic architecture started for implementing Azure IAM as code using Azure AD and Azure RM Terraform providers. A CI/CD pipeline is implemented using a Github Actions workflow generating Terraform format/init/validate/plan results based on PR commits providing a solution for code review by your team prior to invoking terraform apply
locally. Python scripts are available for generating terraform syntax for new Azure AD users (azuread-create-users.py
) and for extracting a list of existing Azure AD users which then generates the terraform code for the list of existing users to be imported into your terraform state (azuread-import-users.py
). While this approach might be a good initial start for managing your Azure IAM as code, alternatively there's a link in the references section below to another more elegant solution to try provided by HashiCorp which uses a for_each loop against a CSV file of users.