Skip to content

Commit 78e5e2b

Browse files
committed
edits
1 parent 6be9a20 commit 78e5e2b

File tree

1 file changed

+35
-37
lines changed

1 file changed

+35
-37
lines changed

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

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
---
22
title: "Quickstart: New policy assignment with Terraform"
3-
description: In this quickstart, you use Terraform and HCL syntax to create a policy assignment to identify non-compliant resources.
4-
ms.date: 03/01/2023
3+
description: In this quickstart, you use Terraform and Hashicorp Configuration Language (HCL) syntax to create a policy assignment to identify non-compliant resources.
4+
ms.date: 03/25/2024
55
ms.topic: quickstart
66
ms.custom: devx-track-terraform
77
ms.tool: terraform
88
---
9+
910
# Quickstart: Create a policy assignment to identify non-compliant resources using Terraform
1011

1112
The first step in understanding compliance in Azure is to identify the status of your resources.
1213
This quickstart steps you through the process of creating a policy assignment to identify virtual
1314
machines that aren't using managed disks.
1415

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

1818
## Prerequisites
1919

@@ -28,17 +28,14 @@ disks across subscription. They're _non-compliant_ with the policy assignment.
2828

2929
## Create the Terraform configuration, variable, and output file
3030

31-
In this quickstart, you create a policy assignment and assign the **Audit VMs that do not use
32-
managed disks** (`06a78e20-9358-41c9-923c-fb736d382a4d`) definition. This policy definition
33-
identifies resources that aren't compliant to the conditions set in the policy definition.
31+
In this quickstart, you create a policy assignment and assign the [Audit VMs that do not use managed disks](https://github.com/Azure/azure-policy/blob/master/built-in-policies/policyDefinitions/Compute/VMRequireManagedDisk_Audit.json) definition. This policy definition identifies resources that aren't compliant to the conditions set in the policy definition.
3432

35-
First, configure the Terraform configuration, variable, and output files. The Terraform resources
36-
for Azure Policy use the
37-
[Azure Provider](https://www.terraform.io/docs/providers/azurerm/index.html).
33+
Configure the Terraform configuration, variable, and output files. The Terraform resources
34+
for Azure Policy use the [Azure Provider](https://www.terraform.io/docs/providers/azurerm/index.html).
3835

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

41-
2. Create `main.tf` with the following code:
38+
1. Create `main.tf` with the following code:
4239

4340
> [!NOTE]
4441
> 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.
@@ -55,7 +52,7 @@ for Azure Policy use the
5552
source = "hashicorp/azurerm"
5653
version = ">= 2.96.0"
5754
}
58-
}
55+
}
5956
}
6057
6158
resource "azurerm_subscription_policy_assignment" "auditvms" {
@@ -66,7 +63,8 @@ for Azure Policy use the
6663
display_name = "Audit VMs without managed disks assignment"
6764
}
6865
```
69-
3. Create `variables.tf` with the following code:
66+
67+
1. Create `variables.tf` with the following code:
7068
7169
```terraform
7270
variable "cust_scope" {
@@ -80,7 +78,7 @@ for Azure Policy use the
8078
- Resource group: `/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}`
8179
- Resource: `/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/[{parentResourcePath}/]`
8280
83-
4. Create `output.tf` with the following code:
81+
1. Create `output.tf` with the following code:
8482
8583
```terraform
8684
output "assignment_id" {
@@ -90,7 +88,7 @@ for Azure Policy use the
9088
9189
## Initialize Terraform and create plan
9290
93-
Next, initialize Terraform to download the necessary providers and then create a plan.
91+
Initialize Terraform to download the necessary providers and then create a plan.
9492
9593
1. Run the [terraform init](https://www.terraform.io/docs/commands/init.html) command. This command
9694
downloads the Azure modules required to create the Azure resources in the Terraform
@@ -124,7 +122,7 @@ Next, initialize Terraform to download the necessary providers and then create a
124122
125123
## Apply the Terraform execution plan
126124

127-
Last, apply the execution plan.
125+
Apply the execution plan.
128126

129127
Run the [terraform apply](https://www.terraform.io/docs/commands/apply.html) command and specify the
130128
`assignment.tfplan` already created.
@@ -135,7 +133,7 @@ terraform apply assignment.tfplan
135133

136134
:::image type="content" source="./media/assign-policy-terraform/terraform-apply.png" alt-text="Screenshot of running the terraform apply command and the resulting resource creation.":::
137135

138-
With the "Apply complete! Resources: 1 added, 0 changed, 0 destroyed." message, the policy
136+
With the `Apply complete! Resources: 1 added, 0 changed, 0 destroyed.` message, the policy
139137
assignment is now created. Since we defined the `outputs.tf` file, the _assignment\_id_ is also
140138
returned.
141139

@@ -153,25 +151,25 @@ Your results resemble the following example:
153151

154152
```json
155153
{
156-
"@odata.context": "https://management.azure.com/subscriptions/<subscriptionId>/providers/Microsoft.PolicyInsights/policyStates/$metadata#latest",
157-
"@odata.count": 3,
158-
"value": [{
159-
"@odata.id": null,
160-
"@odata.context": "https://management.azure.com/subscriptions/<subscriptionId>/providers/Microsoft.PolicyInsights/policyStates/$metadata#latest/$entity",
161-
"ResourceId": "/subscriptions/<subscriptionId>/resourcegroups/<rgname>/providers/microsoft.compute/virtualmachines/<virtualmachineId>"
162-
},
163-
{
164-
"@odata.id": null,
165-
"@odata.context": "https://management.azure.com/subscriptions/<subscriptionId>/providers/Microsoft.PolicyInsights/policyStates/$metadata#latest/$entity",
166-
"ResourceId": "/subscriptions/<subscriptionId>/resourcegroups/<rgname>/providers/microsoft.compute/virtualmachines/<virtualmachine2Id>"
167-
},
168-
{
169-
"@odata.id": null,
170-
"@odata.context": "https://management.azure.com/subscriptions/<subscriptionId>/providers/Microsoft.PolicyInsights/policyStates/$metadata#latest/$entity",
171-
"ResourceId": "/subscriptions/<subscriptionName>/resourcegroups/<rgname>/providers/microsoft.compute/virtualmachines/<virtualmachine3ID>"
172-
}
173-
174-
]
154+
"@odata.context": "https://management.azure.com/subscriptions/<subscriptionId>/providers/Microsoft.PolicyInsights/policyStates/$metadata#latest",
155+
"@odata.count": 3,
156+
"value": [
157+
{
158+
"@odata.id": null,
159+
"@odata.context": "https://management.azure.com/subscriptions/<subscriptionId>/providers/Microsoft.PolicyInsights/policyStates/$metadata#latest/$entity",
160+
"ResourceId": "/subscriptions/<subscriptionId>/resourcegroups/<rgname>/providers/microsoft.compute/virtualmachines/<virtualmachineId>"
161+
},
162+
{
163+
"@odata.id": null,
164+
"@odata.context": "https://management.azure.com/subscriptions/<subscriptionId>/providers/Microsoft.PolicyInsights/policyStates/$metadata#latest/$entity",
165+
"ResourceId": "/subscriptions/<subscriptionId>/resourcegroups/<rgname>/providers/microsoft.compute/virtualmachines/<virtualmachine2Id>"
166+
},
167+
{
168+
"@odata.id": null,
169+
"@odata.context": "https://management.azure.com/subscriptions/<subscriptionId>/providers/Microsoft.PolicyInsights/policyStates/$metadata#latest/$entity",
170+
"ResourceId": "/subscriptions/<subscriptionName>/resourcegroups/<rgname>/providers/microsoft.compute/virtualmachines/<virtualmachine3ID>"
171+
}
172+
]
175173
}
176174
```
177175

@@ -204,4 +202,4 @@ To learn more about assigning policies to validate that new resources are compli
204202
tutorial for:
205203

206204
> [!div class="nextstepaction"]
207-
> [Creating and managing policies](./tutorials/create-and-manage.md)
205+
> [Tutorial: Create and manage policies to enforce compliance](./tutorials/create-and-manage.md)

0 commit comments

Comments
 (0)