简体   繁体   English

Terraform - Azure 资源组:错误:ID 不能是资源组 ID

[英]Terraform - Azure Resource Group: Error: ID cannot be a Resource Group ID

I am currently migrating our cloud infrastructure over to Terraform.我目前正在将我们的云基础架构迁移到 Terraform。 I am trying to assign a policy to a resource group.我正在尝试将策略分配给资源组。 However it fails with an error但是它失败并出现错误

│ Error: ID cannot be a Resource Group ID │ 错误:ID 不能是资源组 ID

│ with azurerm_resource_policy_assignment.example, on main.tf line 50, in resource a zurerm_resource_policy_assignment" "example": │ 使用 azurerm_resource_policy_assignment.example,在 main.tf 第 50 行,资源中的 zurerm_resource_policy_assignment""example":

│ 50: resource_id = azurerm_resource_group.example.id │ 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
}

It should be:它应该是:

resource_group_id = azurerm_resource_group.example.id

rather then而不是

resource_id = azurerm_resource_group.example.id

In order to assign a policy to a resource group you cannot use azurerm_resource_policy_assignment but would need to use azurerm_resource_group_policy_assignment .为了将策略分配给资源组,您不能使用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
}

Reference: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group_policy_assignment参考: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group_policy_assignment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Terraform 资源组 ID 作为变量 - Terraform Resource Group ID as a Variable 在 azure 中使用 terraform 创建资源组:创建后无法直接找到资源组 - Creating a resource group with terraform in azure: Cannot find resource group directly after creating it 使用 terraform 创建新的 azure 资源组时出错 - Error creating new azure resource group using terraform 在具有terraform的订阅下创建一个Azure资源组 - create an azure resource group under a subscription with terraform 如何使用 terraform 在 azure 中管理现有资源组 - How to manage existing resource group in azure with terraform 使用 terraform 创建 Azure 资源组 - Azure resource group creation using terraform azure devops 中的 Azure 资源组部署失败并出现错误 [“消息”:“ID 为 '#######' 的部署存在”] - Azure resource group deployment in azure devops failed with error [“Message”: “Deployment with id '#######' exists”] 是否可以在特定资源组 ID 或名称上创建 Azure 策略 - Is it possible to create Azure policy on a specific resource group ID or name 使用资源组和系统 ID 获取订阅 ID - Getting Subscription ID with Resource Group and System ID 使用 terraform 在预建资源组中部署 azure 资源 - Deploy azure resource in prebuilt resource group using terraform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM