I have a policy deployed via bicep with the following ruleset:
if: {
allOf: [
{
field: 'type'
equals: 'Microsoft.Resources/subscriptions/resourceGroups'
}
{
field: '[concat(\'tags[\', \'DeployedByIaC\', \']\')]'
equals: 'True'
}
]
}
then: {
effect: 'deployIfNotExists'
details: {
type: 'Microsoft.Authorization/locks'
roleDefinitionIds: [
'/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c' // Contributor role
]
existenceCondition: {
field: 'Microsoft.Authorization/locks/level'
equals: 'ReadOnlyLock'
}
deployment: {
properties: {
mode: 'Incremental'
template: {
'$schema': 'https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#'
contentVersion: '1.0.0.0'
resources: [
{
type: 'Microsoft.Authorization/locks'
apiVersion: '2016-09-01'
name: 'ReadOnlyLock'
properties: {
level: 'ReadOnly'
notes: 'This lock is applied by policy.'
}
}
]
}
}
}
}
}
All works fine and it scans for the tag no problem. But when it wants to deploy the lock i get the error it does not have enough rights
The assigment bicep code looks like:
resource createAssignment 'Microsoft.Authorization/policyAssignments@2023-04-01' = if (policy.builtin == false) {
name : policy.name
identity: {
type: 'SystemAssigned'
}
location: location
properties: {
displayName: policy.displayName
policyDefinitionId: policy.id
}
}
Rewritten the policy rule but no effect. I want to deploy this policy so when it gets the tag DeployedByIac it locks the resource group.
You have not granted your
managed identitythe right permissions to perform aresource lock.It requires
Microsoft.Authorization/*orMicrosoft.Authorization/locks/*actions, which only theOwnerand theUser Access Administratorbuilt-in roles have.You are using
Contributor.Owner:8e3af657-a8ff-443c-a75c-2fe8c4bcb635.User Access Administrator:18d7d88d-d35e-4fb5-a5c3-7773c20a72d9These roles are heavily overprivileged for what you want to accomplish, so you are better off by creating a custom role for your use-case.https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/lock-resources?tabs=json&wt.mc_id=MVP_323223#who-can-create-or-delete-locks
I can also see that there is a new role
Storage Account Backup Contributorwhich can perform:Microsoft.Authorization/locks/readMicrosoft.Authorization/locks/writeMicrosoft.Authorization/locks/delete*** Updated below due to new error from OP ***
existenceConditiononly proceed iftrue, unlikepolicyRulethat only proceed iffalse. https://learn.microsoft.com/en-us/azure/governance/policy/concepts/effects?wt.mc_id=MVP_323223#deployifnotexists-propertiesHowever, there is no such thing as
ReadOnlyLock. The correct value isReadOnly.https://learn.microsoft.com/en-us/azure/templates/microsoft.authorization/locks?pivots=deployment-language-bicep&wt.mc_id=MVP_323223#managementlockproperties