How can I create an Azure policy that validates Resource Group Names

和自甴很熟 提交于 2019-12-11 01:48:39

问题


I am trying to create an Azure policy which I can assign at the subscription level, and control the naming of the resource groups in the subscription.

Policies need to target a resource type or otherwise limit their application, else they apply globally to all resources.

What resource type (or other method) can I use to limit my validation to the resource group name only?

Here is what I am trying:

$definition = New-AzureRmPolicyDefinition -Name resourceGroupNamePatterns 
   -Description "Restrict resource group names to allowed prefixes only" -Policy '{
    "if": {
        "allOf": [
          {
            "not": {
              "field": "name",
              "like": "Pattern1-*"
            }
          },
          {
            "not": {
              "field": "name",
              "like": "Pattern2-*"
            }
          },
          {
            "field": "type",
            "equals": "Microsoft.Resources/subscriptions/resourcegroups"
          }
        ]
    },
    "then": {
        "effect": "deny"
    }
}'

回答1:


Not sure if this question is still relevant, but at the time of posting Azure Policy did not support evaluation on resource groups.

The policy definition provided in the question is correct.

Please try updating your powershell version, and updating the policy definition. It will default to mode: all which in turn will enable policy evaluation on resource groups.

Documentation about Policy mode: https://docs.microsoft.com/en-us/azure/azure-policy/policy-definition

Mode

The mode determines which resource types will be evaluated for a policy. The supported modes are:

  • all: evaluate resource groups and all resource types
  • indexed: only evaluate resource types that support tags and location

We recommend that you set mode to all. All policy definitions created through the portal use the all mode. If you use PowerShell or Azure CLI, you need to specify the mode parameter and set it to all.




回答2:


The resource groups are Microsoft.Resources/subscriptions/resourcegroups type. You can kinda infer that from the resource provider operations:

Get-AzureRmProviderOperation 'Microsoft.Resources/*'


来源:https://stackoverflow.com/questions/46053057/how-can-i-create-an-azure-policy-that-validates-resource-group-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!