Enforcing Azure Policy for Convert Tags either being all lowercase or all uppercase

578 Views Asked by At

Error : Editing policy definition 'Enforce Lowercase or Uppercase on Tags and their Values' in 'subscription ABCXXX' failed. A function or parameter in policy assignment '' associated with the policy definition '' could not be validated. Please either fix the policy or remove the policy assignment to unblock. If using template functions, try following the tips in: https://aka.ms/policy-avoiding-template-failures. The inner exception 'Unable to evaluate the template language function 'json'. The argument provided is not a valid JSON string.

"parameters": {
  "toLowerOrToUpper": {
    "type": "String",
    "metadata": {
      "displayName": "Enforce Lowercase or Uppercase",
      "description": "If set to Lowercase then the Policy will convert all tags on existing and new resources to lowercase. Uppercase will convert them to uppercase."
    },
    "allowedValues": [
      "Lowercase",
      "Uppercase"
    ]
  }
},
"policyRule": {
  "if": {
    "allOf": [
      {
        "field": "type",
        "equals": "Microsoft.Resources/subscriptions/resourceGroups"

      }
    ]
  },
  "then": {
    "effect": "modify",
      "operations": [
        {
          "operation": "addOrReplace",
          "field": "tags",
          "value": "[json(if(equals(parameters('toLowerOrToUpper'), 'Lowercase'), toLower(string(field('tags'))),toUpper(string(field('tags')))))]"
        }
      ]
    }
  }
}
1

There are 1 best solutions below

0
AndreiMS On

I'm not sure how to get this entirely working but in the "then" block I think you should have "effect" and "details" which contains "operations" and "roleDefinitionIds".

Here is an example (modify it to suit your needs):

"then": {
    "effect": "modify",
    "details": {
        "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
        ],
        "operations": [{
            "operation": "add",
            "field": "[concat('tags[', parameters('tagName'), ']')]",
            "value": "[parameters('tagValue')]"
        }]
    }
}

Source: https://learn.microsoft.com/en-us/azure/governance/policy/samples/pattern-effect-details