|
| 1 | +# Terraform Template - Azure Policy Assignment (with Managed Identity) |
| 2 | + |
| 3 | +Costa Rica |
| 4 | + |
| 5 | +[](https://github.com/) |
| 6 | +[brown9804](https://github.com/brown9804) |
| 7 | + |
| 8 | +Last updated: 2026-02-09 |
| 9 | + |
| 10 | +------------------------------------------ |
| 11 | + |
| 12 | +> This template contains Terraform configurations to create an Azure Policy Assignment scoped to a Resource Group, using a User Assigned Managed Identity. |
| 13 | +
|
| 14 | +> [!NOTE] |
| 15 | +> Some Azure Policies (for example, `DeployIfNotExists` / remediation scenarios) require an identity on the assignment. This template always creates a User Assigned Managed Identity and attaches it to the assignment. |
| 16 | +
|
| 17 | +## File Descriptions |
| 18 | + |
| 19 | +- **main.tf**: Creates the Resource Group, User Assigned Managed Identity, and the Azure Policy Assignment. |
| 20 | +- **variables.tf**: Defines the input variables used in the Terraform configuration. |
| 21 | +- **provider.tf**: Configures the Azure provider to interact with Azure resources. |
| 22 | +- **terraform.tfvars**: Provides example values for the variables defined in `variables.tf`. |
| 23 | +- **outputs.tf**: Defines outputs such as the policy assignment ID and identity IDs. |
| 24 | + |
| 25 | +## Variables |
| 26 | + |
| 27 | +| Variable Name | Description | Type | Example Value | |
| 28 | +| --- | --- | --- | --- | |
| 29 | +| `resource_group_name` | The name of the Azure Resource Group to create and scope the assignment to. | string | `"rg-identity-security-dev"` | |
| 30 | +| `location` | The Azure region where the Resource Group and identity will be created. | string | `"East US"` | |
| 31 | +| `managed_identity_name` | The name of the User Assigned Managed Identity to create. | string | `"id-policy-identity-security-dev-001"` | |
| 32 | +| `policy_assignment_name` | The name of the Azure Policy Assignment. | string | `"pa-identity-security-dev-001"` | |
| 33 | +| `policy_definition_id` | The policy definition resource ID (built-in or custom). | string | `"/providers/Microsoft.Authorization/policyDefinitions/<id>"` | |
| 34 | +| `policy_assignment_display_name` | Optional display name for the assignment. | string | `"Identity/Security policy assignment (dev)"` | |
| 35 | +| `policy_assignment_description` | Optional description for the assignment. | string | `"Example policy assignment scoped to a resource group."` | |
| 36 | +| `policy_parameters_json` | Optional policy parameters JSON string. | string | `jsonencode({ effect = { value = "Audit" } })` | |
| 37 | +| `enforce` | Whether the policy should be enforced. | bool | `true` | |
| 38 | +| `tags` | A map of tags to assign to the resources. | map(string) | `{ "env": "dev" }` | |
| 39 | + |
| 40 | +## Usage |
| 41 | + |
| 42 | +1. Authenticate: |
| 43 | + |
| 44 | + ```sh |
| 45 | + az login |
| 46 | + ``` |
| 47 | + |
| 48 | +2. Ensure Azure CLI has the correct active subscription: |
| 49 | + |
| 50 | + ```sh |
| 51 | + az account show |
| 52 | + # If needed: |
| 53 | + az account set --subscription "<subscription-id-or-name>" |
| 54 | + ``` |
| 55 | + |
| 56 | +3. Initialize: |
| 57 | + |
| 58 | + ```sh |
| 59 | + terraform init -upgrade |
| 60 | + ``` |
| 61 | + |
| 62 | +4. Validate and plan: |
| 63 | + |
| 64 | + ```sh |
| 65 | + terraform validate |
| 66 | + terraform plan |
| 67 | + ``` |
| 68 | + |
| 69 | +5. Apply: |
| 70 | + |
| 71 | + ```sh |
| 72 | + terraform apply -auto-approve |
| 73 | + ``` |
| 74 | + |
| 75 | +> [!NOTES] |
| 76 | +> |
| 77 | +> - This template creates the Resource Group for you. |
| 78 | +> - If you leave `policy_definition_id` as a placeholder, `terraform apply` will fail with `PolicyDefinitionNotFound`. Use Azure CLI to find a valid definition ID (built-in or custom), for example: `az policy definition list --query "[0].id" -o tsv`. |
| 79 | +> - If your policy requires remediation, you may need to grant the assignment identity additional Azure RBAC permissions at the assignment scope. |
| 80 | +
|
| 81 | +<!-- START BADGE --> |
| 82 | +<div align="center"> |
| 83 | + <img src="https://img.shields.io/badge/Total%20views-1646-limegreen" alt="Total views"> |
| 84 | + <p>Refresh Date: 2026-02-09</p> |
| 85 | +</div> |
| 86 | +<!-- END BADGE --> |
0 commit comments