Overview and What You Will Learn
Azure Policy is a service that evaluates Azure resources against rules you define, reports which ones are non-compliant, and can block or fix them. A policy is a single rule, and an initiative bundles several policies so you can assign a whole standard at once. In this lab, you will assign a built-in policy that requires a tag on resources, run it without enforcement to see what it would catch, fix a non-compliant resource, and then enforce it so non-compliant deployments are blocked.
- Explaining how Azure Policy differs from Azure RBAC
- Finding a built-in policy definition and assigning it at a scope
- Testing safely with
DoNotEnforcebefore enforcing - Reading compliance results and triggering an on-demand scan
- Enforcing a policy and seeing a deployment denied
- Choosing policy effects such as Audit, Deny and Modify
Why This Matters in Production
In an organisation with hundreds of engineers, such as a company like Atlassian, a request to please tag every resource with a cost center fails within weeks. Some people forget, others tag inconsistently, and finance cannot allocate cloud spend. Azure Policy turns that request into a rule the platform enforces, so a missing tag is caught at deployment time instead of discovered in the monthly bill.
Core Principles
RBAC and Azure Policy answer different questions. RBAC decides who is allowed to perform an action. Policy decides what a resource is allowed to look like, no matter who is deploying it. Both checks run when a request reaches Azure Resource Manager, so a user with Contributor rights can still be blocked by a policy.
allowed to act?} A -->|No| X1([Denied: no permission]) A -->|Yes| P{Policy
resource compliant?} P -->|No| X2([Denied: policy]) P -->|Yes| C[Resource created] classDef req fill:#D1FAE5,color:#1E293B classDef check fill:#F97316,color:#FFFFFF classDef deny fill:#EF4444,color:#FFFFFF classDef ok fill:#10B981,color:#FFFFFF class R req class A,P check class X1,X2 deny class C ok
Every policy has an effect that decides what happens to a non-compliant resource:
| Effect | What it does |
|---|---|
| Audit | Allows the resource and records it as non-compliant |
| Deny | Blocks the deployment with a policy error |
| Modify or Append | Adds or changes properties such as tags during deployment |
| DeployIfNotExists | Deploys a related resource, such as a diagnostic setting |
Policies are assigned at a scope, and an assignment applies to everything beneath it: a management group, subscription, resource group or single resource. To test safely, set the assignment's enforcement mode to DoNotEnforce. Azure still evaluates every resource and reports compliance, but it does not block anything.
DoNotEnforce] --> S2[Review
compliance report] S2 --> S3[Fix or exempt
non-compliant items] S3 --> S4[Enforce
the assignment] classDef step fill:#326CE5,color:#FFFFFF classDef go fill:#10B981,color:#FFFFFF class S1,S2,S3 step class S4 go
Detailed Step-by-Step Practical Lab
Set the variables used throughout the lab. Storage account names are globally unique, lowercase and 3 to 24 characters.
export RG=rg-policy-lab-indiaexport LOCATION=centralindiaexport SA_NAME=stpolicylab$RANDOM- Create the resource group and capture its ID for use as the assignment scope:
az group create --name $RG --location $LOCATION export RG_ID=$(az group show --name $RG --query id --output tsv)- Find the built-in policy that requires a tag. This definition denies resources that lack the tag, which is why the next steps use
DoNotEnforceto test it first:
export DEF_ID=$(az policy definition list \ --query "[?displayName=='Require a tag on resources'].name | [0]" \ --output tsv) echo $DEF_ID- Write the policy parameters to a file. The CLI expects JSON here, and a file avoids quoting problems:
cat > policy-params.json << 'EOF'{ "tagName": { "value": "CostCenter" }}EOF- Assign the policy to the resource group without enforcement, so it reports but blocks nothing:
az policy assignment create \ --name require-costcenter-tag \ --display-name "Require CostCenter tag" \ --scope $RG_ID \ --policy $DEF_ID \ --params policy-params.json \ --enforcement-mode DoNotEnforceRemember
DoNotEnforceis the safe way to preview any policy, including ones with a Deny effect. It evaluates resources and fills in the compliance report, but it never blocks a deployment.
- Create a resource without the required tag:
az storage account create \ --name $SA_NAME \ --resource-group $RG \ --location $LOCATION \ --sku Standard_LRSThe creation succeeds, because the assignment is not enforced.
- Start an on-demand compliance scan instead of waiting for the next automatic evaluation, then list the non-compliant resources. The scan can take several minutes:
az policy state trigger-scan --resource-group $RG az policy state list \ --resource-group $RG \ --filter "complianceState eq 'NonCompliant'" \ --query "[].{resource:resourceId, policy:policyAssignmentName}" \ --output tableThe storage account appears in the list. If the list is empty, wait a few minutes and run the query again.
- Fix the issue by adding the missing tag, then scan again to see the resource become compliant:
az resource tag \ --tags CostCenter=CC-4021 \ --resource-group $RG \ --name $SA_NAME \ --resource-type "Microsoft.Storage/storageAccounts" az policy state trigger-scan --resource-group $RG- Enforce the policy. Recreate the assignment without
DoNotEnforce, then try to create an untagged resource:
az policy assignment delete --name require-costcenter-tag --scope $RG_ID az policy assignment create \ --name require-costcenter-tag \ --display-name "Require CostCenter tag" \ --scope $RG_ID \ --policy $DEF_ID \ --params policy-params.json az storage account create \ --name ${SA_NAME}b \ --resource-group $RG \ --location $LOCATION \ --sku Standard_LRSThe last command fails with a RequestDisallowedByPolicy error that names the assignment. New assignments can take a few minutes to start enforcing, so if the creation unexpectedly succeeds, delete that account, wait, and try again. Then create it with the tag and it succeeds:
az storage account create \ --name ${SA_NAME}b \ --resource-group $RG \ --location $LOCATION \ --sku Standard_LRS \ --tags CostCenter=CC-4021- Clean up:
az policy assignment delete --name require-costcenter-tag --scope $RG_IDaz group delete --name $RG --yes --no-waitProduction Best Practices & Common Pitfalls
Common MistakeAssigning a new policy straight into enforcement. A small logic error can block a whole team's legitimate deployments the moment it goes live. Assign with
DoNotEnforce, read the compliance report, fix or exempt the gaps, and only then enforce.
TipBundle related policies into an initiative, such as tagging, encryption and allowed regions, and assign that once at a management group. One assignment is easier to review and cannot drift out of sync with its siblings.
- Policy complements RBAC and does not replace it. A user with Contributor access can still be blocked by Deny, and a user denied by RBAC never reaches the policy check.
- Deny does not touch existing resources. It blocks new and changed resources, while existing ones are only flagged as non-compliant.
- Evaluation is not instant. New assignments, and newly created resources, can take several minutes to appear in compliance data, so use
trigger-scanbefore concluding a policy is not working. - Use Modify or DeployIfNotExists to fix problems, not just report them. These effects need a managed identity with the right role, and existing resources are corrected through a remediation task.
- Use exemptions for justified exceptions. An exemption on a specific resource or scope is auditable, while carving the resource out of the assignment is not.
- Assign high and exempt low. Assigning at a management group or subscription gives consistent coverage, and the
--not-scopesoption can exclude a specific resource group.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
az policy definition list |
List built-in and custom policy definitions |
az policy assignment create --enforcement-mode DoNotEnforce |
Assign a policy that reports without blocking |
az policy state trigger-scan --resource-group RG |
Start an on-demand compliance evaluation |
az policy state list --filter "complianceState eq 'NonCompliant'" |
List non-compliant resources |
az policy assignment delete --name NAME --scope SCOPE |
Remove an assignment |
az policy exemption create |
Exempt a resource or scope from an assignment |
| Problem | Cause | Fix |
|---|---|---|
| Compliance list is empty | Evaluation has not finished yet | Run trigger-scan and wait a few minutes |
RequestDisallowedByPolicy |
A Deny policy blocked the deployment | Read the error for the assignment name and fix the resource |
| Untagged resource still created after enforcing | Assignment not yet propagated | Wait a few minutes and retry |
| Parameters rejected | Params file is not valid JSON or has a wrong name | Check the parameter name in the definition |
| Remediation does nothing | Assignment has no managed identity or role | Assign an identity with the needed role |