azure - Terraform - Azure 资源组 : Error: ID cannot be a Resource Group ID

标签 azure terraform terraform-provider-azure

我目前正在将我们的云基础设施迁移到 Terraform。我正在尝试将策略分配给资源组。但是它失败并出现错误

│ Error: ID cannot be a Resource Group ID

│ with azurerm_resource_policy_assignment.example, on main.tf line 50, in resource a zurerm_resource_policy_assignment" "example":

│ 50: resource_id = azurerm_resource_group.example.id

# Configure the Microsoft Azure provider
provider "azurerm" {
  features {}
}

data "azurerm_subscription" "primary" {
}

data "azurerm_client_config" "example" {
}

# Define Policy
resource "azurerm_policy_definition" "example" {
  display_name = "only-deploy-in-westus"
  name        = "only-deploy-in-westus"
  policy_type = "Custom"
  mode        = "All"

  policy_rule = <<POLICY_RULE
    {
    "if": {
      "not": {
        "field": "location",
        "equals": "westus"
      }
    },
    "then": {
      "effect": "Deny"
    }
  }
POLICY_RULE
}

# Create a Resource Group if it doesn’t exist
resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "West US"
}

# Assign Permission
resource "azurerm_role_assignment" "example" {
  scope                = azurerm_resource_group.example.id
  role_definition_name = "Reader"
  principal_id         = data.azurerm_client_config.example.object_id
}

# Assign Policy
resource "azurerm_resource_policy_assignment" "example" {
  name                 = "example-policy-assignment"
  resource_id          = azurerm_resource_group.example.id
  policy_definition_id = azurerm_policy_definition.example.id
}

最佳答案

要将策略分配给资源组,您不能使用 azurerm_resource_policy_assignment,但需要使用 azurerm_resource_group_policy_assignment

resource "azurerm_resource_group_policy_assignment" "example" {
  name                 = "example"
  resource_group_id    = azurerm_resource_group.example.id
  policy_definition_id = azurerm_policy_definition.example.id
}

引用:https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group_policy_assignment

关于azure - Terraform - Azure 资源组 : Error: ID cannot be a Resource Group ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73780821/

相关文章:

azure - 无法删除 azure 上的 docker 镜像,给出 "Error: No such image"

node.js - 如何找出 CreateBlockBlobFromStream 方法的流长度

c# - 在单个 C# Web 服务中检索记录并保存到数据库

amazon-web-services - 解析 ECS Fargate 任务中的服务主机名

azure - 如何使用 terraform 创建两个 VNET 并在每个 VNET 中创建一些子网

azure - Gitlab 管道作业 token 失败

Terraform 模块 aws 访问 key

amazon-web-services - 使用 Terraform 启用 Inspector v2

azure - 使用 Terraform 将 Synapse 工作区分配给存储容器

azure - 通过 terraform 进行 Elasticsearch 升级的正确方法