Skip to content

Commit e607cc2

Browse files
authored
Merge pull request #266588 from davidsmatlak/ds-update-policy-bicep-20240216
Updates Azure Policy Bicep quickstart
2 parents 4d505d0 + 663d826 commit e607cc2

File tree

3 files changed

+175
-63
lines changed

3 files changed

+175
-63
lines changed
Lines changed: 175 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
---
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
2+
title: "Quickstart: Create policy assignment using Bicep file"
3+
description: In this quickstart, you create an Azure Policy assignment to identify non-compliant resources using a Bicep file.
4+
ms.date: 02/20/2024
55
ms.topic: quickstart
66
ms.custom: subject-bicepqs, devx-track-bicep, devx-track-azurecli, devx-track-azurepowershell
77
---
88

99
# Quickstart: Create a policy assignment to identify non-compliant resources by using a Bicep file
1010

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.
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 and audits virtual machines that don't use managed disks. After you create the policy assignment, you identify non-compliant virtual machines.
1212

1313
[!INCLUDE [About Bicep](../../../includes/resource-manager-quickstart-bicep-introduction.md)]
1414

15-
> [!NOTE]
16-
> Azure Policy is a free service. For more information, go to [Overview of Azure Policy](./overview.md).
17-
1815
## Prerequisites
1916

2017
- If you don't have an Azure account, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin.
2118
- [Bicep](../../azure-resource-manager/bicep/install.md).
2219
- [Azure PowerShell](/powershell/azure/install-az-ps) or [Azure CLI](/cli/azure/install-azure-cli).
2320
- [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.
21+
- `Microsoft.PolicyInsights` must be [registered](../../azure-resource-manager/management/resource-providers-and-types.md) in your Azure subscription. To register a resource provider, you must have permission to register resource providers. That permission is included in the Contributor and Owner roles.
22+
- A resource group with at least one virtual machine that doesn't use managed disks.
2523

2624
## Review the Bicep file
2725

@@ -31,7 +29,7 @@ Create the following Bicep file as _policy-assignment.bicep_.
3129

3230
1. Open Visual Studio Code and select **File** > **New Text File**.
3331
1. Copy and paste the Bicep file into Visual Studio Code.
34-
1. Select **File** > **Save** and use the filename _policy-policy-assignment.bicep_.
32+
1. Select **File** > **Save** and use the filename _policy-assignment.bicep_.
3533

3634
```bicep
3735
param policyAssignmentName string = 'audit-vm-managed-disks'
@@ -55,7 +53,7 @@ resource assignment 'Microsoft.Authorization/policyAssignments@2023-04-01' = {
5553
output assignmentId string = assignment.id
5654
```
5755

58-
The resource type defined in the Bicep file is [Microsoft.Authorization/policyAssignments](/azure/templates/microsoft.authorization/policyassignments).
56+
The resource type defined in the Bicep file is [Microsoft.Authorization/policyAssignments](/azure/templates/microsoft.authorization/policyassignments). The Bicep file creates a policy assignment named _audit-vm-managed-disks_.
5957

6058
For more information about Bicep files:
6159

@@ -91,122 +89,236 @@ az account set --subscription <subscriptionID>
9189

9290
---
9391

94-
The following commands create a resource group and deploy the policy definition.
92+
You can verify if `Microsoft.PolicyInsights` is registered. If it isn't, you can run a command to register the resource provider.
9593

9694
# [PowerShell](#tab/azure-powershell)
9795

9896
```azurepowershell
99-
New-AzResourceGroup -Name "PolicyGroup" -Location "westus"
97+
Get-AzResourceProvider -ProviderNamespace 'Microsoft.PolicyInsights' |
98+
Select-Object -Property ResourceTypes, RegistrationState
10099
101-
New-AzResourceGroupDeployment `
102-
-Name PolicyDeployment `
103-
-ResourceGroupName PolicyGroup `
104-
-TemplateFile policy-assignment.bicep
100+
Register-AzResourceProvider -ProviderNamespace 'Microsoft.PolicyInsights'
105101
```
106102

107103
# [Azure CLI](#tab/azure-cli)
108104

109105
```azurecli
110-
az group create --name "PolicyGroup" --location "westus"
106+
az provider show \
107+
--namespace Microsoft.PolicyInsights \
108+
--query "{Provider:namespace,State:registrationState}" \
109+
--output table
110+
111+
az provider register --namespace Microsoft.PolicyInsights
112+
```
113+
114+
---
115+
116+
The following commands deploy the policy definition to your resource group. Replace `<resourceGroupName>` with your resource group name:
117+
118+
# [PowerShell](#tab/azure-powershell)
119+
120+
```azurepowershell
121+
$rg = Get-AzResourceGroup -Name '<resourceGroupName>'
122+
123+
$deployparms = @{
124+
Name = 'PolicyDeployment'
125+
ResourceGroupName = $rg.ResourceGroupName
126+
TemplateFile = 'policy-assignment.bicep'
127+
}
128+
129+
New-AzResourceGroupDeployment @deployparms
130+
```
131+
132+
The `$rg` variable stores properties for the resource group. The `$deployparms` variable uses [splatting](/powershell/module/microsoft.powershell.core/about/about_splatting) to create parameter values and improve readability. The `New-AzResourceGroupDeployment` command uses the parameter values defined in the `$deployparms` variable.
133+
134+
- `Name` is the deployment name displayed in the output and in Azure for the resource group's deployments.
135+
- `ResourceGroupName` uses the `$rg.ResourceGroupName` property to get the name of your resource group where the policy is assigned.
136+
- `TemplateFile` specifies the Bicep file's name and location on your local computer.
137+
138+
# [Azure CLI](#tab/azure-cli)
139+
140+
```azurecli
141+
rgname=$(az group show --resource-group <resourceGroupName> --query name --output tsv)
111142
112143
az deployment group create \
113144
--name PolicyDeployment \
114-
--resource-group PolicyGroup \
145+
--resource-group $rgname \
115146
--template-file policy-assignment.bicep
116147
```
117148

149+
The `rgname` variable uses an expression to get your resource group's name used in the deployment command. The Azure CLI commands use a backslash (`\`) for line continuation to improve readability.
150+
151+
- `name` is the deployment name displayed in the output and in Azure for the resource group's deployments.
152+
- `resource-group` is the name of your resource group where the policy is assigned.
153+
- `template-file` specifies the Bicep file's name and location on your local computer.
154+
118155
---
119156

120-
The Bicep file outputs the policy `assignmentId`. You create a variable for the policy assignment ID in the commands that validate the deployment.
157+
You can verify the policy assignment's deployment with the following command:
158+
159+
# [PowerShell](#tab/azure-powershell)
160+
161+
The command uses the `$rg.ResourceId` property to get the resource group's ID.
162+
163+
```azurepowershell
164+
Get-AzPolicyAssignment -Name 'audit-vm-managed-disks' -Scope $rg.ResourceId
165+
```
166+
167+
```output
168+
Name : audit-vm-managed-disks
169+
ResourceId : /subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.Authorization/policyAssignments/audit-vm-managed-disks
170+
ResourceName : audit-vm-managed-disks
171+
ResourceGroupName : {resourceGroupName}
172+
ResourceType : Microsoft.Authorization/policyAssignments
173+
SubscriptionId : {subscriptionId}
174+
PolicyAssignmentId : /subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.Authorization/policyAssignments/audit-vm-managed-disks
175+
Properties : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.Policy.PsPolicyAssignmentProperties
176+
```
121177

122-
## Validate the deployment
178+
# [Azure CLI](#tab/azure-cli)
179+
180+
The `rgid` variable uses an expression to get the resource group's ID used to show the policy assignment.
181+
182+
```azurecli
183+
rgid=$(az group show --resource-group $rgname --query id --output tsv)
123184
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.
185+
az policy assignment show --name "audit-vm-managed-disks" --scope $rgid
186+
```
125187

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.
188+
The output is verbose but resembles the following example:
189+
190+
```output
191+
"description": "Policy assignment to resource group scope created with Bicep file",
192+
"displayName": "audit-vm-managed-disks",
193+
"enforcementMode": "Default",
194+
"id": "/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.Authorization/policyAssignments/audit-vm-managed-disks",
195+
"identity": null,
196+
"location": null,
197+
"metadata": {
198+
"createdBy": "11111111-1111-1111-1111-111111111111",
199+
"createdOn": "2024-02-20T20:57:09.574944Z",
200+
"updatedBy": null,
201+
"updatedOn": null
202+
},
203+
"name": "audit-vm-managed-disks",
204+
"nonComplianceMessages": [
205+
{
206+
"message": "Virtual machines should use managed disks",
207+
"policyDefinitionReferenceId": null
208+
}
209+
]
210+
```
129211

130-
The **Compliance state** for a new policy assignment is shown as **Not started** because it takes a few minutes to become active.
212+
---
131213

132-
:::image type="content" source="./media/assign-policy-bicep/policy-compliance.png" alt-text="Screenshot of compliance details on the Policy Compliance page.":::
214+
## Identify non-compliant resources
133215

134-
For more information, go to [How compliance works](./concepts/compliance-states.md).
216+
After the policy assignment is deployed, virtual machines that are deployed to the resource group are audited for compliance with the managed disk policy.
135217

136-
You can also get the compliance state with Azure PowerShell or Azure CLI.
218+
The compliance state for a new policy assignment takes a few minutes to become active and provide results about the policy's state.
137219

138220
# [PowerShell](#tab/azure-powershell)
139221
```azurepowershell
140-
# Verifies policy assignment was deployed
141-
$rg = Get-AzResourceGroup -Name "PolicyGroup"
142-
Get-AzPolicyAssignment -Name "audit-vm-managed-disks" -Scope $rg.ResourceId
222+
$complianceparms = @{
223+
ResourceGroupName = $rg.ResourceGroupName
224+
PolicyAssignmentName = 'audit-vm-managed-disks'
225+
Filter = 'IsCompliant eq false'
226+
}
143227
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
228+
Get-AzPolicyState @complianceparms
147229
```
148230

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.
231+
The `$complianceparms` variable creates parameter values used in the `Get-AzPolicyState` command.
232+
233+
- `ResourceGroupName` gets the resource group name from the `$rg.ResourceGroupName` property.
234+
- `PolicyAssignmentName` specifies the name used when the policy assignment was created.
235+
- `Filter` uses an expression to find resources that aren't compliant with the policy assignment.
236+
237+
Your results resemble the following example and `ComplianceState` shows `NonCompliant`:
238+
239+
```output
240+
Timestamp : 2/20/2024 18:55:45
241+
ResourceId : /subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/microsoft.compute/virtualmachines/{vmId}
242+
PolicyAssignmentId : /subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/microsoft.authorization/policyassignments/audit-vm-managed-disks
243+
PolicyDefinitionId : /providers/microsoft.authorization/policydefinitions/06a78e20-9358-41c9-923c-fb736d382a4d
244+
IsCompliant : False
245+
SubscriptionId : {subscriptionId}
246+
ResourceType : Microsoft.Compute/virtualMachines
247+
ResourceLocation : {location}
248+
ResourceGroup : {resourceGroupName}
249+
ResourceTags : tbd
250+
PolicyAssignmentName : audit-vm-managed-disks
251+
PolicyAssignmentOwner : tbd
252+
PolicyAssignmentScope : /subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}
253+
PolicyDefinitionName : 06a78e20-9358-41c9-923c-fb736d382a4d
254+
PolicyDefinitionAction : audit
255+
PolicyDefinitionCategory : tbd
256+
ManagementGroupIds : {managementGroupId}
257+
ComplianceState : NonCompliant
258+
AdditionalProperties : {[complianceReasonCode, ]}
259+
```
150260

151261
# [Azure CLI](#tab/azure-cli)
152262

153263
```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
264+
policyid=$(az policy assignment show \
265+
--name "audit-vm-managed-disks" \
266+
--scope $rgid \
267+
--query id \
268+
--output tsv)
157269
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
270+
az policy state list --resource $policyid --filter "(isCompliant eq false)"
161271
```
162272

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.
273+
The `policyid` variable uses an expression to get the policy assignment's ID.
164274

165-
---
275+
The `filter` parameter limits the output to non-compliant resources.
166276

167-
## Clean up resources
168-
169-
To remove the assignment from Azure, follow these steps:
277+
The `az policy state list` output is verbose, but for this article the `complianceState` shows `NonCompliant`.
170278

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
174-
assignment**.
175-
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.":::
279+
```output
280+
"complianceState": "NonCompliant",
281+
"components": null,
282+
"effectiveParameters": "",
283+
"isCompliant": false,
284+
```
177285

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.
286+
---
180287

181-
You can also delete the policy assignment and resource group with Azure PowerShell or Azure CLI.
288+
## Clean up resources
182289

183290
# [PowerShell](#tab/azure-powershell)
291+
184292
```azurepowershell
185-
Remove-AzPolicyAssignment -Id $policyid.ResourceId
186-
Remove-AzResourceGroup -Name "PolicyGroup"
293+
Remove-AzPolicyAssignment -Name 'audit-vm-managed-disks' -Scope $rg.ResourceId
294+
```
295+
296+
To sign out of your Azure PowerShell session:
187297

188-
# Sign out of Azure
298+
```azurepowershell
189299
Disconnect-AzAccount
190300
```
191301

192302
# [Azure CLI](#tab/azure-cli)
193303

194304
```azurecli
195-
az policy assignment delete --name "audit-vm-managed-disks" --scope $rg
196-
az group delete --name PolicyGroup
305+
az policy assignment delete --name "audit-vm-managed-disks" --scope $rgid
306+
```
307+
308+
To sign out of your Azure CLI session:
197309

198-
# Sign out of Azure
310+
```azurecli
199311
az logout
200312
```
201313

202314
---
203315

204316
## Next steps
205317

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.
318+
In this quickstart, you assigned a built-in policy definition to a resource group scope and reviewed its compliance state. The policy definition audits if the virtual machines in the resource group are compliant and identifies resources that aren't compliant.
207319

208320
To learn more about assigning policies to validate that new resources are compliant, continue to the
209321
tutorial.
210322

211323
> [!div class="nextstepaction"]
212-
> [Creating and managing policies](./tutorials/create-and-manage.md)
324+
> [Tutorial: Create and manage policies to enforce compliance](./tutorials/create-and-manage.md)
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)