Azure Policy - Best Practices

Introduction

Azure Policy helps you enforce organizational standards and assess compliance at scale. This guide provides proven best‑practice patterns for building, deploying, and maintaining policies across your Azure environment.

Design Principles

Show Policy Effect Matrix
{
  "Effect": "Deny",
  "If": {
    "field": "type",
    "equals": "Microsoft.Storage/storageAccounts"
  },
  "Then": {
    "effect": "[parameters('effect')]"
  }
}

Deployment Strategies

Automate policy deployment using Azure CLI, PowerShell, or ARM templates.

Azure CLI Example

az policy definition create \
  --name "allowed-locations" \
  --display-name "Allowed Locations" \
  --description "Restricts resource locations to approved list." \
  --rules '{
    "if": {
      "field": "location",
      "notIn": "[parameters('allowedLocations')]"
    },
    "then": {
      "effect": "deny"
    }
  }' \
  --params '{
    "allowedLocations": {
      "type": "Array",
      "metadata": {
        "description": "The list of allowed Azure locations.",
        "displayName": "Allowed locations"
      }
    }
  }' \
  --mode All

ARM Template Snippet

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
      "type": "Microsoft.Authorization/policyDefinitions",
      "apiVersion": "2021-06-01",
      "name": "allowed-locations",
      "properties": {
        "displayName": "Allowed Locations",
        "policyType": "Custom",
        "mode": "All",
        "description": "Restricts resource locations to approved list.",
        "parameters": {
          "allowedLocations": {
            "type": "Array",
            "metadata": {
              "description": "The list of allowed Azure locations.",
              "displayName": "Allowed locations"
            }
          }
        },
        "policyRule": {
          "if": {
            "field": "location",
            "notIn": "[parameters('allowedLocations')]"
          },
          "then": {
            "effect": "deny"
          }
        }
      }
    }
  ]
}

Monitoring & Remediation

Integrate Azure Policy with Azure Monitor, Azure Sentinel, and Azure Automation for continuous compliance.

Sample Log Analytics Query

PolicyResources
| where ComplianceState == "NonCompliant"
| summarize Count = count() by PolicyDefinitionName, SubscriptionId
| order by Count desc

Sample Policies

Below are common policy scenarios with ready‑to‑use definitions.

PolicyEffectScopeLink
Allowed VM SKUsDenyManagement GroupView
Enforce TaggingDenySubscriptionView
Secure Transfer RequiredDenyResource GroupView

Additional Resources