Azure Custom Roles, Deny Assignments and Conditions
-
Phase 0: Before you start
-
Phase 1: The foundation
-
Phase 2: Day-to-day operations
-
Phase 3: Enterprise architecture
-
Phase 4: Troubleshooting step by step
-
Phase 5: Final project and career
Azure custom roles cover the jobs no built-in role fits, like a network team that should edit firewall rules but never delete a whole security group, or an operator who can start and stop VMs but mustn’t resize them.
So when the built-in options are either too little or far too much, you write your own role.
This lesson covers three ways to make RBAC more precise: custom roles, conditions on role assignments, and deny assignments, which are the one thing in Azure that can say no to an Owner.
In the lab, Asha first gets a custom “NSG Rule Editor” role, then permission to hand out the Reader role and nothing else, and at the end you’ll run into a deny assignment created by a deployment stack.
| Time | You need | Cost | By the end you can |
|---|---|---|---|
| About 50 minutes | The CLI, Asha’s login, the management groups from Lesson 6 | Nothing (network security groups and roles cost nothing) | Write and test a custom role, limit what an access admin can grant, and recognise a deny assignment |
What it is, in one line
A custom role is your own list of allowed actions, a condition narrows an assignment further, and a deny assignment blocks actions even for Owners.
In Simple Words
A special key, a key with rules, and a sealed door
Go back to the caretaker and his key ring. Now and then no key on the ring fits the job, so he gets a locksmith to cut a special key that opens only the electricity cupboards, and that’s your custom role.
Another time he lets the head cleaner use the key-cutting machine, on one rule: “you can cut corridor keys and nothing else”. That rule is a condition.
Then there are doors the building owner has sealed, and the caretaker’s master key won’t open those either. That’s what a deny assignment is.
Engineer's View
How the three tools work
- Custom role: a JSON definition with
Actions,NotActions,DataActions,NotDataActionsandAssignableScopes. The actions are provider operation strings likeMicrosoft.Network/networkSecurityGroups/securityRules/write, and you can use wildcards such assecurityRules/*. One tenant can hold up to 5,000 custom roles. - Assignable scopes decide where the role can be assigned. You can list subscriptions and resource groups, or a single management group, though a custom role scoped to a management group can’t have
DataActions. - Conditions (ABAC) put a rule on a role assignment, written in a small expression language. Admins mostly use them to give Role Based Access Control Administrator while allowing only certain roles, or certain principal types, to be granted. They work on blob data too, for example “only blobs with this index tag”.
- Deny assignments block the listed actions for everyone at a scope apart from principals that are explicitly excluded. You can’t create one by hand. Azure creates them for features like deployment stacks with deny settings, managed applications and some managed services.
The order Azure checks things in matters here. It gathers the allows from role assignments (checking any conditions), then it checks deny assignments, and a matching deny beats any allow, Owner included.
So DenyAssignmentAuthorizationFailed tells you that you have permission and are being denied anyway.
az role assignment list won’t show deny assignments. To see them, go to Access control (IAM) > Deny assignments in the portal, or call the API at Microsoft.Authorization/denyAssignments.
How it works, step by step
Building a custom role is a short loop, and you go round it until the role is right:
- Write down the job in one sentence: “edit NSG rules, nothing else”.
- Find the operations with
az provider operation showinstead of guessing the names. - Write the JSON with the smallest set of actions and a narrow assignable scope.
- Create it and assign it to a test group at a test scope.
- Test as the user: the job you allowed works, and the next obvious step fails.
- Review it whenever Azure adds new operations, because a wildcard picks them up on its own.
Testing is the step people skip, and it’s where you find out that a role which “should work” is missing a read action the portal needs, or that a wildcard lets through something you never meant to allow.
Conditions go through the same loop, except you test both sides: a grant inside the rule works and a grant outside it fails.
Hands-on lab: a custom role, a condition and a deny
Before you run any command, double-check it
Read every command twice before you press Enter. Check the target: the system or account, the IDs, the names and the sizes. These commands are for learning and were tested in our lab, not on your system. At work, run the read-only list commands first, follow your change process, make sure a backup exists, and ask a second person to check anything that changes or deletes data. You run them at your own risk.
Your test object is one network security group (NSG), and NSGs are free. You’ll need two PowerShell windows again, your admin window and Asha’s window with $env:AZURE_CONFIG_DIR = 'C:\azure-lab\asha-cli', signed in as Asha the same way as in Lesson 7.
1Create a test NSG
In your admin window, run this:
az group create --name rg-saat-l08 --tags course=saatvedha-azure lesson=08 -o none
az network nsg create --resource-group rg-saat-l08 --name nsg-saat-l08 --tags course=saatvedha-azure lesson=08 -o none
$rg = az group show --name rg-saat-l08 --query id -o tsv
$grp = az ad group show --group grp-saat-helpdesk --query id -o tsv2Find the exact operation names
Run this:
az provider operation show --namespace Microsoft.Network --query "resourceTypes[?name=='networkSecurityGroups/securityRules'].operations[].name" -o tsvYou should see something like this:
Microsoft.Network/networkSecurityGroups/securityRules/read
Microsoft.Network/networkSecurityGroups/securityRules/write
Microsoft.Network/networkSecurityGroups/securityRules/delete3Write and create the custom role
Paste this whole block:
@'
{
"Name": "SaatVedha NSG Rule Editor",
"Description": "Read NSGs and add, change or remove their rules. Cannot create or delete NSGs.",
"Actions": [
"Microsoft.Network/networkSecurityGroups/read",
"Microsoft.Network/networkSecurityGroups/securityRules/*",
"Microsoft.Resources/subscriptions/resourceGroups/read"
],
"NotActions": [],
"DataActions": [],
"NotDataActions": [],
"AssignableScopes": [ "/providers/Microsoft.Management/managementGroups/mg-saatvedha" ]
}
'@ | Set-Content C:\azure-lab\role-nsg-rule-editor.json
az role definition create --role-definition "@C:\azure-lab\role-nsg-rule-editor.json" --query "{name:roleName, type:roleType}" -o tableYou should see something like this:
Name Type
------------------------- ----------
SaatVedha NSG Rule Editor CustomRoleThe assignable scope is the course management group, so the role can be used on any subscription under mg-saatvedha and nowhere else.
4Assign it and test as Asha
In your admin window, run this:
az role assignment create --assignee-object-id $grp --assignee-principal-type Group --role "SaatVedha NSG Rule Editor" --scope $rg -o noneWait two minutes, then run this in Asha’s window:
az network nsg rule create --resource-group rg-saat-l08 --nsg-name nsg-saat-l08 --name allow-https --priority 200 --direction Inbound --access Allow --protocol Tcp --destination-port-ranges 443 --query provisioningState -o tsv
az network nsg create --resource-group rg-saat-l08 --name nsg-saat-l08-new -o none
az network nsg delete --resource-group rg-saat-l08 --name nsg-saat-l08You should see something like this:
Succeeded
(AuthorizationFailed) The client 'asha.rao@priyakoutlook.onmicrosoft.com' ... does not have authorization to perform action
'Microsoft.Network/networkSecurityGroups/write' over scope '.../networkSecurityGroups/nsg-saat-l08-new' ...
(AuthorizationFailed) The client 'asha.rao@priyakoutlook.onmicrosoft.com' ... does not have authorization to perform action
'Microsoft.Network/networkSecurityGroups/delete' over scope '.../networkSecurityGroups/nsg-saat-l08' ...That’s the job and nothing more: Asha can add a rule but can’t create or delete security groups. A real “firewall change” role for a network operations team looks a lot like this.

5Let Asha hand out Reader, and only Reader
Back in Lesson 7, Asha couldn’t give anyone access. This time you give her group Role Based Access Control Administrator on the resource group, with a condition that allows only the Reader role (its ID is acdd72a7-3385-48ef-bd42-f606fba81ae7).
In your admin window, paste this:
@'
((!(ActionMatches{'Microsoft.Authorization/roleAssignments/write'})) OR (@Request[Microsoft.Authorization/roleAssignments:RoleDefinitionId] ForAnyOfAnyValues:GuidEquals {acdd72a7-3385-48ef-bd42-f606fba81ae7})) AND ((!(ActionMatches{'Microsoft.Authorization/roleAssignments/delete'})) OR (@Resource[Microsoft.Authorization/roleAssignments:RoleDefinitionId] ForAnyOfAnyValues:GuidEquals {acdd72a7-3385-48ef-bd42-f606fba81ae7}))
'@ | Set-Content C:\azure-lab\cond-reader-only.txt
az role assignment create --assignee-object-id $grp --assignee-principal-type Group --role "Role Based Access Control Administrator" --scope $rg --condition "@C:\azure-lab\cond-reader-only.txt" --condition-version "2.0" -o noneIn plain words the condition says: “if you’re writing a role assignment, the role has to be Reader, and if you’re deleting one, it has to be a Reader assignment”. Passing it with @file keeps PowerShell and the Windows az.cmd wrapper from mangling its brackets and quotes, because the CLI reads any argument starting with @ from that file.
Wait two minutes, then run this in Asha’s window:
$ravi = az ad user list --filter "startswith(displayName,'Ravi')" --query "[0].id" -o tsv
az role assignment create --assignee-object-id $ravi --assignee-principal-type User --role Reader --resource-group rg-saat-l08 --query roleDefinitionName -o tsv
az role assignment create --assignee-object-id $ravi --assignee-principal-type User --role Contributor --resource-group rg-saat-l08You should see something like this:
Reader
(AuthorizationFailed) The client 'asha.rao@priyakoutlook.onmicrosoft.com' ... does not have authorization to perform action
'Microsoft.Authorization/roleAssignments/write' over scope '.../resourceGroups/rg-saat-l08/providers/Microsoft.Authorization/roleAssignments/...' ...✅ Checkpoint: Asha can edit NSG rules but not create or delete NSGs, and she can grant Reader but not Contributor.
Break It, Then Fix It
The first mistake stops a custom role from being created at all, and the second shows you the only thing that beats an Owner.
1Break it: one missing letter in an action
In your admin window, run this:
(Get-Content C:\azure-lab\role-nsg-rule-editor.json -Raw).Replace('networkSecurityGroups/securityRules','networkSecurityGroup/securityRules').Replace('NSG Rule Editor','NSG Rule Editor 2') | Set-Content C:\azure-lab\role-typo.json
az role definition create --role-definition "@C:\azure-lab\role-typo.json"You’ll get an error like this:
(InvalidActionOrNotAction) 'Microsoft.Network/networkSecurityGroup/securityRules/*' does not match any of the actions supported by the providers.
Code: InvalidActionOrNotActionAzure checks each action against the list the resource providers publish, so a typo gets caught right here and doesn’t fail quietly later on. It’s the reason step 2 copies names from az provider operation show instead of typing them from memory.
There’s nothing to fix in Azure, because the role was never created. Delete the bad file:
Remove-Item C:\azure-lab\role-typo.json2Break it: an Owner meets a deny assignment
In your admin window, deploy an NSG through a deployment stack that denies deletes by pasting this:
@'
param location string = resourceGroup().location
resource nsg 'Microsoft.Network/networkSecurityGroups@2024-05-01' = {
name: 'nsg-saat-l08-stack'
location: location
tags: {
course: 'saatvedha-azure'
lesson: '08'
}
}
'@ | Set-Content C:\azure-lab\nsg-stack.bicep
az stack group create --name stk-saat-l08 --resource-group rg-saat-l08 --template-file C:\azure-lab\nsg-stack.bicep --deny-settings-mode denyDelete --action-on-unmanage deleteAll --yes -o none
az network nsg delete --resource-group rg-saat-l08 --name nsg-saat-l08-stackYou’ll get an error like this:
(DenyAssignmentAuthorizationFailed) The client 'priya.k_outlook.com#EXT#@...' with object id '...' has permission to perform
action 'Microsoft.Network/networkSecurityGroups/delete' on scope '.../networkSecurityGroups/nsg-saat-l08-stack'; however, the access
is denied because of the deny assignment with name 'Deny assignment '531c2711-...' created by Deployment Stack '/subscriptions/.../
resourceGroups/rg-saat-l08/providers/Microsoft.Resources/deploymentStacks/stk-saat-l08''.
Code: DenyAssignmentAuthorizationFailedYou’re an Owner, and the message even says you have permission, but the deny assignment still wins. You didn’t see it in az role assignment list because that only lists allows.
Find it like this:
az rest --method get --url "https://management.azure.com$rg/providers/Microsoft.Authorization/denyAssignments?api-version=2022-04-01" --query "value[].properties.denyAssignmentName" -o tsvYou should see something like this:
Deny assignment '531c2711-3f75-5170-ae80-6b06de7fc2e7' created by Deployment Stack '/subscriptions/.../stk-saat-l08'To fix it, take the protection away the same way it was added, by deleting the stack. With deleteAll it deletes the NSG it manages as well:
az stack group delete --name stk-saat-l08 --resource-group rg-saat-l08 --action-on-unmanage deleteAll --yes
az network nsg list --resource-group rg-saat-l08 --query "[].name" -o tsvYou should see this:
nsg-saat-l08
3Clean up
In Asha’s window:
az logout
Remove-Item Env:AZURE_CONFIG_DIR
Remove-Item C:\azure-lab\asha-cli -Recurse -ForceRun this in your admin window (a custom role can only be deleted once its assignments are gone):
az role assignment delete --scope $rg
az group delete --name rg-saat-l08 --yes
az role definition delete --name "SaatVedha NSG Rule Editor"
C:\azure-lab\Find-LabResources.ps1You should see this at the end:
Nothing tagged course=saatvedha-azure is left.✅ Checkpoint: You reproduced InvalidActionOrNotAction and DenyAssignmentAuthorizationFailed, and the custom role, assignments and group are gone.
This happened at work
The custom role that quietly grew
This one comes from an insurance company that created a custom role called “App Support” back in 2023. Someone was in a hurry and gave it Microsoft.Web/* and Microsoft.Storage/storageAccounts/*, so the support staff could restart web apps and look at storage settings.
Two years later an auditor asked why support staff could read customer documents, and nobody had meant to allow that. The wildcard storageAccounts/* took in listKeys/action, and an account key lets you read every blob in the account.
On top of that, the support portal had a “Connection strings” page for the web apps that the same wildcard made readable. Forty people had held this access the whole time.
What they changed: they rewrote each custom role with explicit actions, removed /* from providers that hold data, and added listKeys to NotActions wherever a wildcard stayed. Storage accounts moved to Entra ID sign-in with key access switched off. The custom roles now sit in Git with a reviewer, and once a quarter a script compares each role with the operations Azure has added since.
Common mistakes and troubleshooting
| Mistake | What you see | What to do |
|---|---|---|
| Typing operation names from memory | InvalidActionOrNotAction | Copy them from az provider operation show. |
| Wildcards on data-holding providers | People can list keys or secrets | Use explicit actions, or put listKeys and similar actions in NotActions. |
| Forgetting a read action | The portal shows “You don’t have access” on a page | Add the matching /read for the parent resource and for the resource group. |
| Deleting a custom role that is still assigned | The delete fails | Take its assignments off first. |
| Looking for denies with az role assignment list | “Owner but still denied” and no clue why | Look at Deny assignments in IAM, or call the denyAssignments API. |
| Passing a long condition inline on Windows | Strange parse errors | Save it in a file and pass --condition "@file". |
Interview questions
Click a question to open it. Each one is worded the way an interviewer would put it, and underneath you’ll find why they ask it and one way you could answer in your own words.
Fresher level
1. When would you create a custom role instead of using a built-in one?
Why they ask this: They want to see that you reach for built-in roles first and know when those don’t fit.
How you might answer: Only when no built-in role is close enough to the job. Say a team can edit NSG rules but mustn’t create or delete NSGs: Network Contributor gives too much and Reader gives too little. Even then I’d check the built-in list first, because Microsoft keeps adding service-specific roles.
2. What’s in a custom role definition?
Why they ask this: A basic structure question.
How you might answer: It has a name and a description, Actions and NotActions for the control plane, DataActions and NotDataActions for data, and AssignableScopes, which say where you’re allowed to assign the role.
3. What is a deny assignment?
Why they ask this: They want to know you’ve heard of the one thing that overrides Owner.
How you might answer: It blocks certain actions at a scope for everyone except the principals it excludes, and that includes Owners. You can’t create one yourself. Azure makes them for things like deployment stacks with deny settings and managed applications.
2–5 years experience
4. An Owner gets DenyAssignmentAuthorizationFailed. Walk me through what you’d check.
Why they ask this: They want to see you go past role assignments to deny assignments.
How you might answer: The error usually names the deny assignment and who created it, a deployment stack for example, so I’d look under Access control, Deny assignments, or query the denyAssignments API at that scope. Then I’d talk to whoever owns that stack or managed app. Adding more roles won’t help, the fix is changing their deny settings or exclusions.
5. How would you let a team lead grant access to their team without being able to grant Owner?
Why they ask this: They’re checking that you know role assignment conditions.
How you might answer: I’d give the lead Role Based Access Control Administrator at the team’s scope, with a condition that only lets them write assignments for a short list of roles, like Reader and Virtual Machine Contributor. When I tried it in my lab, the condition allowed only Reader, and granting Contributor failed with AuthorizationFailed.
6. A custom role works in the CLI but users say the portal shows errors. Why?
Why they ask this: They want awareness of the extra reads the portal does.
How you might answer: The portal reads more than the CLI command needs, things like the resource group, related resources or diagnostic settings, so the role is probably missing a /read action. I’d ask which page fails, find the failing call in the browser’s developer tools or the error message, and add only that read.
Senior level
7. How would you manage custom roles across many subscriptions?
Why they ask this: They want a scalable, reviewable approach.
How you might answer: I’d define them once with assignable scopes on a management group, so every subscription under it can use them. The JSON sits in Git and a pipeline deploys it, with a reviewer on each change. A scheduled job compares what’s in Azure with Git and also lists any new provider operations that the wildcards would pick up.
8. What are the risks of wildcards in custom roles?
Why they ask this: They want a real example, not a textbook answer.
How you might answer: A wildcard picks up new operations on its own. On a provider that holds data, that can mean something like listKeys on storage, which in effect gives full data access. So on those providers I’d use explicit actions or put the risky ones in NotActions, and look at the roles again when providers change.
9. Design access for a VM operations team that may start, stop and restart VMs but not change them.
Why they ask this: They want a concrete least-privilege design.
How you might answer: I’d write a custom role with Microsoft.Compute/virtualMachines/read, start/action, restart/action, deallocate/action and powerOff/action, plus read on the resource group. There’s no write in it, so they can’t resize a VM or change its disks. I’d assign it to the team’s group on the resource groups they look after, and use Azure Policy and tags so new VMs end up in those groups.
Quick quiz
Q1. What do AssignableScopes control?
- Who can use the role
- Where the role can be assigned
- How long the role lasts
- Which regions it works in
Show answer
B — They list the scopes where the role may be assigned.
Q2. Which error means a custom role uses an action name that doesn’t exist?
- AuthorizationFailed
- InvalidActionOrNotAction
- ScopeLocked
- RoleDefinitionNotFound
Show answer
B — Azure checks actions against the providers’ operation lists.
Q3. An Owner is blocked with DenyAssignmentAuthorizationFailed. What blocked them?
- A missing role
- A resource lock
- A deny assignment
- Azure Policy
Show answer
C — Deny assignments win over allows, even Owner.
Q4. Where can you see deny assignments?
- az role assignment list
- Access control (IAM) > Deny assignments, or the denyAssignments API
- Activity log only
- Azure Advisor
Show answer
B — They are separate from role assignments.
Q5. What did the condition in the lab allow Asha to do?
- Grant any role
- Grant only the Reader role
- Delete the resource group
- Create NSGs
Show answer
B — The condition only allowed writing and deleting Reader assignments.
Quick recap
You can now make access as wide as the job and no wider, and you know the one rule that beats Owner.
- Custom roles are JSON lists of actions with assignable scopes, and you copy the action names instead of guessing them.
- Wildcards grow over time, so keep them off providers that hold data.
- Conditions narrow an assignment, for example “may grant Reader only”.
- Deny assignments win over every allow and are created by features like deployment stacks.
- Test each role as the user: the job should work and the next step should fail.
Next lesson
Lesson 9: MFA, security defaults and Conditional Access → You’ll start an Entra ID P2 trial and build a Conditional Access policy for Asha, safely, starting in report-only mode.
Further reading: Azure custom roles
Want to go deeper into Azure custom roles? See Microsoft’s Azure custom roles, Delegate Azure role assignment management to others with conditions and Understand Azure deny assignments. You can also see the full plan for this course on the Azure Cloud Administration course page.
