Lab 02 – Role-Based Access Control (RBAC) & Azure Policy
Objectives
- Assign built-in roles at different scopes (subscription, resource group, resource)
- Create a custom RBAC role
- Create and assign an Azure Policy definition and initiative
- Use Policy to enforce tagging and allowed locations
Prerequisites
- Owner or User Access Administrator on the subscription
- Lab 01 users/groups (optional, can use any test user)
Estimated Time
45 minutes total
- Search for Resource groups > Create
- Subscription: your subscription. Resource group:
rg-az104-lab02
- Region:
East US
- Select Review + create > Create
ℹ️
Tip
Resource groups are free containers. Always organize resources into RGs by project, environment, or cost center.
Understanding RBAC Scopes
❗
Important
RBAC assignments inherit down the hierarchy: Subscription → Resource Group → Resource. An assignment at the RG level applies to all resources within it.
- Go to rg-az104-lab02 > Access control (IAM) > Add > Add role assignment
- Role tab: select Reader. Select Next
- Members tab: Assign access to User, group, or service principal
- Select members > choose
grp-az104-lab (from Lab 01)
- Review + assign
⚠️
Warning
Resource-level assignments are more restrictive. Bob here can only access this one storage account, not others in the RG.
- Create a storage account (or use one from Lab 04)
- Open the storage account > Access control (IAM) > Add role assignment
- Role = Storage Blob Data Contributor > assign to Bob
- This grants Bob access only at this storage account scope, not the whole RG
Why Custom Roles?
ℹ️
Tip
Built-in roles like "Virtual Machine Contributor" are too broad. Custom roles let you grant minimal permissions—only what's needed.
- Go to the subscription > Access control (IAM) > Add > Add custom role
- Basics tab:
- Custom role name:
VM Operator (Start/Stop only)
- Description:
Can start and restart VMs but not create or delete them
- Baseline permissions: Clone a role > select Virtual Machine Contributor
- Permissions tab: select Edit permissions in JSON
- Replace the
actions/notActions arrays with:
{
"actions": [
"Microsoft.Compute/virtualMachines/start/action",
"Microsoft.Compute/virtualMachines/restart/action",
"Microsoft.Compute/virtualMachines/read"
],
"notActions": []
}
- Assignable scopes tab: confirm scope is your subscription
- Access control (IAM) > Add role assignment
- Search for VM Operator (Start/Stop only) under Custom roles
- Assign to
carol@<yourtenant>.onmicrosoft.com at rg-az104-lab02 scope
Understanding Policy
❗
Important
Policies PREVENT creation/modification of resources that don't comply. Unlike RBAC (who can do what), Policy enforces what is allowed to be created.
- Search for Policy > Definitions
- Search for "Require a tag on resource groups"
- Select it > Assign
- Basics tab: Scope = your subscription
- Parameters tab: set
Tag Name = CostCenter
- Effect: confirm it's Deny (blocks creation without tag)
- Review + create > Create
⚠️
Warning
This will block resource creation. Expect a policy error.
- Go to Resource groups > Create
- Name:
rg-policy-test, Region: East US
- Select Review + create and expect this error:
RequestDisallowedByPolicy
The request is denied by an Azure policy.
Reason: Resource group does not have required tags.
- Do not proceed past this validation error
- This proves the policy is working!
- Go to Policy > Definitions > + Policy Definition
- Name:
Allowed Azure Locations
- Category: General
- Description:
Restrict resource creation to approved regions only
- Policy rule (replace template with):
{
"policyRule": {
"if": {
"not": {
"field": "location",
"in": [
"East US",
"West US",
"West Europe"
]
}
},
"then": {
"effect": "Deny"
}
}
}
- Review + create > Create
- Policy > Assignments > Assign Policy
- Scope: your subscription
- Policy definition: select
Allowed Azure Locations
- Effect: Deny
- Review + create > Assign
- Try to create a resource in Central US (not in the allowed list)
- Expect a Deny error
- Try creating in East US (in the allowed list)
- Should succeed
Success Criteria
Lab Completion Checklist
- Resource group
rg-az104-lab02 created
- Reader role assigned to group at RG scope
- Storage Blob Data Contributor assigned to Bob at resource scope
- Custom role
VM Operator (Start/Stop only) created and assigned
- Tag policy prevents resource group creation without
CostCenter tag
- Location policy allows only East US, West US, West Europe
Cleanup (If Needed)
To remove policies and role assignments:
# Remove policy assignments
Remove-AzPolicyAssignment -Name "Require a tag on resource groups" -Scope "/subscriptions/[subscription-id]"
Remove-AzPolicyAssignment -Name "Allowed Azure Locations" -Scope "/subscriptions/[subscription-id]"
# Remove custom role
Remove-AzRoleDefinition -Id "[role-id]" -Force
# Remove resource group (includes all resources inside)
Remove-AzResourceGroup -Name "rg-az104-lab02" -Force
ℹ️
Tip
Keeping test RGs around helps you learn. Only delete if you're confident you won't need them.
Key Takeaways
- RBAC = WHO can do WHAT (identity-based)
- Policy = WHAT resources can be created (compliance-based)
- Scopes matter: Assignment at subscription affects all RGs; assignment at RG affects only resources in that RG
- Least privilege: Custom roles let you grant only what's needed
- Policy prevents: Unlike RBAC (which is permissive), Policy actively prevents non-compliant operations