OnCallReadyStart learning

Azure Lesson 32.12 · Azure IV: Governance, Storage & Backup · 19 min read

Policy that fixes things: modify, deployIfNotExists, remediation, initiatives and your own rules

In plain words

Some house rules don't just refuse things, they fix them. "Every box gets the department's label" - and a helper sticks the label on as the box comes in. "Every computer needs a backup" - and a helper sets the backup up.

The helper is a robot with its own badge (a managed identity), and the badge needs the right access or the robot can't do anything. Boxes that were already in the building before the rule existed only get fixed when you send the robot round on purpose (a remediation task).

Policy that fixes things: modify, deployIfNotExists, remediation, initiatives and your own rules

Deny and audit only judge. The other effects change things: modify adds the missing tag as the resource is created, deployIfNotExists (DINE) turns on the backup or the diagnostic setting nobody remembered. They are what keeps a large estate tagged and backed up without tickets. They also have two catches that trip people in exams and in real life: they need a managed identity with a role, and they do not touch resources that already exist until you run a remediation task.

Need to know:

  • modify: adds, replaces or removes tags and a few properties (the ones whose aliases are marked modifiable) on create and update. append: the older effect, adds fields to the request, without remediation support.
  • auditIfNotExists (AINE) / deployIfNotExists (DINE): run after a resource is created or updated. They look for a related resource (details.type, e.g. a backup protected item for a VM) and report it missing (AINE) or deploy it with an ARM template (DINE).
  • An assignment with modify or DINE needs a managed identity (system- or user-assigned) and that identity needs the roles the definition lists (roleDefinitionIds) at the scope it fixes.
  • Existing resources: the assignment only marks them non-compliant. A remediation task (az policy remediation create) applies the effect to them, as the assignment's identity.

Tags that follow the resource group

Finance reports on costCenter. Resource groups have it, resources don't, because tags are not inherited. The built-in Inherit a tag from the resource group if missing (modify) fixes that:

$ az group create -n rg-try-policy -l westeurope --tags costCenter=4711 -o none
$ az storage account create -n sttrypol01 -g rg-try-policy -o none
$ RG=$(az group show -n rg-try-policy --query id -o tsv)
$ az policy assignment create -n try-inherit-cc --display-name "Inherit costCenter from the RG" --policy ea3f2387-9b95-492a-a190-fcdc54f7b070 --scope $RG -p '{"tagName":{"value":"costCenter"}}' --mi-system-assigned -l westeurope --role Contributor --identity-scope $RG --query "{name:name, identity:identity.type}"
{
  "identity": "SystemAssigned",
  "name": "try-inherit-cc"
}
$ az role assignment list --scope $RG --query "[?principalType=='ServicePrincipal'].{role:roleDefinitionName, principal:principalId}" -o table
Role         Principal
-----------  ------------------------------------
Contributor  f412284d-db86-4944-9a1d-9530ab5ae838
  • --mi-system-assigned -l westeurope: the assignment gets a system-assigned identity, which lives in a region (-l).
  • --role Contributor --identity-scope $RG: the CLI also creates the role assignment for that identity. The portal does this for you. Without it the identity exists with no rights. The definition's roleDefinitionIds says which roles it needs. For tag operations the documentation uses Contributor; Tag Contributor is the narrower choice when it covers everything the definition does.

A new resource gets the tag at creation:

$ az storage account create -n sttrypol02 -g rg-try-policy -o none
$ az storage account show -n sttrypol02 -g rg-try-policy --query tags
{
  "costCenter": "4711"
}
$ az storage account show -n sttrypol01 -g rg-try-policy --query tags
{}

The old account is untouched. It is only reported:

$ az policy state trigger-scan -g rg-try-policy
$ az policy state list -g rg-try-policy --query "[].{resource:resourceId, state:complianceState}" -o table
Resource                                                                                                                                 State
---------------------------------------------------------------------------------------------------------------------------------------  ------------
/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-try-policy/providers/Microsoft.Storage/storageAccounts/sttrypol01  NonCompliant
/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-try-policy/providers/Microsoft.Storage/storageAccounts/sttrypol02  Compliant
$ az storage account show -n sttrypol01 -g rg-try-policy --query tags
{}

The remediation failures you will see in real life:

  • No role for the identity (assignment made with the CLI, --role forgotten): every deployment fails. The portal shows the identity and offers to grant the roles.
  • Remediating before evaluation: a remediation started minutes after the assignment finds nothing to do (totalDeployments: 0), because compliance has not been computed yet. Run trigger-scan first, or --resource-discovery-mode ReEvaluateCompliance.
  • The fix fails for the resource itself: a lock, another policy's deny, an invalid template. Each failed deployment has its own error in the remediation's details.

deployIfNotExists: the related resource

DINE is how platform teams make "every VM is backed up" or "every Key Vault sends logs to Log Analytics" true without asking every team. Its then.details names:

  • type (and optional existenceCondition): the related resource that must exist, e.g. Microsoft.RecoveryServices/backupprotecteditems for a VM;
  • roleDefinitionIds: what the identity needs (for backup: Backup Contributor and Virtual Machine Contributor);
  • deployment: the ARM template that creates the related resource.

The backup lessons later in this chapter assign the built-in Configure backup on virtual machines without a given tag to an existing recovery services vault in the same location (09ce66bc-1220-4153-8104-e3f51c936913), and you will watch a remediation protect VMs created before the policy. Its sibling Azure Backup should be enabled for Virtual Machines (AINE) only reports, with no identity needed.

Initiatives

Ten rules assigned one by one become ten assignments to keep in sync. An initiative (policy set definition) bundles them, with shared parameters, and is assigned once. Microsoft's Microsoft cloud security benchmark (assigned by Defender for Cloud) is an initiative with hundreds of members. Your own "storage baseline" can be two.

First, a custom definition: storage accounts must allow only TLS 1.2. The rule and its parameters are JSON files:

$ cd ~/oncall-lab/labs/azure4/try
$ cat rule.json
{
  "if": {
    "allOf": [
      {
        "field": "type",
        "equals": "Microsoft.Storage/storageAccounts"
      },
      {
        "field": "Microsoft.Storage/storageAccounts/minimumTlsVersion",
        "notEquals": "TLS1_2"
      }
    ]
  },
  "then": {
    "effect": "[parameters('effect')]"
  }
}
$ az policy definition create -n try-storage-tls12 --display-name "Storage accounts must use TLS 1.2" --mode Indexed --rules rule.json --params params.json --metadata category=Storage --query "{name:name, type:policyType, mode:mode, category:metadata.category}"
{
  "category": "Storage",
  "mode": "Indexed",
  "name": "try-storage-tls12",
  "type": "Custom"
}
  • field uses aliases: Microsoft.Storage/storageAccounts/minimumTlsVersion is the alias for properties.minimumTlsVersion. az provider show --namespace Microsoft.Storage --expand "resourceTypes/aliases" lists them. A condition on a property with no alias can't be written.
  • A custom definition lives at a subscription or a management group. Define it as high as the widest scope you will assign it at.

Then the initiative, with its members referenced by id, each given a policyDefinitionReferenceId, and the set's effect parameter passed down to both:

$ cat set.json
[
  {
    "policyDefinitionId": "/subscriptions/00000000-1111-2222-3333-444444444444/providers/Microsoft.Authorization/policyDefinitions/try-storage-tls12",
    "policyDefinitionReferenceId": "tls12",
    "parameters": {
      "effect": {
        "value": "[parameters('effect')]"
      }
    }
  },
  {
    "policyDefinitionId": "/providers/Microsoft.Authorization/policyDefinitions/404c3081-a854-4457-ae30-26a93ef643f9",
    "policyDefinitionReferenceId": "httpsOnly",
    "parameters": {
      "effect": {
        "value": "[parameters('effect')]"
      }
    }
  }
]
$ az policy set-definition create -n try-storage-baseline --display-name "Storage baseline" --definitions set.json --params '{"effect":{"type":"String","allowedValues":["Audit","Deny","Disabled"],"defaultValue":"Audit"}}' --query "{name:name, members:policyDefinitions[].policyDefinitionReferenceId}"
{
  "members": [
    "tls12",
    "httpsOnly"
  ],
  "name": "try-storage-baseline"
}
$ az policy assignment create -n try-baseline --display-name "Storage baseline (deny)" --policy-set-definition try-storage-baseline -g rg-try-policy -p '{"effect":{"value":"Deny"}}' -o none
$ az storage account create -n sttrypol04 -g rg-try-policy --min-tls-version TLS1_0 --query minimumTlsVersion
WARNING: TLS 1.0 and TLS 1.1 have been retired on 2026/02/03, will use TLS 1.2 instead.
"TLS1_2"

That one was not refused. Since azure-cli 2.83 the CLI rewrites TLS1_0 and TLS1_1 to TLS1_2 (TLS 1.0/1.1 were retired for Azure Storage on 3 Feb 2026) and only warns, so the request Policy sees is compliant. A template sends exactly what it says, which makes it the honest test of the rule:

$ az deployment group create -g rg-try-policy -n tls10 --template-file tls10.json -o none 2>&1 | head -1
ERROR: (InvalidTemplateDeployment) The template deployment failed because of policy violation. Please see details for more information.
$ az deployment group create -g rg-try-policy -n tls10 --template-file tls10.json -o none 2>&1 | grep -m1 RequestDisallowedByPolicy
Exception Details:	(RequestDisallowedByPolicy) Resource 'sttrypol03' was disallowed by policy. Policy identifiers: '[{"policyAssignment":{"name":"Storage baseline (deny)","id":"/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-try-policy/providers/Microsoft.Authorization/policyAssignments/try-baseline"},"policyDefinition":{"name":"Storage accounts must use TLS 1.2","id":"/subscriptions/00000000-1111-2222-3333-444444444444/providers/Microsoft.Authorization/policyDefinitions/try-storage-tls12","version":"1.0.0"},"policySetDefinition":{"name":"try-storage-baseline","id":"/subscriptions/00000000-1111-2222-3333-444444444444/providers/Microsoft.Authorization/policySetDefinitions/try-storage-baseline","version":"1.0.0"}}]'.

A deployment wraps the refusal: InvalidTemplateDeployment outside, the same RequestDisallowedByPolicy inside. The denial names three things now: the assignment, the member definition that denied, and the set it came from (policySetDefinition). Compliance for an initiative is reported per member: one assignment, two rows per resource.

Policy as code

Assignments clicked in the portal drift and nobody reviews them. Mature teams keep definitions, initiatives and assignments in a repository (Bicep/ARM, Terraform azurerm_policy_*, or Enterprise Policy as Code), review changes in pull requests, roll them out as Audit first and switch to Deny with a date announced. Policy changes are production changes: a wrong deny at a management group stops every pipeline below it.

On the exam (AZ-104 1.3.1): which effect does what (append/modify vs deny vs audit vs AINE vs DINE), why modify and DINE need a managed identity and a role, remediation tasks for existing resources, initiatives vs definitions, custom definitions and aliases, and where a definition must live to be assigned at a management group.

In an interview: "You assigned a policy that adds a missing tag. New resources have it, the old ones don't. Why?" - "Policy changes resources only when they are created or updated. Existing ones are just evaluated and marked non-compliant. To fix them you run a remediation task, which applies the modify effect as the assignment's managed identity. That identity needs a role on the scope, for tags Contributor or Tag Contributor, or every remediation deployment fails."

What you can now do

  • Assign a modify or DINE policy with a managed identity and the role it needs.
  • Fix existing resources with a remediation task, and say why one would report 0 or failed deployments.
  • Write a custom definition with aliases and parameters, bundle definitions into an initiative, and assign it once.

Why it helps

modify and deployIfNotExists are how large estates stay tagged, logged and backed up without tickets to every team. They are also where governance projects stall: assignments made without the identity's role, remediations started before the scan, existing resources nobody remediated.

Initiatives and custom definitions turn a pile of rules into a baseline you can version, review and roll out. That is the difference between governance that scales and a list of exceptions.

Commands in this lesson

az cd cat

FAQ

Why does my remediation task show 0 deployments?

It ran before the assignment was evaluated, so it found no non-compliant resources. Run az policy state trigger-scan first, or create the remediation with --resource-discovery-mode ReEvaluateCompliance. The remediation works from the last evaluation, so an assignment made minutes ago has nothing to remediate yet.

Why do all remediation deployments fail?

Most often the assignment's managed identity has no role on the scope. The portal grants the roles from the definition's roleDefinitionIds automatically; with the CLI you add --role and --identity-scope, or create the role assignments yourself. A lock or another deny policy on the target can also fail them.

modify or append?

modify can add, replace or remove tags and some properties marked as modifiable, and supports remediation tasks for existing resources. append is the older effect that only adds fields to the request and has no remediation. For tags, use modify.

Where should a custom policy definition live?

At the highest scope you will assign it: a definition saved in a subscription can only be assigned there and below, one saved in a management group can be assigned anywhere under that group. Keep definitions, initiatives and assignments in a repository and deploy them like code.

What does an initiative add over separate assignments?

One assignment for many rules, shared parameters (one effect parameter for the whole baseline), and compliance reported per member under one name. Changing the baseline means changing one initiative instead of chasing ten assignments. Assignments of the same initiative can still use different effects per scope.

In an interview Mid

You assigned a policy that adds a missing tag. New resources have it, old ones don't. Why, and what do you do?

Policy changes resources only when they are created or updated. Existing ones are just evaluated and marked non-compliant. To fix them I run a remediation task (az policy remediation create) after a compliance scan, and it applies the modify effect as the assignment's managed identity. That identity needs a role on the scope, Contributor or Tag Contributor for tags, or every remediation deployment fails.

Also asked: What does deployIfNotExists do, and what does it need? · How would you make sure every VM in a subscription is backed up? · What is a policy alias?

Practise this lesson in the terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.