All guides

Azure Policy: audit, deny and fix resources that break the rules

Azure Policy compares the properties of your resources with rules that you assign. Microsoft's guidance is to start with an audit effect in place of an enforcement effect, and to track how the definition affects your resources.

A person holding a phone above a laptop keyboard

Definitions, initiatives and assignments

  • A policy definition is a business rule in JSON. Its policyRule has an if block with the conditions and a then block with one effect.
  • An initiative, also referred to as a policySet, groups several definitions.
  • An assignment applies a definition or an initiative to a scope: a management group, a subscription, a resource group or an individual resource. Subscopes can be excluded. The assignment also carries the parameter values.

Azure Policy evaluates a resource on four occasions: the resource is created or updated in a scope with an assignment, a scope receives a new assignment, an existing assignment is updated, and the standard compliance evaluation cycle comes round, once every 24 hours.

The effects

Each definition has a single effect. Azure Policy supports eleven:

  • audit creates a warning event in the activity log for a non-compliant resource and does not stop the request. auditIfNotExists audits resources related to the one that matched.
  • deny prevents a create or update request that does not match the standard. The request returns a 403.
  • denyAction blocks an action on resources. The only action it supports today is DELETE.
  • modify adds, updates or removes properties or tags during creation or update. append adds fields to the request. Microsoft recommends modify for tags.
  • deployIfNotExists runs a template deployment when the related resource it checks for does not exist.
  • disabled turns a definition off for a single assignment.
  • manual lets you attest the compliance of resources yourself.
  • mutate is specific to Kubernetes policy definitions, and addToNetworkGroup to Azure Virtual Network Manager.

A definition can take its effect from a parameter. The assignment then chooses among the allowed values.

Two built-in policies

Allowed locations restricts the locations you can specify when deploying resources. Its parameters are listOfAllowedLocations and effect, which accepts Audit, Deny or Disabled and defaults to Deny. It excludes resource groups, which have a separate definition named Allowed locations for resource groups, and resources that use the global region.

Require a tag on resources takes one parameter, tagName, and its effect is fixed at deny. It does not apply to resource groups. Require a tag on resource groups is a separate definition.

Audit before deny

Microsoft's guidance is to start with an audit or auditIfNotExists effect in place of an enforcement effect such as deny, to track how a definition affects the resources in your environment.

Where the effect is a parameter, set it to Audit. You can also set the assignment's enforcement mode to DoNotEnforce. Resources are still evaluated, but the effect is not enforced when a resource is created or updated. Microsoft describes this mode as the way to review compliance impact before enforcement.

Shell

az policy assignment create \
  --name allowed-locations \
  --scope /subscriptions/<subscription-id> \
  --policy e56962a6-4747-49cd-b67b-bf8b01975c4c \
  -p "{ 'listOfAllowedLocations': { 'value': [ 'eastus', 'westus' ] } }" \
  --enforcement-mode DoNotEnforce

az policy assignment create \
  --name require-owner-tag \
  --scope /subscriptions/<subscription-id> \
  --policy 871b6d14-10aa-478d-b590-94f262ecfa99 \
  -p "{ 'tagName': { 'value': 'owner' } }" \
  --enforcement-mode DoNotEnforce

The two --policy values are the names of the built-in definitions, which are GUIDs. The regions and the tag name owner are choices made for this example, so put your own in their place.

Shell

az policy state summarize

az policy assignment update \
  --name require-owner-tag \
  --enforcement-mode Default

The first command returns the latest summary of non-compliant policy states for the current subscription. Review the non-compliant resources, then run the second command to enforce the effect.

Remediation tasks

The deny effect prevents create and update requests. Existing resources that match a deny definition are marked as non-compliant.

Resources that are non-compliant to a modify or deployIfNotExists policy can be brought into compliance through a remediation task. The built-in Inherit a tag from the resource group if missing uses modify in this way. Two conditions apply:

  • The assignment uses a managed identity. With the CLI, create it with --mi-system-assigned and --location on az policy assignment create.
  • The identity needs the roles that the definition lists under roleDefinitionIds. The portal grants them automatically. With any other method you grant them manually, or the remediation deployment fails.

Shell

az policy remediation create \
  --name fix-owner-tag \
  --policy-assignment <policy-assignment-id>

A single remediation task remediates one policy at a time. For an initiative assignment, you select the policy to remediate.

Mistakes to avoid

  1. Enforcement first. Start with an audit effect or DoNotEnforce, read the compliance results, then enforce.
  2. A missing role on the managed identity. Outside the portal, grant the roles yourself before you start a remediation task.
  3. Tags on resources only. The tag policies for resources and for resource groups are separate definitions.
  4. Overlapping deny policies. Each assignment is evaluated individually, and Microsoft calls the net result cumulative most restrictive. In its example, one policy allows only one region on a subscription and another allows a different region on a resource group inside it. With both set to deny, every new resource in that resource group is denied.
  5. Management group resources. Assigning a policy at the management group level is possible. Evaluation still covers only the resources at subscription level and at resource group level.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides