Azure Policy - value count not behaving as expected

Niclas Madsen 0 Reputation points MVP
2023-01-29T19:19:49.16+00:00

Hello,

I am trying to create a policy that will only allow certain values in a specific tag name.

https://learn.microsoft.com/en-us/azure/governance/policy/concepts/definition-structure#value-count-examples

According to the examples and the description of "current", then it should evaluate each array value and see if they match the where clause.

E.g. Environment = DEV
Environment = STG
Environment = PRD.

This behavior seem to not be true though?!

User's image

This is a picture of the non-compliance for a resource group that SHOULD BE compliant, since the tag is Environment = DEV.

I tried to put the Count.value = DEV and that one is working, but as soon as there are more values in the array, it is not working. It is showing compliance only for the incorrect tags.

Furthermore, I have tried to use match, like, and notmatch instead of equals. Also not after where. I also tried to parameterized the array with no difference.

{
  "mode": "All",
  "policyRule": {
    "if": {
      "allOf": [
        {
          "field": "type",
          "equals": "Microsoft.Resources/subscriptions/resourceGroups"
        },
        {
          "count": {
            "value": [
              "DEV",
              "STG",
              "PRD"
            ],
            "where": {
              "field": "[concat('tags[', parameters('envTagName'), ']')]",
              "equals": "[current()]"
            }
          },
          "greaterOrEquals": 1
        }
      ]
    },
    "then": {
      "effect": "[parameters('effect')]"
    }
  },
  "parameters": {
    "effect": {
      "type": "String",
      "metadata": {
        "displayName": "Effect",
        "description": "Enable or disable the execution of the audit policy"
      },
      "allowedValues": [
        "Audit",
        "Deny",
        "Disabled"
      ],
      "defaultValue": "Audit"
    },
    "envTagName": {
      "type": "String",
      "metadata": {
        "displayName": "Environment Tag Name",
        "description": "Name of the tag, such as 'environment'"
      },
      "defaultValue": "Environment"
    }
  }
}
Azure Policy
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
1,014 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Luke Murray 11,436 Reputation points MVP Volunteer Moderator
    2023-01-29T22:27:50.6833333+00:00

    Take a look at the Array, its a new feature in Q4 of 2022:

    "parameters": {
        "allowedLocations": {
            "type": "array",
            "metadata": {
                "description": "The list of allowed locations for resources.",
                "displayName": "Allowed locations",
                "strongType": "location"
            },
            "defaultValue": "eastus2",
            "allowedValues": [
                "eastus2",
                "eastus",
                "westus2"
            ]
    
        }
    }
    

    https://learn.microsoft.com/en-us/azure/governance/policy/how-to/author-policies-for-arrays


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.