|
| 1 | +--- |
| 2 | +title: Details of the policy definition structure basics |
| 3 | +description: Describes how policy definition basics are used to establish conventions for Azure resources in your organization. |
| 4 | +ms.date: 04/01/2024 |
| 5 | +ms.topic: conceptual |
| 6 | +--- |
| 7 | + |
| 8 | +# Azure Policy definition structure basics |
| 9 | + |
| 10 | +Azure Policy definitions describe resource compliance [conditions](./definition-structure-policy-rule.md#conditions) and the effect to take if a condition is met. A condition compares a resource property [field](./definition-structure-policy-rule.md#fields) or a [value](./definition-structure-policy-rule.md#value) to a required value. Resource property fields are accessed by using [aliases](./definition-structure-alias.md). When a resource property field is an array, a special [array alias](./definition-structure-alias.md#understanding-the-array-alias) can be used to select values from all array members and apply a condition to each one. Learn more about [conditions](./definition-structure-policy-rule.md#conditions). |
| 11 | + |
| 12 | +By using policy assignments, you can control costs and manage your resources. For example, you can specify that only certain types of virtual machines are allowed. Or, you can require that resources have a particular tag. Assignments at a scope apply to all resources at that scope and below. If a policy assignment is applied to a resource group, it's applicable to all the resources in that resource group. |
| 13 | + |
| 14 | +You use JSON to create a policy definition that contains elements for: |
| 15 | + |
| 16 | +- `displayName` |
| 17 | +- `description` |
| 18 | +- `mode` |
| 19 | +- `metadata` |
| 20 | +- `parameters` |
| 21 | +- `policyRule` |
| 22 | + - logical evaluations |
| 23 | + - `effect` |
| 24 | + |
| 25 | +For example, the following JSON shows a policy that limits where resources are deployed: |
| 26 | + |
| 27 | +```json |
| 28 | +{ |
| 29 | + "properties": { |
| 30 | + "displayName": "Allowed locations", |
| 31 | + "description": "This policy enables you to restrict the locations your organization can specify when deploying resources.", |
| 32 | + "mode": "Indexed", |
| 33 | + "metadata": { |
| 34 | + "version": "1.0.0", |
| 35 | + "category": "Locations" |
| 36 | + }, |
| 37 | + "parameters": { |
| 38 | + "allowedLocations": { |
| 39 | + "type": "array", |
| 40 | + "metadata": { |
| 41 | + "description": "The list of locations that can be specified when deploying resources", |
| 42 | + "strongType": "location", |
| 43 | + "displayName": "Allowed locations" |
| 44 | + }, |
| 45 | + "defaultValue": [ |
| 46 | + "westus2" |
| 47 | + ] |
| 48 | + } |
| 49 | + }, |
| 50 | + "policyRule": { |
| 51 | + "if": { |
| 52 | + "not": { |
| 53 | + "field": "location", |
| 54 | + "in": "[parameters('allowedLocations')]" |
| 55 | + } |
| 56 | + }, |
| 57 | + "then": { |
| 58 | + "effect": "deny" |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +For more information, go to the [policy definition schema](https://schema.management.azure.com/schemas/2020-10-01/policyDefinition.json). Azure Policy built-ins and patterns are at [Azure Policy samples](../samples/index.md). |
| 66 | + |
| 67 | +## Display name and description |
| 68 | + |
| 69 | +You use `displayName` and `description` to identify the policy definition and provide context for when the definition is used. The `displayName` has a maximum length of _128_ characters and `description` a maximum length of _512_ characters. |
| 70 | + |
| 71 | +> [!NOTE] |
| 72 | +> During the creation or updating of a policy definition, `id`, `type`, and `name` are defined |
| 73 | +> by properties external to the JSON and aren't necessary in the JSON file. Fetching the policy |
| 74 | +> definition via SDK returns the `id`, `type`, and `name` properties as part of the JSON, but |
| 75 | +> each are read-only information related to the policy definition. |
| 76 | +
|
| 77 | +## Policy type |
| 78 | + |
| 79 | +While the `policyType` property can't be set, there are three values returned by SDK and visible in the portal: |
| 80 | + |
| 81 | +- `Builtin`: Microsoft provides and maintains these policy definitions. |
| 82 | +- `Custom`: All policy definitions created by customers have this value. |
| 83 | +- `Static`: Indicates a [Regulatory Compliance](./regulatory-compliance.md) policy definition with |
| 84 | + Microsoft **Ownership**. The compliance results for these policy definitions are the results of |
| 85 | + non-Microsoft audits of Microsoft infrastructure. In the Azure portal, this value is sometimes |
| 86 | + displayed as **Microsoft managed**. For more information, see |
| 87 | + [Shared responsibility in the cloud](../../../security/fundamentals/shared-responsibility.md). |
| 88 | + |
| 89 | +## Mode |
| 90 | + |
| 91 | +The `mode` is configured depending on if the policy is targeting an Azure Resource Manager property or a Resource Provider property. |
| 92 | + |
| 93 | +### Resource Manager modes |
| 94 | + |
| 95 | +The `mode` determines which resource types are evaluated for a policy definition. The supported modes are: |
| 96 | + |
| 97 | +- `all`: evaluate resource groups, subscriptions, and all resource types |
| 98 | +- `indexed`: only evaluate resource types that support tags and location |
| 99 | + |
| 100 | +For example, resource `Microsoft.Network/routeTables` supports tags and location and is evaluated in both modes. However, resource `Microsoft.Network/routeTables/routes` can't be tagged and isn't evaluated in `Indexed` mode. |
| 101 | + |
| 102 | +We recommend that you set `mode` to `all` in most cases. All policy definitions created through the portal use the `all` mode. If you use PowerShell or Azure CLI, you can specify the `mode` parameter manually. If the policy definition doesn't include a `mode` value, it defaults to `all` in Azure PowerShell and to `null` in Azure CLI. A `null` mode is the same as using `indexed` to support backward compatibility. |
| 103 | + |
| 104 | +`indexed` should be used when creating policies that enforce tags or locations. While not required, it prevents resources that don't support tags and locations from showing up as non-compliant in the compliance results. The exception is resource groups and subscriptions. Policy definitions that enforce location or tags on a resource group or subscription should set `mode` to `all` and specifically target the `Microsoft.Resources/subscriptions/resourceGroups` or `Microsoft.Resources/subscriptions` type. For an example, see [Pattern: Tags - Sample #1](../samples/pattern-tags.md). For a list of resources that support tags, see [Tag support for Azure resources](../../../azure-resource-manager/management/tag-support.md). |
| 105 | + |
| 106 | +### Resource Provider modes |
| 107 | + |
| 108 | +The following Resource Provider modes are fully supported: |
| 109 | + |
| 110 | +- `Microsoft.Kubernetes.Data` for managing Kubernetes clusters and components such as pods, containers, and ingresses. Supported for Azure Kubernetes Service clusters and [Azure Arc-enabled Kubernetes clusters](../../../aks/intro-kubernetes.md). Definitions using this Resource Provider mode use the effects _audit_, _deny_, and _disabled_. |
| 111 | +- `Microsoft.KeyVault.Data` for managing vaults and certificates in [Azure Key Vault](../../../key-vault/general/overview.md). For more information on these policy definitions, see [Integrate Azure Key Vault with Azure Policy](../../../key-vault/general/azure-policy.md). |
| 112 | +- `Microsoft.Network.Data` for managing [Azure Virtual Network Manager](../../../virtual-network-manager/overview.md) custom membership policies using Azure Policy. |
| 113 | + |
| 114 | +The following Resource Provider modes are currently supported as a [preview](https://azure.microsoft.com/support/legal/preview-supplemental-terms/): |
| 115 | + |
| 116 | +- `Microsoft.ManagedHSM.Data` for managing [Managed Hardware Security Module (HSM)](../../../key-vault/managed-hsm/azure-policy.md) keys using Azure Policy. |
| 117 | +- `Microsoft.DataFactory.Data` for using Azure Policy to deny [Azure Data Factory](../../../data-factory/introduction.md) outbound traffic domain names not specified in an allowlist. This Resource Provider mode is enforcement only and doesn't report compliance in public preview. |
| 118 | +- `Microsoft.MachineLearningServices.v2.Data` for managing [Azure Machine Learning](../../../machine-learning/overview-what-is-azure-machine-learning.md) model deployments. This Resource Provider mode reports compliance for newly created and updated components. During public preview, compliance records remain for 24 hours. Model deployments that exist before these policy definitions are assigned don't report compliance. |
| 119 | + |
| 120 | +> [!NOTE] |
| 121 | +>Unless explicitly stated, Resource Provider modes only support built-in policy definitions, and exemptions are not supported at the component-level. |
| 122 | +
|
| 123 | +## Metadata |
| 124 | + |
| 125 | +The optional `metadata` property stores information about the policy definition. Customers can define any properties and values useful to their organization in `metadata`. However, there are some _common_ properties used by Azure Policy and in built-ins. Each `metadata` property has a limit of 1,024 characters. |
| 126 | + |
| 127 | +### Common metadata properties |
| 128 | + |
| 129 | +- `version` (string): Tracks details about the version of the contents of a policy definition. |
| 130 | +- `category` (string): Determines under which category in the Azure portal the policy definition is displayed. |
| 131 | +- `preview` (boolean): True or false flag for if the policy definition is _preview_. |
| 132 | +- `deprecated` (boolean): True or false flag for if the policy definition is marked as _deprecated_. |
| 133 | +- `portalReview` (string): Determines whether parameters should be reviewed in the portal, regardless of the required input. |
| 134 | + |
| 135 | +> [!NOTE] |
| 136 | +> The Azure Policy service uses `version`, `preview`, and `deprecated` properties to convey level of |
| 137 | +> change to a built-in policy definition or initiative and state. The format of `version` is: |
| 138 | +> `{Major}.{Minor}.{Patch}`. Specific states, such as _deprecated_ or _preview_, are appended to the |
| 139 | +> `version` property or in another property as a **boolean**. For more information about the way |
| 140 | +> Azure Policy versions built-ins, see |
| 141 | +> [Built-in versioning](https://github.com/Azure/azure-policy/blob/master/built-in-policies/README.md). |
| 142 | +> To learn more about what it means for a policy to be _deprecated_ or in _preview_, see [Preview and deprecated policies](https://github.com/Azure/azure-policy/blob/master/built-in-policies/README.md#preview-and-deprecated-policies). |
| 143 | +
|
| 144 | +## Definition location |
| 145 | + |
| 146 | +While creating an initiative or policy, it's necessary to specify the definition location. The definition location must be a management group or a subscription. This location determines the scope to which the initiative or policy can be assigned. Resources must be direct members of or children within the hierarchy of the definition location to target for assignment. |
| 147 | + |
| 148 | +If the definition location is a: |
| 149 | + |
| 150 | +- **Subscription** - Only resources within that subscription can be assigned the policy definition. |
| 151 | +- **Management group** - Only resources within child management groups and child subscriptions can be assigned the policy definition. If you plan to apply the policy definition to several subscriptions, the location must be a management group that contains each subscription. |
| 152 | + |
| 153 | +For more information, see [Understand scope in Azure Policy](./scope.md#definition-location). |
| 154 | + |
| 155 | +## Next steps |
| 156 | + |
| 157 | +- For more information about policy definition structure, go to [parameters](./definition-structure-parameters.md), [policy rule](./definition-structure-policy-rule.md), and [alias](./definition-structure-alias.md). |
| 158 | +- For initiatives, go to [initiative definition structure](./initiative-definition-structure.md). |
| 159 | +- Review examples at [Azure Policy samples](../samples/index.md). |
| 160 | +- Review [Understanding policy effects](effects.md). |
| 161 | +- Understand how to [programmatically create policies](../how-to/programmatically-create.md). |
| 162 | +- Learn how to [get compliance data](../how-to/get-compliance-data.md). |
| 163 | +- Learn how to [remediate non-compliant resources](../how-to/remediate-resources.md). |
| 164 | +- Review what a management group is with [Organize your resources with Azure management groups](../../management-groups/overview.md). |
0 commit comments