Terraform:创建Azure策略(允许的位置)-缺少值

kb5ga3dv  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(110)

我遇到了以下错误消息,但我无法确定我错过了什么。
错误:创建作用域策略分配(作用域:“/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx”策略分配名称:“允许的位置”):400错误提示:策略参数缺少值:策略参数“listOfAllowedLocations”缺少值。
下面是我的Terraform代码

provider "azurerm" {
  features {}
  }

  terraform {
  required_providers {
      azurerm = {
          source = "hashicorp/azurerm"
          version = ">= 2.96.0"
      }
  }
  }

  resource "azurerm_subscription_policy_assignment" "Allowedlocations2" {
    name = "Allowed locations"
    subscription_id = var.cust_scope
    policy_definition_id = "/providers/Microsoft.Authorization/policyDefinitions/e56962a6-4747-49cd-b67b-bf8b01975c4c"
    description = "This policy enables you to restrict the locations your organization can specify when deploying resources."
    display_name = "Allowed locations"
    metadata = <<METADATA
      {
      "category": "General"
      }
METADATA

    parameters = <<PARAMETERS
      {
          "listOfAllowedLocations": {
            "type": "Array",
            "metadata": {
              "description": "The list of locations that can be specified when deploying resources.",
              "strongType": "location",
              "displayName": "Allowed locations",
              "strongType": "location"
            },
            "defaultValue": [
              "eastus"
            ],
            "allowedValues": [
              "eastus",
              "eastus2"
            ]
          }
        }
PARAMETERS

  }

作为一个Terraform初学者,我错过了一个值,但我无法找到它。如果有人知道它是什么以及如何找到它,请告诉我。

azurerm_subscription_policy_assignment.Allowedlocations2: Creating...
╷
│ Error: creating Scoped Policy Assignment (Scope: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
│ Policy Assignment Name: "Allowed locations"): unexpected status 400 with error: **PolicyParametersMissingValue: The policy parameters 'listOfAllowedLocations' are missing a value.**
│
│   with azurerm_subscription_policy_assignment.Allowedlocations2,
│   on main.tf line 65, in resource "azurerm_subscription_policy_assignment" "Allowedlocations2":
│   65:   resource "azurerm_subscription_policy_assignment" "Allowedlocations2" {
│
│ creating Scoped Policy Assignment (Scope: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
│ Policy Assignment Name: "Allowed locations"): unexpected status 400 with error: PolicyParametersMissingValue: The policy parameters 'listOfAllowedLocations' are missing a value.
╵
smtd7mpg

smtd7mpg1#

错误:创建作用域策略分配(作用域:“/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx”策略分配名称:“允许的位置”):400错误提示:策略参数缺少值:策略参数“listOfAllowedLocations”缺少值。
上述错误消息的原因表明,listOfAllowedLocations参数在您的策略assignment.it中缺少一个值,似乎您错误地定义了parameters块。
当我尝试在不使用错误格式的listOfAllowedLocations的情况下传递它时,我也会得到与您相同的错误。

下面是Terraform code更新,用于创建策略定义和分配。

provider "azurerm" {
  features {}
}

data "azurerm_subscription" "current" {}

data "azurerm_policy_definition" "example" {
  display_name = "Allowed locations"
}

output "id" {
  value = data.azurerm_policy_definition.example.id
}

resource "azurerm_subscription_policy_assignment" "example" {
  name                 = "Allowed Locations Policy-1"
  policy_definition_id = data.azurerm_policy_definition.example.id
  subscription_id      = data.azurerm_subscription.current.id

  parameters = jsonencode({
    listOfAllowedLocations = {
      value = ["eastus", "westus"]
    }
  })
}

Terraform适用:

一旦运行,上述代码策略将分配给subscription范围,如下所示。

相关问题