【Azure Policy】使用Azure Policy来精准控制资源的创建类型

问题描述

在 Azure 中使用 Azure Policy 来禁止创建某些资源,是一种非常有效的治理手段。可以通过定义策略(Policy Definition)并分配(Assignment)到订阅或资源组,来实现对资源创建的限制。

下面将介绍使用Deny策略来阻止创建 Azure AI services 中的 Document intelligence资源。


image.png

问题解答

在使用Deny的策略中,用 if 条件来匹配需要禁止的资源类型,比如 虚拟机的资源类型为 Microsoft.Compute/virtualMachines。它的PolicyRule设置就非常简单,只需要一个条件就可以。

示例如下:

   "policyRule": {
      "if": {
        "field": "type",
        "equals": "Microsoft.Compute/virtualMachines"
      },
      "then": {
        "effect": "deny"
      }
    }

但对于Azure AI services 中的 Document intelligence资源, 只根据类型 type 为 Microsoft.CognitiveServices/accounts 就无法实现精准判断 Document Intelligence资源,因为Anomaly detector,Content moderator等资源的类型都是Microsoft.CognitiveServices/accounts。

根据对Document Intelligence的资源文件进行分析,发现他们之间使用Kind属性值进行区分。比如:

  • Document intelligence 的Kind值为FormRecognizer
  • Anomaly detector 的Kind值为AnomalyDetector
  • Content moderator 的Kind值为ContentModerator

所以,可以使用如下的PolicyRule来实现禁止创建Document intelligence资源:

{
  "mode": "All",
  "policyRule": {
    "if": {
      "allOf": [
        {
          "field": "type",
          "equals": "Microsoft.CognitiveServices/accounts"
        },
        {
          "field": "Kind",
          "equals": "FormRecognizer"
        }
      ]
    },
    "then": {
      "effect": "deny"
    }
  },
  "parameters": {}
}

创建好策略并分配后,验证效果如下:

[图片上传失败...(image-5e6d53-1750857877686)]

参考资料

What are the resource providers for Azure services : https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/azure-services-resource-providers

Azure Policy 定义结构策略规则 : https://docs.azure.cn/zh-cn/governance/policy/concepts/definition-structure-policy-rule

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容