|
| 1 | +--- |
| 2 | +title: Azure Policy and Cosmos DB |
| 3 | +description: This article describes how to use Azure Policy to implement governance and controls for Cosmos DB resources. |
| 4 | +author: plzm |
| 5 | +ms.author: paelaz |
| 6 | +ms.service: cosmos-db |
| 7 | +ms.topic: conceptual |
| 8 | +ms.date: 05/20/2020 |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +# Azure Policy Overview |
| 13 | + |
| 14 | +[Azure Policy](../governance/policy/overview.md) helps to enforce organizational governance standards, assess resource compliance, and implement automatic remediation. Common use cases include security, cost management, and configuration consistency. |
| 15 | + |
| 16 | +Azure Policy provides built-in policy definitions. Custom policy definitions can be created for scenarios not addressed by built-in policy definitions. Consult [Azure Policy documentation](../governance/policy/overview.md) for specifics. |
| 17 | + |
| 18 | +## Assigning a Built-in Policy Definition |
| 19 | + |
| 20 | +Policy _assignments_ are created from built-in policy _definitions_. Assignments are scoped to an Azure management group, an Azure subscription, or a resource group and will apply to resources within the scope. Optionally, specific resources can be excluded from the scope. |
| 21 | + |
| 22 | +Policy assignments can be created with the [Azure Portal](../governance/policy/assign-policy-portal.md), [Azure Powershell](../governance/policy/assign-policy-portal.md), [Azure CLI](../governance/policy/assign-policy-portal.md), or [ARM template](../governance/policy/assign-policy-portal.md). |
| 23 | + |
| 24 | +To create a policy assignment from a built-in policy definition for Cosmos DB, follow the steps to [create a policy assignment with the Azure Portal](../governance/policy/assign-policy-portal.md). |
| 25 | + |
| 26 | +At the step to select a policy definition, enter `Cosmos DB` in the Search field. This will filter the list of available built-in policy definitions. Select one of the available built-in policy definitions, then the **Select** button to continue with policy assignment creation. |
| 27 | + |
| 28 | +> [!TIP] |
| 29 | +> The built-in policy definition names shown on **Available Definitions** can also be used with Azure Powershell, Azure CLI, or ARM templates to create policy assignments. |
| 30 | +
|
| 31 | +:::image type="content" source="./media/policy/available-definitions.png" alt-text="Search for Cosmos DB built-in policy definitions"::: |
| 32 | + |
| 33 | +## Creating a Custom Policy Definition |
| 34 | + |
| 35 | +For specific scenarios not addressed by built-in policies, [a custom policy definition can be created](../governance/policy/tutorials/create-custom-policy-definition). Policy _assignments_ can be created from either built-in or custom policy _definitions_. |
| 36 | + |
| 37 | +### Property Types and Property Aliases in Policy Rules |
| 38 | + |
| 39 | +The [custom policy definition steps](../governance/policy/tutorials/create-custom-policy-definition) include identifying resource properties and property aliases, which are needed to create policy rules. |
| 40 | + |
| 41 | +To identify Cosmos DB property aliases, use the namespace `Microsoft.DocumentDB` with one of the methods shown in the custom policy definition steps. |
| 42 | + |
| 43 | +#### Using the Azure CLI: |
| 44 | +```azurecli-interactive |
| 45 | +# Login first with az login if not using Cloud Shell |
| 46 | +
|
| 47 | +# Get Azure Policy aliases for namespace Microsoft.DocumentDB |
| 48 | +az provider show --namespace Microsoft.DocumentDB --expand "resourceTypes/aliases" --query "resourceTypes[].aliases[].name" |
| 49 | +``` |
| 50 | + |
| 51 | +#### Using Azure PowerShell: |
| 52 | +```azurepowershell-interactive |
| 53 | +# Login first with Connect-AzAccount if not using Cloud Shell |
| 54 | +
|
| 55 | +# Use Get-AzPolicyAlias to list aliases for Microsoft.DocumentDB namespace |
| 56 | +(Get-AzPolicyAlias -NamespaceMatch 'Microsoft.DocumentDB').Aliases |
| 57 | +``` |
| 58 | + |
| 59 | +The output of listing Cosmos DB property aliases using one of the methods described above is a list of property alias names. Partial sample Cosmos DB output: |
| 60 | + |
| 61 | +```json |
| 62 | +[ |
| 63 | + "Microsoft.DocumentDB/databaseAccounts/sku.name", |
| 64 | + "Microsoft.DocumentDB/databaseAccounts/virtualNetworkRules[*]", |
| 65 | + "Microsoft.DocumentDB/databaseAccounts/virtualNetworkRules[*].id", |
| 66 | + "Microsoft.DocumentDB/databaseAccounts/isVirtualNetworkFilterEnabled", |
| 67 | + "Microsoft.DocumentDB/databaseAccounts/consistencyPolicy.defaultConsistencyLevel", |
| 68 | + "Microsoft.DocumentDB/databaseAccounts/enableAutomaticFailover", |
| 69 | + "Microsoft.DocumentDB/databaseAccounts/Locations", |
| 70 | + "Microsoft.DocumentDB/databaseAccounts/Locations[*]", |
| 71 | + "Microsoft.DocumentDB/databaseAccounts/Locations[*].locationName", |
| 72 | + "..." |
| 73 | +] |
| 74 | +``` |
| 75 | + |
| 76 | +Any of these property alias names can be used in [custom policy definition rules](../governance/policy/tutorials/create-custom-policy-definition#policy-rule). |
| 77 | + |
| 78 | +For example, to create a policy to check if a Cosmos DB SQL database's provisioned throughput is greater than a maximum allowable limit of 400 RU/s, a custom policy definition would include two rules: one to check for the specific type to check, and one for the specific property of the type. Both rules would use alias names. |
| 79 | + |
| 80 | +```json |
| 81 | +"policyRule": { |
| 82 | + "if": { |
| 83 | + "allOf": [ |
| 84 | + { |
| 85 | + "field": "type", |
| 86 | + "equals": "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/throughputSettings" |
| 87 | + }, |
| 88 | + { |
| 89 | + "field": "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/throughputSettings/default.resource.throughput", |
| 90 | + "greater": 400 |
| 91 | + } |
| 92 | + ] |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +Once a custom policy definition is saved, it can be used similarly to built-in policy definitions to create policy assignments. |
| 98 | + |
| 99 | +## Policy Compliance |
| 100 | + |
| 101 | +After policy assignments are created, Azure Policy evaluates the resources in the policy assignment's scope and assesses each resource's _compliance_ with the policy, applying the _effect_ specified in the policy to non-compliant resources. |
| 102 | + |
| 103 | +Compliance results and remediation details can be reviewed in the [Azure portal](../governance/policy/how-to/get-compliance-data#portal) or via the [Azure CLI](../governance/policy/how-to/get-compliance-data#command-line) or [Azure Monitor logs](../governance/policy/how-to/get-compliance-data#azure-monitor-logs). |
| 104 | + |
| 105 | +In the following example, two policy assignments were created. One policy assignment was created from a built-in policy definition to check that Azure Cosmos DB resources were deployed only to an allowed list of Azure regions. The other policy assignment was created from a custom policy definition to check that provisioned throughput on Azure Cosmos DB resources does not exceed a specified maximum. |
| 106 | + |
| 107 | +After the policy assignments were deployed, the compliance dashboard shows evaluation results (note that this can take up to 30 minutes after policy assignment deployment). |
| 108 | + |
| 109 | +The screenshot shows the following compliance evaluation results: |
| 110 | + |
| 111 | +- 0 of 1 Azure Cosmos DB accounts in scope are compliant with the policy assignment to check that resources were deployed to allowed regions |
| 112 | +- 1 of 2 Azure Cosmos DB database or collection resources in scope are compliant with the policy assignment to check for provisioned throughput exceeding the specified maximum |
| 113 | + |
| 114 | +:::image type="content" source="./media/policy/compliance.png" alt-text="Search for Cosmos DB built-in policy definitions"::: |
| 115 | + |
| 116 | +Non-compliant resources can be [remediated with Azure Policy](../governance/policy/how-to/remediate-resources). |
| 117 | + |
| 118 | +## Next Steps |
| 119 | + |
| 120 | +- [Review sample custom policy definitions for Azure Cosmos DB](https://github.com/Azure/azure-policy/tree/master/samples/CosmosDB) |
| 121 | +- [Create a policy assignment in the Azure Portal](../governance/policy/assign-policy-portal) |
| 122 | +- [Review Azure Policy built-in policy definitions for Azure Cosmos DB](./policy-samples.md) |
0 commit comments