|
1 | 1 | ---
|
2 |
| -title: "Quickstart: New policy assignment with Bicep file" |
3 |
| -description: In this quickstart, you use a Bicep file to create a policy assignment to identify non-compliant resources. |
4 |
| -ms.date: 03/24/2022 |
| 2 | +title: Create a policy assignment with Bicep file |
| 3 | +description: In this quickstart, you use a Bicep file to create an Azure policy assignment that identifies non-compliant resources. |
| 4 | +ms.date: 01/08/2024 |
5 | 5 | ms.topic: quickstart
|
6 | 6 | ms.custom: subject-bicepqs, devx-track-bicep
|
7 | 7 | ---
|
8 |
| -# Quickstart: Create a policy assignment to identify non-compliant resources by using a Bicep file |
9 | 8 |
|
10 |
| -The first step in understanding compliance in Azure is to identify the status of your resources. |
11 |
| -This quickstart steps you through the process of using a |
12 |
| -[Bicep](https://github.com/Azure/bicep) file compiled to an Azure Resource |
13 |
| -Manager (ARM) deployment template to create a policy assignment to identify virtual machines that |
14 |
| -aren't using managed disks. At the end of this process, you'll successfully identify virtual |
15 |
| -machines that aren't using managed disks. They're _non-compliant_ with the policy assignment. |
| 9 | +# Quickstart: Create a policy assignment to identify non-compliant resources by using a Bicep file |
16 | 10 |
|
17 |
| -[!INCLUDE [About Azure Resource Manager](../../../includes/resource-manager-quickstart-introduction.md)] |
| 11 | +In this quickstart, you use a Bicep file to create a policy assignment that validates resource's compliance with an Azure policy. The policy is assigned to a resource group scope and audits if virtual machines use managed disks. Virtual machines deployed in the resource group that don't use managed disks are _non-compliant_ with the policy assignment. |
18 | 12 |
|
19 |
| -If your environment meets the prerequisites and you're familiar with using ARM templates, select the |
20 |
| -**Deploy to Azure** button. The template opens in the Azure portal. |
| 13 | +[!INCLUDE [About Bicep](../../../includes/resource-manager-quickstart-bicep-introduction.md)] |
21 | 14 |
|
22 |
| -:::image type="content" source="../../media/template-deployments/deploy-to-azure.svg" alt-text="Button to deploy the ARM template for assigning an Azure Policy to Azure." border="false" link="https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-quickstart-templates%2Fmaster%2Fquickstarts%2Fmicrosoft.authorization%2Fazurepolicy-assign-builtinpolicy-resourcegroup%2Fazuredeploy.json"::: |
| 15 | +> [!NOTE] |
| 16 | +> Azure Policy is a free service. For more information, go to [Overview of Azure Policy](./overview.md). |
23 | 17 |
|
24 | 18 | ## Prerequisites
|
25 | 19 |
|
26 |
| -- If you don't have an Azure subscription, create a [free](https://azure.microsoft.com/free/) |
27 |
| - account before you begin. |
28 |
| -- Bicep version `0.3` or higher installed. If you don't yet have Bicep CLI or need to update, see |
29 |
| - [Install Bicep](../../azure-resource-manager/bicep/install.md). |
| 20 | +- If you don't have an Azure account, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin. |
| 21 | +- [Bicep](../../azure-resource-manager/bicep/install.md). |
| 22 | +- [Azure PowerShell](/powershell/azure/install-az-ps) or [Azure CLI](/cli/azure/install-azure-cli). |
| 23 | +- [Visual Studio Code](https://code.visualstudio.com/) and the [Bicep extension for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-bicep). |
| 24 | +- `Microsoft.PolicyInsights` must be [registered](../../azure-resource-manager/management/resource-providers-and-types.md) in your Azure subscription. |
30 | 25 |
|
31 | 26 | ## Review the Bicep file
|
32 | 27 |
|
33 |
| -In this quickstart, you create a policy assignment and assign a built-in policy definition called [_Audit VMs that do not use managed disks_](https://github.com/Azure/azure-policy/blob/master/built-in-policies/policyDefinitions/Compute/VMRequireManagedDisk_Audit.json). For a partial |
34 |
| -list of available built-in policies, see [Azure Policy samples](./samples/index.md). |
| 28 | +The Bicep file creates a policy assignment for a resource group scope and assigns the built-in policy definition [Audit VMs that do not use managed disks](https://github.com/Azure/azure-policy/blob/master/built-in-policies/policyDefinitions/Compute/VMRequireManagedDisk_Audit.json). For a list of available built-in policies, see [Azure Policy samples](./samples/index.md). |
35 | 29 |
|
36 |
| -Create the following Bicep file as `assignment.bicep`: |
| 30 | +Create the following Bicep file as _policy-assignment.bicep_. |
| 31 | + |
| 32 | +1. Open Visual Studio Code and select **File** > **New Text File**. |
| 33 | +1. Copy and paste the Bicep file into Visual Studio Code. |
| 34 | +1. Select **File** > **Save** and use the filename _policy-policy-assignment.bicep_. |
37 | 35 |
|
38 | 36 | ```bicep
|
39 |
| -param policyAssignmentName string = 'audit-vm-manageddisks' |
| 37 | +param policyAssignmentName string = 'audit-vm-managed-disks' |
40 | 38 | param policyDefinitionID string = '/providers/Microsoft.Authorization/policyDefinitions/06a78e20-9358-41c9-923c-fb736d382a4d'
|
41 | 39 |
|
42 |
| -resource assignment 'Microsoft.Authorization/policyAssignments@2021-09-01' = { |
43 |
| - name: policyAssignmentName |
44 |
| - scope: subscriptionResourceId('Microsoft.Resources/resourceGroups', resourceGroup().name) |
45 |
| - properties: { |
46 |
| - policyDefinitionId: policyDefinitionID |
47 |
| - } |
| 40 | +resource assignment 'Microsoft.Authorization/policyAssignments@2023-04-01' = { |
| 41 | + name: policyAssignmentName |
| 42 | + scope: resourceGroup() |
| 43 | + properties: { |
| 44 | + policyDefinitionId: policyDefinitionID |
| 45 | + description: 'Policy assignment to resource group scope created with Bicep file' |
| 46 | + displayName: 'audit-vm-managed-disks' |
| 47 | + nonComplianceMessages: [ |
| 48 | + { |
| 49 | + message: 'Virtual machines should use managed disks' |
| 50 | + } |
| 51 | + ] |
| 52 | + } |
48 | 53 | }
|
49 | 54 |
|
50 | 55 | output assignmentId string = assignment.id
|
51 | 56 | ```
|
52 | 57 |
|
53 |
| -The resource defined in the file is: |
| 58 | +The resource type defined in the Bicep file is [Microsoft.Authorization/policyAssignments](/azure/templates/microsoft.authorization/policyassignments). |
54 | 59 |
|
55 |
| -- [Microsoft.Authorization/policyAssignments](/azure/templates/microsoft.authorization/policyassignments) |
| 60 | +For more information about Bicep files: |
56 | 61 |
|
57 |
| -## Deploy the template |
| 62 | +- To find more Bicep samples, go to [Browse code samples](/samples/browse/?expanded=azure&languages=bicep). |
| 63 | +- To learn more about template reference's for deployments, go to [Azure template reference](/azure/templates/microsoft.authorization/allversions). |
| 64 | +- To learn how to develop Bicep files, go to [Bicep documentation](../../azure-resource-manager/bicep/overview.md). |
| 65 | +- To learn about subscription-level deployments, go to [Subscription deployments with Bicep files](../../azure-resource-manager/bicep/deploy-to-subscription.md). |
58 | 66 |
|
59 |
| -> [!NOTE] |
60 |
| -> Azure Policy service is free. For more information, see |
61 |
| -> [Overview of Azure Policy](./overview.md). |
| 67 | +## Deploy the Bicep file |
| 68 | + |
| 69 | +You can deploy the Bicep file with Azure PowerShell or Azure CLI. |
| 70 | + |
| 71 | +From a Visual Studio Code terminal session, connect to Azure. If you have more than one subscription, run the commands to set context to your subscription. Replace `<subscriptionID>` with your Azure subscription ID. |
| 72 | + |
| 73 | +# [PowerShell](#tab/azure-powershell) |
| 74 | +```azurepowershell |
| 75 | +Connect-AzAccount |
| 76 | +
|
| 77 | +# Run these commands if you have multiple subscriptions |
| 78 | +Get-AzSubScription |
| 79 | +Set-AzContext -Subscription <subscriptionID> |
| 80 | +``` |
| 81 | + |
| 82 | +# [Azure CLI](#tab/azure-cli) |
| 83 | + |
| 84 | +```azurecli |
| 85 | +az login |
| 86 | +
|
| 87 | +# Run these commands if you have multiple subscriptions |
| 88 | +az account list --output table |
| 89 | +az account set --subscription <subscriptionID> |
| 90 | +``` |
| 91 | + |
| 92 | +--- |
62 | 93 |
|
63 |
| -After the Bicep CLI is installed and file created, you can deploy the Bicep file with: |
| 94 | +The following commands create a resource group and deploy the policy definition. |
64 | 95 |
|
65 | 96 | # [PowerShell](#tab/azure-powershell)
|
66 | 97 |
|
67 |
| -```azurepowershell-interactive |
| 98 | +```azurepowershell |
| 99 | +New-AzResourceGroup -Name "PolicyGroup" -Location "westus" |
| 100 | +
|
68 | 101 | New-AzResourceGroupDeployment `
|
69 | 102 | -Name PolicyDeployment `
|
70 | 103 | -ResourceGroupName PolicyGroup `
|
71 |
| - -TemplateFile assignment.bicep |
| 104 | + -TemplateFile policy-assignment.bicep |
72 | 105 | ```
|
73 | 106 |
|
74 | 107 | # [Azure CLI](#tab/azure-cli)
|
75 | 108 |
|
76 |
| -```azurecli-interactive |
| 109 | +```azurecli |
| 110 | +az group create --name "PolicyGroup" --location "westus" |
| 111 | +
|
77 | 112 | az deployment group create \
|
78 | 113 | --name PolicyDeployment \
|
79 | 114 | --resource-group PolicyGroup \
|
80 |
| - --template-file assignment.bicep |
| 115 | + --template-file policy-assignment.bicep |
81 | 116 | ```
|
82 | 117 |
|
83 | 118 | ---
|
84 | 119 |
|
85 |
| -Some other resources: |
86 |
| - |
87 |
| -- To find more samples templates, see |
88 |
| - [Azure Quickstart Template](https://azure.microsoft.com/resources/templates/?resourceType=Microsoft.Authorization&pageNumber=1&sort=Popular). |
89 |
| -- To see the template reference, go to |
90 |
| - [Azure template reference](/azure/templates/microsoft.authorization/allversions). |
91 |
| -- To learn how to develop ARM templates, see |
92 |
| - [Azure Resource Manager documentation](../../azure-resource-manager/management/overview.md). |
93 |
| -- To learn subscription-level deployment, see |
94 |
| - [Create resource groups and resources at the subscription level](../../azure-resource-manager/templates/deploy-to-subscription.md). |
| 120 | +The Bicep file outputs the policy `assignmentId`. You create a variable for the policy assignment ID in the commands that validate the deployment. |
95 | 121 |
|
96 | 122 | ## Validate the deployment
|
97 | 123 |
|
98 |
| -Select **Compliance** in the left side of the page. Then locate the _Audit VMs that do not use |
99 |
| -managed disks_ policy assignment you created. |
| 124 | +After the policy assignment is deployed, virtual machines that are deployed to the _PolicyGroup_ resource group are audited for compliance with the managed disk policy. |
100 | 125 |
|
101 |
| -:::image type="content" source="./media/assign-policy-template/policy-compliance.png" alt-text="Screenshot of compliance details on the Policy Compliance page." border="false"::: |
| 126 | +1. Sign in to [Azure portal](https://portal.azure.com) |
| 127 | +1. Go to **Policy** and select **Compliance** on the left side of the page. |
| 128 | +1. Search for the _audit-vm-managed-disks_ policy assignment. |
102 | 129 |
|
103 |
| -If there are any existing resources that aren't compliant with this new assignment, they appear |
104 |
| -under **Non-compliant resources**. |
| 130 | +The **Compliance state** for a new policy assignment is shown as **Not started** because it takes a few minutes to become active. |
105 | 131 |
|
106 |
| -For more information, see |
107 |
| -[How compliance works](./concepts/compliance-states.md). |
| 132 | +:::image type="content" source="./media/assign-policy-bicep/policy-compliance.png" alt-text="Screenshot of compliance details on the Policy Compliance page."::: |
108 | 133 |
|
109 |
| -## Clean up resources |
| 134 | +For more information, go to [How compliance works](./concepts/compliance-states.md). |
| 135 | + |
| 136 | +You can also get the compliance state with Azure PowerShell or Azure CLI. |
| 137 | + |
| 138 | +# [PowerShell](#tab/azure-powershell) |
| 139 | +```azurepowershell |
| 140 | +# Verifies policy assignment was deployed |
| 141 | +$rg = Get-AzResourceGroup -Name "PolicyGroup" |
| 142 | +Get-AzPolicyAssignment -Name "audit-vm-managed-disks" -Scope $rg.ResourceId |
| 143 | +
|
| 144 | +# Shows the number of non-compliant resources and policies |
| 145 | +$policyid = (Get-AzPolicyAssignment -Name "audit-vm-managed-disks" -Scope $rg.ResourceId) |
| 146 | +Get-AzPolicyStateSummary -ResourceId $policyid.ResourceId |
| 147 | +``` |
| 148 | + |
| 149 | +The `$rg` variable stores the resource group's properties and `Get-AzPolicyAssignment` shows your policy assignment. The `$policyid` variable stores the policy assignment's resource ID, and `Get-AzPolicyStateSummary` shows the number of non-compliant resources and policies. |
| 150 | + |
| 151 | +# [Azure CLI](#tab/azure-cli) |
| 152 | + |
| 153 | +```azurecli |
| 154 | +# Verifies policy assignment was deployed |
| 155 | +rg=$(az group show --resource-group PolicyGroup --query id --output tsv) |
| 156 | +az policy assignment show --name "audit-vm-managed-disks" --scope $rg |
110 | 157 |
|
111 |
| -To remove the assignment created, follow these steps: |
| 158 | +# Shows the number of non-compliant resources and policies |
| 159 | +policyid=$(az policy assignment show --name "audit-vm-managed-disks" --scope $rg --query id --output tsv) |
| 160 | +az policy state summarize --resource $policyid |
| 161 | +``` |
| 162 | + |
| 163 | +The `$rg` variable stores the resource group's properties and `az policy assignment show` displays your policy assignment. The `$policyid` variable stores the policy assignment's resource ID and `az policy state summarize` shows the number of non-compliant resources and policies. |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +## Clean up resources |
112 | 168 |
|
113 |
| -1. Select **Compliance** (or **Assignments**) in the left side of the Azure Policy page and locate |
114 |
| - the _Audit VMs that do not use managed disks_ policy assignment you created. |
| 169 | +To remove the assignment from Azure, follow these steps: |
115 | 170 |
|
116 |
| -1. Right-click the _Audit VMs that do not use managed disks_ policy assignment and select **Delete |
| 171 | +1. Select **Compliance** in the left side of the Azure Policy page. |
| 172 | +1. Locate the _audit-vm-managed-disks_ policy assignment. |
| 173 | +1. Right-click the _audit-vm-managed-disks_ policy assignment and select **Delete |
117 | 174 | assignment**.
|
118 | 175 |
|
119 |
| - :::image type="content" source="./media/assign-policy-template/delete-assignment.png" alt-text="Screenshot of using the context menu to delete an assignment from the Compliance page." border="false"::: |
| 176 | + :::image type="content" source="./media/assign-policy-bicep/delete-assignment.png" alt-text="Screenshot of the context menu to delete an assignment from the Policy Compliance page."::: |
120 | 177 |
|
121 |
| -1. Delete the `assignment.bicep` file. |
| 178 | +1. Delete the resource group _PolicyGroup_. Go to the Azure resource group and select **Delete resource group**. |
| 179 | +1. Delete the _policy-assignment.bicep_ file. |
| 180 | + |
| 181 | +You can also delete the policy assignment and resource group with Azure PowerShell or Azure CLI. |
| 182 | + |
| 183 | +# [PowerShell](#tab/azure-powershell) |
| 184 | +```azurepowershell |
| 185 | +Remove-AzPolicyAssignment -Id $policyid.ResourceId |
| 186 | +Remove-AzResourceGroup -Name "PolicyGroup" |
| 187 | +
|
| 188 | +# Sign out of Azure |
| 189 | +Disconnect-AzAccount |
| 190 | +``` |
| 191 | + |
| 192 | +# [Azure CLI](#tab/azure-cli) |
| 193 | + |
| 194 | +```azurecli |
| 195 | +az policy assignment delete --name "audit-vm-managed-disks" --scope $rg |
| 196 | +az group delete --name PolicyGroup |
| 197 | +
|
| 198 | +# Sign out of Azure |
| 199 | +az logout |
| 200 | +``` |
| 201 | + |
| 202 | +--- |
122 | 203 |
|
123 | 204 | ## Next steps
|
124 | 205 |
|
125 |
| -In this quickstart, you assigned a built-in policy definition to a scope and evaluated its |
126 |
| -compliance report. The policy definition validates that all the resources in the scope are compliant |
127 |
| -and identifies which ones aren't. |
| 206 | +In this quickstart, you assigned a built-in policy definition to a resource group scope and reviewed its compliance report. The policy definition audits if the virtual machine resources in the resource group are compliant and identifies resources that aren't compliant. |
128 | 207 |
|
129 | 208 | To learn more about assigning policies to validate that new resources are compliant, continue to the
|
130 |
| -tutorial for: |
| 209 | +tutorial. |
131 | 210 |
|
132 | 211 | > [!div class="nextstepaction"]
|
133 | 212 | > [Creating and managing policies](./tutorials/create-and-manage.md)
|
0 commit comments