Enforcing Azure Tag Compliance with Azure Policy: A Step-by-Step Guide to Automating Tagging
Enforce and inherit tags across your resources, ensuring compliance, cost tracking, and resource management in your Azure environment

Let’s break down the provided Azure Policy code shown below line by line. This policy is designed to enforce a specific tag (CostCenter) on resources by modifying them if the tag is missing. Here’s the detailed explanation:
Code Breakdown
"policyRule": {
"if": {
"field": "tags['CostCenter']",
"exists": "false"
},
"then": {
"effect": "modify",
"details": {
"roleDefinitionIds": [
"/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
],
"operations": [{
"operation": "addOrReplace",
"field": "tags['CostCenter']",
"value": "[resourcegroup().tags['CostCenter']]"
}]
}
}
}
1. policyRule Section
This is the main section that defines the logic of the policy. It consists of two parts: if (the condition) and then (the action to take if the condition is met).
2. if Condition
"if": {
"field": "tags['CostCenter']",
"exists": "false"
}
Purpose: This defines the condition that the policy evaluates.
field:tags['CostCenter']:This checks the
CostCentertag on the resource.The
fieldkeyword is used to specify the property of the resource to evaluate.
exists:false:This checks whether the
CostCentertag does not exist on the resource.If the tag is missing, the condition evaluates to
true, and the policy will take action.
3. then Action
"then": {
"effect": "modify",
"details": {
"roleDefinitionIds": [
"/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
],
"operations": [{
"operation": "addOrReplace",
"field": "tags['CostCenter']",
"value": "[resourcegroup().tags['CostCenter']]"
}]
}
}
- Purpose: This defines the action to take if the
ifcondition is true (i.e., theCostCentertag is missing).
effect: modify
The
modifyeffect is used to change the resource to bring it into compliance.In this case, it will add or replace the
CostCentertag on the resource.
details Section
This section specifies how the modify effect will be applied.
roleDefinitionIds
"roleDefinitionIds": [
"/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
]
Purpose: Specifies the role required to perform the
modifyoperation.Role ID:
b24988ac-6180-42a0-ab88-20f7382dd24ccorresponds to the Contributor role in Azure.- The Contributor role has the necessary permissions to modify resources, including adding or replacing tags.
operations
"operations": [{
"operation": "addOrReplace",
"field": "tags['CostCenter']",
"value": "[resourcegroup().tags['CostCenter']]"
}]
Purpose: Defines the specific operation to perform on the resource.
operation:addOrReplace:- This operation will add the tag if it doesn’t exist or replace its value if it already exists.
field:tags['CostCenter']:- Specifies the target field to modify, which is the
CostCentertag.
- Specifies the target field to modify, which is the
value:[resourcegroup().tags['CostCenter']]:This is a policy function that retrieves the value of the
CostCentertag from the resource group that the resource belongs to.The tag value is inherited from the resource group and applied to the resource.
How This Policy Works
Evaluation:
The policy evaluates whether the
CostCentertag exists on a resource.If the tag is missing, the condition (
if) evaluates totrue.
Action:
The policy applies the
modifyeffect.It uses the Contributor role to add or replace the
CostCentertag on the resource.The value of the
CostCentertag is inherited from the resource group.
Result:
If the
CostCentertag is missing, it will be added to the resource with the value from the resource group.If the
CostCentertag already exists but has a different value, it will be replaced with the value from the resource group.
Example Scenario
Resource Group:
- Has a
CostCentertag with the valueFinance.
- Has a
Resource:
- Does not have a
CostCentertag.
- Does not have a
Policy Action:
- The policy adds the
CostCentertag to the resource with the valueFinance(inherited from the resource group).
- The policy adds the
Key Points
Tag Inheritance: This policy ensures that resources inherit the
CostCentertag from their resource group, promoting consistency.Compliance: The
modifyeffect automatically brings non-compliant resources into compliance by adding or updating the tag.Role-Based Access: The policy requires the Contributor role to perform the modification, ensuring proper permissions are in place.
Roberto w/assistance from DeepSeek





