Azure Policy
Write and assign Azure Policy definitions to enforce naming conventions, allowed resource types, and location restrictions automatically at subscription or resource group scope.
Azure Policy is a free Cloud & IT Cert Prep lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cloud & IT Cert Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Azure Policy?
Azure Policy is a governance service that allows you to define, assign, and manage rules — called policy definitions — that control or audit the configuration of Azure resources. Unlike RBAC, which controls who can take actions, Azure Policy controls what the resulting resource configuration is allowed to be. You use it to enforce organisational standards like naming conventions, approved resource types, and required security configurations.
Policy Definitions
A policy definition is a JSON document that defines a condition to evaluate against a resource and an effect to apply when the condition is met. Azure provides hundreds of built-in policy definitions covering common scenarios. You can also create custom policy definitions when built-in policies do not match your requirement. Policy definitions are reusable objects that must be assigned to a scope before they take effect.
// Example built-in policy: require a specific tag on all resources
// Policy definition logic (simplified):
{
'if': {
'field': 'tags[Environment]',
'exists': 'false'
},
'then': {
'effect': 'deny'
}
}Policy Effects
The effect in a policy definition determines what happens when the policy condition is triggered. Key effects include: Deny — blocks the resource creation or update that violates the policy; Audit — allows the action but logs a warning in the compliance report; Append — adds required fields (like tags) to a resource before creation; Modify — changes a property on existing or new resources; DeployIfNotExists — deploys a related resource if a condition is not met (e.g., deploying a diagnostics extension to every VM).
Policy Assignment
A policy assignment applies a policy definition to a specific scope: management group, subscription, or resource group. All resources within that scope (and child scopes) are evaluated against the assigned policy. Assignments can include parameters — for example, a policy that restricts allowed Azure regions can be parameterised so the same definition is assigned with different region lists in different management groups.
# Assign a built-in policy to restrict allowed locations
az policy assignment create \
--name 'allowed-locations' \
--policy 'e56962a6-4747-49cd-b67b-bf8b01975c4c' \
--scope /subscriptions/<sub-id> \
--params '{"listOfAllowedLocations": {"value": ["eastus", "westeurope"]}}'
# List all policy assignments in a subscription
az policy assignment list \
--scope /subscriptions/<sub-id> \
--output tableInitiative Definitions (Policy Sets)
An initiative definition (also called a policy set) groups multiple related policy definitions into a single assignable package. Instead of assigning 20 individual policies, you assign one initiative. Azure provides built-in initiatives for compliance frameworks like ISO 27001, NIST SP 800-53, Azure Security Benchmark, and PCI DSS. Assigning the CIS Microsoft Azure Foundations Benchmark initiative, for example, evaluates your subscription against over 180 security controls simultaneously.
# Assign the Azure Security Benchmark initiative
az policy assignment create \
--name 'azure-security-benchmark' \
--policy-set-definition '1f3afdf9-d0c9-4c3d-847f-89da613e70a8' \
--scope /subscriptions/<sub-id>Compliance Reporting
After assigning a policy, Azure evaluates all existing resources in the scope and generates a compliance report. The report shows the overall compliance percentage (compliant resources / total resources), the compliance state of each resource, and which specific policy conditions failed. You can filter by policy initiative, scope, or resource type. Non-compliant resources are listed with the exact policy rule they violated, guiding remediation.
# Get policy compliance summary
az policy state summarize \
--subscription <sub-id> \
--output json
# List all non-compliant resources
az policy state list \
--filter 'complianceState eq NonCompliant' \
--output tableRemediation Tasks
For policies with the DeployIfNotExists or Modify effect, Azure can create remediation tasks to bring existing non-compliant resources into compliance. For example, a DeployIfNotExists policy that deploys a Log Analytics agent to every VM can have a remediation task triggered to install the agent on all existing VMs that currently lack it. New resources are automatically remediated at creation; existing resources need an explicit remediation task.
# Create a remediation task for existing non-compliant resources
az policy remediation create \
--name 'remediate-vm-logs' \
--policy-assignment 'deploy-vm-log-analytics' \
--resource-group myRGPolicy Exemptions
Sometimes specific resources legitimately need to be excluded from a policy — a legacy resource that cannot be modified, or an approved exception documented in a change request. Policy exemptions allow you to exclude a specific resource or resource group from a policy assignment without removing the policy globally. Exemptions have an optional expiry date and a category (Waiver or Mitigated) to document the reason for the exception.
# Create a policy exemption for a specific resource group
az policy exemption create \
--name 'legacy-rg-exemption' \
--policy-assignment 'allowed-locations' \
--scope /subscriptions/<sub-id>/resourceGroups/legacyRG \
--exemption-category Waiver \
--expires-on '2027-01-01T00:00:00Z' \
--description 'Legacy workload pending migration'DeployIfNotExists: Configuration at Scale
DeployIfNotExists is one of the most powerful policy effects. When a resource is created without a required related resource (e.g., a VM without a diagnostic extension), the policy automatically triggers an ARM template deployment to add the missing component. This enables Azure Policy to become an automatic configuration management tool — ensuring every VM always has monitoring agents, every storage account always has advanced threat protection, and every SQL Database always has auditing enabled.
Common Policy Use Cases
Organisations use Azure Policy for many governance scenarios. Location restrictions — only allow resources in approved Azure regions for data sovereignty. Allowed resource types — prevent deployment of expensive or unapproved services. Tag enforcement — require CostCentre and Environment tags on all resources. SKU restrictions — limit VM sizes to cost-appropriate tiers in non-production subscriptions. Security baselines — enforce TLS 1.2 minimum, disable public network access on storage accounts, and require private endpoints.
Azure Policy vs RBAC
It is important to understand the different roles of RBAC and Azure Policy. RBAC controls who can perform operations — it grants or denies actions based on identity. Azure Policy controls what the resource configuration must be — it evaluates and enforces properties regardless of who performs the action. Both are needed in a well-governed environment: RBAC ensures only authorised people deploy resources, and Azure Policy ensures those resources comply with configuration standards.
Quick Check
Test your understanding of Microsoft Azure Fundamentals (AZ-900) concepts from this lesson.
Lesson Recap
In this lesson you learned: Azure Policy defines conditions and effects to control resource configuration at any scope, initiative definitions bundle multiple policies into a single assignable compliance package, and DeployIfNotExists enables automatic configuration enforcement for missing components at scale. Next up we explore resource tags and resource locks for protecting Azure resources.
Frequently asked questions
Is the “Azure Policy” lesson free?
Yes — the full text of “Azure Policy” is free to read here on the web, and the Cloud & IT Cert Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cloud & IT Cert Prep course, upgrade to CoddyKit PRO.
What will I learn in “Azure Policy”?
Write and assign Azure Policy definitions to enforce naming conventions, allowed resource types, and location restrictions automatically at subscription or resource group scope. You practise Cloud & IT Cert Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Cloud & IT Cert Prep?
No prior experience is required. Cloud & IT Cert Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Azure Policy” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Cloud & IT Cert Prep lesson?
Yes. Every Cloud & IT Cert Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.