Skip to content

AuditIfNotExists and DeployIfNotExists describe the related resource's compliant state

Context and Problem Statement

auditIfNotExists and deployIfNotExists check a related resource: for a virtual machine, its antimalware extension; for a database, its encryption settings. details.type and the optional name, resourceGroupName and existenceScope say which related resources to look at. details.existenceCondition says what a related resource must look like. Learn: "If any matching related resource evaluates to true, the effect is satisfied". deployIfNotExists adds roles and a template deployment (ADR-0031). 74 of the 149 custom definitions in the Azure Landing Zones Library use deployIfNotExists. How are these effects written, and how is the existence condition compiled?

Decision Drivers

  • The existence condition is a compliant condition: true means the related resource is fine. Bylaw's Require already describes a compliant state, so authors should write it the same way.
  • Field names in the existence condition refer to the related resource, so they resolve against its type.
  • The same shape as Modify (ADR-0024): an effect with details has its own keyword, in place of Effect.

Considered Options

  • A keyword per effect, whose block holds Require blocks for the related resource
  • A Related keyword inside Resource, plus a separate effect keyword
  • The existence condition as raw JSON

Decision Outcome

Chosen option: "A keyword per effect, whose block holds Require blocks for the related resource".

Resource 'Microsoft.Compute/virtualMachines' {
    When {
        Field storageProfile.osDisk.osType | Must -Be 'Windows'
    }
}

AuditIfNotExists 'Microsoft.Compute/virtualMachines/extensions' {
    Require 'The antimalware extension is installed' {
        Field publisher | Must -Be 'Microsoft.Azure.Security'
        Field 'Microsoft.Compute/virtualMachines/extensions/type' | Must -Be 'IaaSAntimalware'
    }
}

The resource being evaluated

  • Resource and When select the resources whose related resources are checked, as for any policy. -Type names the related resource, so it doesn't replace Resource. When every resource of the type is checked, the Resource block can be left out: Resource 'Microsoft.KeyVault/vaults' (ADR-0002).
  • Require blocks in Resource are still allowed and still inverted: a resource that meets them is compliant whatever its related resources are. A resource is non-compliant only if it matches When, breaks a Require, and has no related resource that meets the existence condition. Most of these policies have no Require in Resource, so for these two effects BYL3041 ("no Require block") isn't reported.
  • -Type is required and emitted as details.type. It's checked against the alias snapshot like a Resource type. Like Resource's type, it's the first positional parameter, so the name can be left out: AuditIfNotExists 'Microsoft.Compute/virtualMachines/extensions' { ... }. A block with no type is still BYL1038.
  • -Name, a literal or an Arm or Param expression, emits details.name. Learn: when -Type is the same as the Resource type, name is required and must be [field('name')], or [field('fullName')] for a child resource. Anything else is a compile error. For AuditIfNotExists, Bylaw also warns that Learn suggests the audit effect instead.
  • -ResourceGroupName emits details.resourceGroupName.
  • -ExistenceScope ResourceGroup or Subscription emits details.existenceScope, omitted when not set (Azure's default is ResourceGroup). Learn: resourceGroupName and existenceScope don't apply when the related type is a child of the Resource type, so setting either then is a warning.
  • -EvaluationDelay takes AfterProvisioning, AfterProvisioningSuccess, AfterProvisioningFailure, or an ISO 8601 duration from 0 to 360 minutes, such as PT30M. Anything else is a compile error. It's omitted when not set (Azure's default is PT10M). Since 2026-11-28 it also takes a Param or an Arm expression, emitted as written, as the Azure Landing Zones Library's Deploy-Private-DNS-Generic uses (ADR-0042). A parameter must be a String whose default and allowed values are valid delays (BYL3095).

The existence condition

  • The block holds Require blocks, which all must hold, as in Resource. Inside them, Field, Tag, Each, AnyOf, AllOf, Count (ADR-0032) and value conditions work as they do in a Resource. When isn't allowed: there is no applicability for a related resource.
  • Field names resolve against -Type, the related resource. Reserved fields such as name and type are the related resource's.
  • The block is optional. Without it there is no existenceCondition, and any related resource of the type satisfies the effect.
  • The existence condition isn't inverted. The Require blocks are emitted as the compliant condition they describe: Must -Be 'x' emits equals 'x'. This is the one place where Require compiles to the compliant form rather than the violation (ADR-0002).
  • Missing fields keep their meaning (ADR-0004). In Require, a missing field doesn't satisfy the requirement, so a related resource without the field doesn't count. The compiler adds exists: true where the operator would otherwise be true on a missing field. -IfPresent makes a missing field satisfy it, adding exists: false where needed. Inside Each, element fields stay lenient, and -Strict makes them strict. An Each emits count(where: violation) equals 0, the compliant form of "every element".

The effect

  • AuditIfNotExists emits the effect AuditIfNotExists. -Effect (Param Effect) may allow AuditIfNotExists and Disabled.
  • DeployIfNotExists emits the effect DeployIfNotExists, and also takes -Role (ADR-0022) and a deployment (ADR-0031). -Effect (Param Effect) may allow DeployIfNotExists, AuditIfNotExists and Disabled. Two ALZ policies allow all three. The keyword sets the details and -Effect sets then.effect: under AuditIfNotExists, Azure ignores the roles and deployment and only reports.
  • A literal effect gets a generated EPAC parameter: AuditIfNotExists and Disabled, or DeployIfNotExists and Disabled. These are the values built-in and ALZ policies use: 70 of the 74 ALZ DeployIfNotExists policies allow exactly those two.

Diagnostics

  • Front end: BYL1038 (no -Type, or one that is not a resource type), BYL1039 (a -Name or -ResourceGroupName that is not a name, Param or Arm), BYL1040 (an invalid -ExistenceScope), BYL1041 (an invalid -EvaluationDelay). As for the other effect keywords, BYL1030 is a literal -Effect and BYL1026 a second effect keyword. When, or a Field outside Require, in the block is BYL1002.
  • Validation: BYL2003 (a related type that is not in the alias snapshot), BYL3072 (an effect parameter that allows values the keyword doesn't). Warnings: BYL3090 (-Type is the Resource type, and -Name is not [field('name')] or [field('fullName')]. Learn requires the resource's own name, but Azure accepts other names and the Azure Landing Zones library uses them, so since 2026-11-28 this is a warning rather than an error), BYL3091 (AuditIfNotExists on the resource itself, where Learn suggests Audit), BYL3092 (-ResourceGroupName or -ExistenceScope on a child type).
  • Field names in the existence condition resolve against -Type, so reserved names such as type and name are the related resource's. A property that shares a reserved name, such as an extension's type, needs its full alias: Microsoft.Compute/virtualMachines/extensions/type.

Consequences

  • Good, because the existence condition reads like any other requirement, and its field names are checked against the right type.
  • Good, because missing fields behave the same way inside and outside the existence condition.
  • Bad, because Require compiles to the compliant form here and to the violation elsewhere. Authors don't see the difference, because both describe the compliant state. The README explains it.

Confirmation

  • Unit tests cover each option and error, field resolution against -Type, and the existence condition for each operator, with and without -IfPresent, and inside Each.
  • Golden tests cover an AuditIfNotExists and a DeployIfNotExists policy in EPAC and plain JSON.
  • The sandbox script measures AuditIfNotExists policies whose existence condition tests a property the related resource doesn't have. Measured on 2026-11-26 (results): a missing field on a related resource behaves as it does on the evaluated resource. notEquals, notIn and notLike hold on it, so the guard is needed; the guarded form Bylaw emits doesn't hold, and the -IfPresent form does.

Pros and Cons of the Options

A keyword per effect with Require blocks

  • Good, because the existence condition is written like the rest of the language.
  • Bad, because Require has two compilation modes.
  • Good, because the related resource is visually separate from the effect.
  • Bad, because the related resource only makes sense with these two effects, so splitting them adds rules about which goes with which.

Raw JSON

  • Good, because it is simple to implement.
  • Bad, because nothing is validated, and the condition is the most error-prone part of these policies.

More Information