Previously in Azure Bicep, you had to use GUIDs for Role Assignments when referencing built-in roles (such as b24988ac-6180-42a0-ab88-20f7382dd24c for the Contributor role). This often made the code difficult to read and error-prone. And AI loves to invent GUIDs.
Thanks to a recent update (introduced in v0.42.1), Bicep now supports the roleDefinitions() function. This allows us to use readable role names for Built-in Roles.
Here is a short before and after example to show how this looks in practice.
Before: Role Assignment with GUID
Previously, we had to copy the corresponding ID straight from the documentation:
param principalId string
var contributorRoleId = 'b24988ac-6180-42a0-ab88-20f7382dd24c' // Contributor role
resource roleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(resourceGroup().id, principalId, contributorRoleId)
properties: {
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', contributorRoleId)
principalId: principalId
}
}
After: Using roleDefinitions()
With the new function, we simply pass the name of the role:
param principalId string
// We can resolve the ID via the function
var contributorRole = roleDefinitions('Contributor').id
resource roleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(resourceGroup().id, principalId, contributorRole)
properties: {
roleDefinitionId: contributorRole
principalId: principalId
}
}
This is significantly more readable and eliminates the need for comments explaining what those hardcoded GUIDs actually mean.
The Catch: No Type Checking
There is one small caveat to keep in mind right now: The passed names are not verified constants. This means that if a typo slips in (e.g., roleDefinitions('Contributer')), the Bicep Language Support Extension in VS Code will not highlight it as an error. The mistake will only become apparent at runtime during the actual deployment.
Despite this small drawback, the new feature is a win for the readability and maintainability of your Bicep templates.