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
Requirealready 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 ofEffect.
Considered Options¶
- A keyword per effect, whose block holds
Requireblocks for the related resource - A
Relatedkeyword insideResource, 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¶
ResourceandWhenselect the resources whose related resources are checked, as for any policy.-Typenames the related resource, so it doesn't replaceResource. When every resource of the type is checked, theResourceblock can be left out:Resource 'Microsoft.KeyVault/vaults'(ADR-0002).Requireblocks inResourceare 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 matchesWhen, breaks aRequire, and has no related resource that meets the existence condition. Most of these policies have noRequireinResource, so for these two effects BYL3041 ("no Require block") isn't reported.
The related resource¶
-Typeis required and emitted asdetails.type. It's checked against the alias snapshot like aResourcetype. LikeResource'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 anArmorParamexpression, emitsdetails.name. Learn: when-Typeis the same as theResourcetype,nameis required and must be[field('name')], or[field('fullName')]for a child resource. Anything else is a compile error. ForAuditIfNotExists, Bylaw also warns that Learn suggests theauditeffect instead.-ResourceGroupNameemitsdetails.resourceGroupName.-ExistenceScope ResourceGrouporSubscriptionemitsdetails.existenceScope, omitted when not set (Azure's default isResourceGroup). Learn:resourceGroupNameandexistenceScopedon't apply when the related type is a child of theResourcetype, so setting either then is a warning.-EvaluationDelaytakesAfterProvisioning,AfterProvisioningSuccess,AfterProvisioningFailure, or an ISO 8601 duration from 0 to 360 minutes, such asPT30M. Anything else is a compile error. It's omitted when not set (Azure's default isPT10M). Since 2026-11-28 it also takes aParamor anArmexpression, emitted as written, as the Azure Landing Zones Library'sDeploy-Private-DNS-Genericuses (ADR-0042). A parameter must be aStringwhose default and allowed values are valid delays (BYL3095).
The existence condition¶
- The block holds
Requireblocks, which all must hold, as inResource. Inside them,Field,Tag,Each,AnyOf,AllOf,Count(ADR-0032) and value conditions work as they do in aResource.Whenisn't allowed: there is no applicability for a related resource. - Field names resolve against
-Type, the related resource. Reserved fields such asnameandtypeare 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
Requireblocks are emitted as the compliant condition they describe:Must -Be 'x'emitsequals 'x'. This is the one place whereRequirecompiles 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 addsexists: truewhere the operator would otherwise be true on a missing field.-IfPresentmakes a missing field satisfy it, addingexists: falsewhere needed. InsideEach, element fields stay lenient, and-Strictmakes them strict. AnEachemitscount(where: violation) equals 0, the compliant form of "every element".
The effect¶
AuditIfNotExistsemits the effectAuditIfNotExists.-Effect (Param Effect)may allowAuditIfNotExistsandDisabled.DeployIfNotExistsemits the effectDeployIfNotExists, and also takes-Role(ADR-0022) and a deployment (ADR-0031).-Effect (Param Effect)may allowDeployIfNotExists,AuditIfNotExistsandDisabled. Two ALZ policies allow all three. The keyword sets the details and-Effectsetsthen.effect: underAuditIfNotExists, Azure ignores the roles and deployment and only reports.- A literal effect gets a generated EPAC parameter:
AuditIfNotExistsandDisabled, orDeployIfNotExistsandDisabled. 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-Nameor-ResourceGroupNamethat is not a name,ParamorArm), BYL1040 (an invalid-ExistenceScope), BYL1041 (an invalid-EvaluationDelay). As for the other effect keywords, BYL1030 is a literal-Effectand BYL1026 a second effect keyword.When, or aFieldoutsideRequire, 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 (
-Typeis theResourcetype, and-Nameis 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 (AuditIfNotExistson the resource itself, where Learn suggestsAudit), BYL3092 (-ResourceGroupNameor-ExistenceScopeon a child type). - Field names in the existence condition resolve against
-Type, so reserved names such astypeandnameare the related resource's. A property that shares a reserved name, such as an extension'stype, 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
Requirecompiles 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 insideEach. - Golden tests cover an
AuditIfNotExistsand aDeployIfNotExistspolicy in EPAC and plain JSON. - The sandbox script measures
AuditIfNotExistspolicies 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,notInandnotLikehold on it, so the guard is needed; the guarded form Bylaw emits doesn't hold, and the-IfPresentform 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
Requirehas two compilation modes.
A separate Related block¶
- 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.