|
| 1 | +--- |
| 2 | +title: Azure Policy definitions auditIfNotExists effect |
| 3 | +description: Azure Policy definitions auditIfNotExists effect determines how compliance is managed and reported. |
| 4 | +ms.date: 04/08/2024 |
| 5 | +ms.topic: conceptual |
| 6 | +--- |
| 7 | + |
| 8 | +# Azure Policy definitions auditIfNotExists effect |
| 9 | + |
| 10 | +The `auditIfNotExists` effect enables auditing of resources _related_ to the resource that matches the `if` condition, but don't have the properties specified in the `details` of the `then` condition. |
| 11 | + |
| 12 | +## AuditIfNotExists evaluation |
| 13 | + |
| 14 | +`auditIfNotExists` runs after a Resource Provider processed a create or update resource request and returned a success status code. The audit occurs if there are no related resources or if the resources defined by `ExistenceCondition` don't evaluate to true. For new and updated resources, Azure Policy adds a `Microsoft.Authorization/policies/audit/action` operation to the activity log and marks the resource as non-compliant. When triggered, the resource that satisfied the `if` condition is the resource that is marked as non-compliant. |
| 15 | + |
| 16 | +## AuditIfNotExists properties |
| 17 | + |
| 18 | +The `details` property of the AuditIfNotExists effects has all the subproperties that define the related resources to match. |
| 19 | + |
| 20 | +- `type` (required) |
| 21 | + - Specifies the type of the related resource to match. |
| 22 | + - If `type` is a resource type underneath the `if` condition resource, the policy queries for resources of this `type` within the scope of the evaluated resource. Otherwise, policy queries within the same resource group or subscription as the evaluated resource depending on the `existenceScope`. |
| 23 | +- `name` (optional) |
| 24 | + - Specifies the exact name of the resource to match and causes the policy to fetch one specific resource instead of all resources of the specified type. |
| 25 | + - When the condition values for `if.field.type` and `then.details.type` match, then `name` becomes _required_ and must be `[field('name')]`, or `[field('fullName')]` for a child resource. However, an [audit](./effect-audit.md) effect should be considered instead. |
| 26 | + |
| 27 | +> [!NOTE] |
| 28 | +> |
| 29 | +> `type` and `name` segments can be combined to generically retrieve nested resources. |
| 30 | +> |
| 31 | +> To retrieve a specific resource, you can use `"type": "Microsoft.ExampleProvider/exampleParentType/exampleNestedType"` and `"name": "parentResourceName/nestedResourceName"`. |
| 32 | +> |
| 33 | +> To retrieve a collection of nested resources, a wildcard character `?` can be provided in place of the last name segment. For example, `"type": "Microsoft.ExampleProvider/exampleParentType/exampleNestedType"` and `"name": "parentResourceName/?"`. This can be combined with field functions to access resources related to the evaluated resource, such as `"name": "[concat(field('name'), '/?')]"`." |
| 34 | +
|
| 35 | +- `resourceGroupName` (optional) |
| 36 | + - Allows the matching of the related resource to come from a different resource group. |
| 37 | + - Doesn't apply if `type` is a resource that would be underneath the `if` condition resource. |
| 38 | + - Default is the `if` condition resource's resource group. |
| 39 | +- `existenceScope` (optional) |
| 40 | + - Allowed values are _Subscription_ and _ResourceGroup_. |
| 41 | + - Sets the scope of where to fetch the related resource to match from. |
| 42 | + - Doesn't apply if `type` is a resource that would be underneath the `if` condition resource. |
| 43 | + - For _ResourceGroup_, would limit to the resource group in `resourceGroupName` if specified. If `resourceGroupName` isn't specified, would limit to the `if` condition resource's resource group, which is the default behavior. |
| 44 | + - For _Subscription_, queries the entire subscription for the related resource. Assignment scope should be set at subscription or higher for proper evaluation. |
| 45 | + - Default is _ResourceGroup_. |
| 46 | +- `evaluationDelay` (optional) |
| 47 | + - Specifies when the existence of the related resources should be evaluated. The delay is only |
| 48 | + used for evaluations that are a result of a create or update resource request. |
| 49 | + - Allowed values are `AfterProvisioning`, `AfterProvisioningSuccess`, `AfterProvisioningFailure`, |
| 50 | + or an ISO 8601 duration between 0 and 360 minutes. |
| 51 | + - The _AfterProvisioning_ values inspect the provisioning result of the resource that was |
| 52 | + evaluated in the policy rule's `if` condition. `AfterProvisioning` runs after provisioning is |
| 53 | + complete, regardless of outcome. Provisioning that takes more than six hours, is treated as a |
| 54 | + failure when determining _AfterProvisioning_ evaluation delays. |
| 55 | + - Default is `PT10M` (10 minutes). |
| 56 | + - Specifying a long evaluation delay might cause the recorded compliance state of the resource to |
| 57 | + not update until the next |
| 58 | + [evaluation trigger](../how-to/get-compliance-data.md#evaluation-triggers). |
| 59 | +- `existenceCondition` (optional) |
| 60 | + - If not specified, any related resource of `type` satisfies the effect and doesn't trigger the |
| 61 | + audit. |
| 62 | + - Uses the same language as the policy rule for the `if` condition, but is evaluated against |
| 63 | + each related resource individually. |
| 64 | + - If any matching related resource evaluates to true, the effect is satisfied and doesn't trigger |
| 65 | + the audit. |
| 66 | + - Can use [field()] to check equivalence with values in the `if` condition. |
| 67 | + - For example, could be used to validate that the parent resource (in the `if` condition) is in |
| 68 | + the same resource location as the matching related resource. |
| 69 | + |
| 70 | +## AuditIfNotExists example |
| 71 | + |
| 72 | +Example: Evaluates Virtual Machines to determine whether the Antimalware extension exists then audits when missing. |
| 73 | + |
| 74 | +```json |
| 75 | +{ |
| 76 | + "if": { |
| 77 | + "field": "type", |
| 78 | + "equals": "Microsoft.Compute/virtualMachines" |
| 79 | + }, |
| 80 | + "then": { |
| 81 | + "effect": "auditIfNotExists", |
| 82 | + "details": { |
| 83 | + "type": "Microsoft.Compute/virtualMachines/extensions", |
| 84 | + "existenceCondition": { |
| 85 | + "allOf": [ |
| 86 | + { |
| 87 | + "field": "Microsoft.Compute/virtualMachines/extensions/publisher", |
| 88 | + "equals": "Microsoft.Azure.Security" |
| 89 | + }, |
| 90 | + { |
| 91 | + "field": "Microsoft.Compute/virtualMachines/extensions/type", |
| 92 | + "equals": "IaaSAntimalware" |
| 93 | + } |
| 94 | + ] |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +## Next steps |
| 102 | + |
| 103 | +- Review examples at [Azure Policy samples](../samples/index.md). |
| 104 | +- Review the [Azure Policy definition structure](definition-structure-basics.md). |
| 105 | +- Understand how to [programmatically create policies](../how-to/programmatically-create.md). |
| 106 | +- Learn how to [get compliance data](../how-to/get-compliance-data.md). |
| 107 | +- Learn how to [remediate non-compliant resources](../how-to/remediate-resources.md). |
| 108 | +- Review [Azure management groups](../../management-groups/overview.md). |
0 commit comments