Skip to content

Commit ac5e56c

Browse files
authored
Merge pull request #224117 from SwathiDhanwada-MSFT/patch-65
(AzureCXP) fixes MicrosoftDocs/azure-docs#103542
2 parents a999471 + a34f02b commit ac5e56c

File tree

1 file changed

+42
-41
lines changed

1 file changed

+42
-41
lines changed

articles/governance/policy/assign-policy-terraform.md

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This quickstart steps you through the process of creating a policy assignment to
1212
machines that aren't using managed disks.
1313

1414
At the end of this process, you'll successfully identify virtual machines that aren't using managed
15-
disks. They're _non-compliant_ with the policy assignment.
15+
disks across subscription. They're _non-compliant_ with the policy assignment.
1616

1717
## Prerequisites
1818

@@ -37,54 +37,55 @@ for Azure Policy use the
3737

3838
1. Create a new folder named `policy-assignment` and change directories into it.
3939

40-
1. Create `main.tf` with the following code:
41-
42-
```hcl
43-
provider "azurerm" {
44-
features {}
45-
}
46-
47-
terraform {
48-
required_providers {
49-
azurerm = {
50-
source = "hashicorp/azurerm"
51-
version = ">= 2.96.0"
52-
}
53-
}
54-
}
40+
2. Create `main.tf` with the following code:
41+
42+
> [!NOTE]
43+
> To create a Policy Assignment at a Management Group use the [azurerm_management_group_policy_assignment](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/management_group_policy_assignment) resource, for a Resource Group use the [azurerm_resource_group_policy_assignment](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group_policy_assignment) and for a Subscription use the [azurerm_subscription_policy_assignment](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subscription_policy_assignment) resource.
44+
45+
46+
```terraform
47+
provider "azurerm" {
48+
features {}
49+
}
50+
51+
terraform {
52+
required_providers {
53+
azurerm = {
54+
source = "hashicorp/azurerm"
55+
version = ">= 2.96.0"
56+
}
57+
}
58+
}
59+
60+
resource "azurerm_subscription_policy_assignment" "auditvms" {
61+
name = "audit-vm-manageddisks"
62+
subscription_id = var.cust_scope
63+
policy_definition_id = "/providers/Microsoft.Authorization/policyDefinitions/06a78e20-9358-41c9-923c-fb736d382a4d"
64+
description = "Shows all virtual machines not using managed disks"
65+
display_name = "Audit VMs without managed disks assignment"
66+
}
67+
```
68+
3. Create `variables.tf` with the following code:
5569
56-
resource "azurerm_resource_policy_assignment" "auditvms" {
57-
name = "audit-vm-manageddisks"
58-
resource_id = var.cust_scope
59-
policy_definition_id = "/providers/Microsoft.Authorization/policyDefinitions/06a78e20-9358-41c9-923c-fb736d382a4d"
60-
description = "Shows all virtual machines not using managed disks"
61-
display_name = "Audit VMs without managed disks assignment"
70+
```terraform
71+
variable "cust_scope" {
72+
default = "{scope}"
6273
}
6374
```
6475
65-
1. Create `variables.tf` with the following code:
66-
67-
```hcl
68-
variable "cust_scope" {
69-
default = "{scope}"
70-
}
71-
```
72-
73-
A scope determines what resources or grouping of resources the policy assignment gets enforced
74-
on. It could range from a management group to an individual resource. Be sure to replace
75-
`{scope}` with one of the following patterns:
76+
A scope determines what resources or grouping of resources the policy assignment gets enforced on. It could range from a management group to an individual resource. Be sure to replace `{scope}` with one of the following patterns based on the declared resource:
7677
7778
- Subscription: `/subscriptions/{subscriptionId}`
7879
- Resource group: `/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}`
7980
- Resource: `/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/[{parentResourcePath}/]`
8081
81-
1. Create `output.tf` with the following code:
82+
4. Create `output.tf` with the following code:
8283
83-
```hcl
84-
output "assignment_id" {
85-
value = azurerm_resource_policy_assignment.auditvms.id
86-
}
87-
```
84+
```terraform
85+
output "assignment_id" {
86+
value = azurerm_resource_policy_assignment.auditvms.id
87+
}
88+
```
8889
8990
## Initialize Terraform and create plan
9091
@@ -140,11 +141,11 @@ returned.
140141
## Identify non-compliant resources
141142

142143
To view the resources that aren't compliant under this new assignment, use the _assignment\_id_
143-
returned by `terraform apply`. With it, run the following command to get the resource IDs of the
144+
returned by ```terraform apply```. With it, run the following command to get the resource IDs of the
144145
non-compliant resources that are output into a JSON file:
145146

146147
```console
147-
armclient post "/subscriptions/<subscriptionID>/resourceGroups/<rgName>/providers/Microsoft.PolicyInsights/policyStates/latest/queryResults?api-version=2019-10-01&$filter=IsCompliant eq false and PolicyAssignmentId eq '<policyAssignmentID>'&$apply=groupby((ResourceId))" > <json file to direct the output with the resource IDs into>
148+
armclient post "/subscriptions/<subscriptionID>/providers/Microsoft.PolicyInsights/policyStates/latest/queryResults?api-version=2019-10-01&$filter=IsCompliant eq false and PolicyAssignmentId eq '<policyAssignmentID>'&$apply=groupby((ResourceId))" > <json file to direct the output with the resource IDs into>
148149
```
149150

150151
Your results resemble the following example:

0 commit comments

Comments
 (0)