A custom policy definition encodes an organization-specific business or security rule when no built-in Azure Policy definition fits. Assign it at a chosen scope to evaluate resource configurations and enforce requirements such as permitted deployment regions. section 10.2.3 section 10.3
A definition contains a rule that tests resource properties and an effect. Deny is evaluated when a resource is updated; DeployIfNotExists can remediate existing resources, for which gradual rollout is recommended. section 10.2.3
Restrict VM locations
Create a definition named allowedLocationsOnly with az policy definition create. Its rule checks virtual-machine location names and uses deny when the location is outside East US or East US 2. The resulting definition is identified as policyType Custom and has mode Indexed. section 10.3
Listing 10.3 Creating a custom policy definition
az policy definition create --name allowedLocationsOnly #1 --display-name 'Allowed Virtual Machine Locations Only' #2 --rules "{ \ #3 \"if\": \ { \"not\": { \ \"field\": \"Microsoft.Compute/virtualMachines/Locations[*].locationName\", \ \"in\": [ \ \"East US\", \ \"East US 2\" \ ] \ } \ }, \ \"then\": { \ \"effect\": \"deny\" \ } \ }" #1 The actual name of the custom policy definition #2 The display name of the custom policy definition #3 The rule of the custom policy definition
Use the listing as a copyable pattern: supply a definition name, display name, and JSON if/then rule, then replace fields, allowed values, and effect for another business rule. section 10.3
Group related custom definitions in a policy initiative when they should be applied together. Use Azure Policy evaluation results to find noncompliant resources; custom security initiatives can also appear in the Microsoft Defender for Cloud Regulatory Compliance dashboard. Assign policies at the highest practical scope to reduce overlap; where policies overlap, the most restrictive policy takes precedence. section 10.3 section 10.4
