【问题标题】:Create a policy exemption where the scope is a resource (Azure & Terraform)创建范围是资源的策略豁免(Azure 和 Terraform)
【发布时间】:2021-12-31 23:15:42
【问题描述】:

您能否使用 Terraform 在 Azure 中的资源(而非 RG)范围内创建策略豁免? 因为我找到了这个模块:

  module "policy_exemptions" {
  source  = "globalbao/policy-exemptions/azurerm"
  version = "0.2.1"
  policyExemptions = {
    exemption1 = {
      deploymentMode     = "Incremental"
      name               = "exemption1"
      displayName        = "exemption1 for Insert-Your-RG-Name1"
      description        = "exemption1 waives compliance on an resource group"
      resourceGroupName  = "Insert-Your-RG-Name1"
      policyAssignmentId = "/providers/Microsoft.Management/managementGroups/production/providers/Microsoft.Authorization/policyAssignments/2f97de7d41f348529e23d8ae"
      policyDefinitionReferenceIds = []
      exemptionCategory = "Waiver"
      expiresOn         = "2025-12-29"
      metadata = {}
    }
  }
}

但是您只能输入 RG 名称而不是资源...
也许我不明白语法?

【问题讨论】:

  • 您可以使用资源的导出资源属性作为参数的值:terraform.io/docs/language/expressions/…。我们需要更具体的信息来提供进一步帮助。
  • 您好@NoaHochman,您使用的模块有一个 ARM 模板,该模板设置了政策豁免,因为没有直接资源可以从 terraform 创建政策豁免。 ARM 模板缺少范围,如果您定义它,则可以将其使用限制为资源组中的特定资源。但现在它被应用于资源组中存在的所有资源。
  • 如果回答对您有帮助,请Accept it as an Answer,以便遇到相同问题的其他人可以找到此解决方案并解决他们的问题。

标签: azure terraform cloud terraform-provider-azure azure-policy


【解决方案1】:

正如我在 cmets 中提到的,政策豁免模块没有提及范围,因此它被应用于资源组。

作为一种解决方案,您必须在初始化 terraform 后更改模块文件中的一些内容。

第 1 步:转到Module>>policy_exemptions>>policyExemption.json。在参数和资源中添加Scope,如下所示:

第 2 步:转到Module>>policy_exemptions>>variables.tf。在变量中添加Scope,如下图:

第 3 步:转到Module>>policy_exemptions>>main.tf。在parameters_content 中的resource_group_deployment 块中添加作用域,如下所示:

第 4 步:然后你可以像下面这样的模块:

  provider "azurerm" {
    features{}
  }
  module "policy_exemptions" {
  source  = "globalbao/policy-exemptions/azurerm"
  version = "0.2.1"
  policyExemptions = {
    exemption1 = {
      deploymentMode     = "Incremental"
      name               = "exemption1"
      displayName        = "exemption1 for cloudshell storage acocunt"
      description        = "exemption1 waives compliance on an resource group"
      resourceGroupName  = "ansuman-resourcegroup"
      policyAssignmentId = "/subscriptions/88xxxxb30-xxxx-xxxx-xxxx-xxxxc93573ae/resourceGroups/ansuman-resourcegroup/providers/Microsoft.Authorization/policyAssignments/b3f5ccb5b1a74a2db0401c0e"
      policyDefinitionReferenceIds = []
      exemptionCategory = "Waiver"
      expiresOn         = "2025-12-29"
      scope             = "/subscriptions/88xxxxb30-xxxx-xxxx-xxxx-xxxxc93573ae/resourceGroups/ansuman-resourcegroup/providers/Microsoft.Storage/storageAccounts/cloudshellansuman123"
      metadata = {}
    }
  }
}

我测试了修改后的模块,以在特定资源(即存储帐户)上为策略 Storage account keys should not be expired 添加豁免。

输出:

您可以在我的Github Repo 提交后找到修改后的代码。您可以检查以下三个提交:

  • 更新 main.tf ...
  • 更新 variables.tf ...
  • 更新 policyExemptions.json

【讨论】:

    猜你喜欢
    • 2023-01-27
    • 2023-01-12
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    相关资源
    最近更新 更多