Trying to set Azure PIM Role Settings for owner role via terraform includes Azure MFA, Activation hours, and also send notifications when eligible to activate this role via local_exec provider in terraform.
I tried this one:
resource "azurerm_role_assignment""pim_subscription_owner"{
scope="/subscriptions/<sub id>"
role_definition_id ="/subscriptions/id/providers/Microsoft.Authorization/roleDefinitions/<sub id>
principal_id=<principal_id>
lifecycle{
ignore_changes=[
role_definition_id,
principal_id
]
}
}
resource "azurerm_role_assignment""mfa_role_assignment"{
scope=azurerm_role_assignment.pim_subscription_owner.scope
role_definition_id="/providers/Microsoft.Authorization/roleDefinitions/"
principal_id=azurerm_role_assignment.pim_subscription_owner.principal_id
provisioner "local_exec" {
command = <<EOT
az ad sp mfa set --id ${azurerm_role_assignment.mfa_role_assignment.principal_id) --auth-type MFA
EOT
interpreter=["bash","-c"]
}
}
There is no direct way via
Terraformto update theAzure PIMrole settings, but you can use a PowerShell script inside Terraform by usingnull_resource.Here is the
PowerShellscript that updates thePIMrole settings, such as justification andMFArequirement on activation.PIMRole.ps1
Terraform file
Please ensure that the
PowerShellscript is in the same folder as the Terraform code before executing it.Reference: Update the justification, MFA, and ticketing rules required on activation
Get-AzureADMSPrivilegedRoleSetting