for_each 中的动态块出现 Terraform 错误

Liq*_*uid 4 terraform terraform-provider-azure

我正在尝试使用 azuremrm 资源storage_share实例化 azure storage_share 映射。根据设计,我需要能够使用同一块实例化多个存储共享;每个共享可能有也可能没有“acl”部分。

我正在考虑使用 for_each 与动态块结合使用来解决这个问题,如相关的 SE 问题中所示:

主.tf

resource "azurerm_storage_share" "storage_share" {
  for_each             = var.storage_share_map
  name                 = each.key
  storage_account_name = azurerm_storage_account.sa.name
  quota                = each.value.quota

  dynamic "acl" {
    for_each = each.value.acl
    content {
      id = acl.value.id

      access_policy {
        permissions = acl.value.access_policy.permissions
        start       = acl.value.access_policy.start
        expiry      = acl.value.access_policy.expiry
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

该变量将定义为:

variable "storage_share_map" {
  type = map(object({
    quota = number,
    acl = object({
      id = string,
      access_policy = object({
        expiry      = string,
        permissions = string,
        start       = string
      })
    }),
  }))
  default     = {}
}
Run Code Online (Sandbox Code Playgroud)

后来在我的测试中参数化为:

storage_share_map = { 
  my-share-2 = {
    quota = 123,
    acl = {
      id = "a-id",
      access_policy = {
        expiry      = "ISO8061 UTC TIME"
        permissions = "rwdl"
        start       = "ISO8601 UTC TIME"
      },
    },
  }
Run Code Online (Sandbox Code Playgroud)

但是,在测试时,terraform 返回以下输出:

Error: Unsupported attribute

  on .terraform\modules\sa\main.tf line 83, in resource "azurerm_storage_share" "storage_share":
  83:       id = acl.value.id
    |----------------
    | acl.value is object with 3 attributes

This object does not have an attribute named "id".


Error: Unsupported attribute

  on .terraform\modules\sa\main.tf line 83, in resource "azurerm_storage_share" "storage_share":
  83:       id = acl.value.id
    |----------------
    | acl.value is "a-id"

This value does not have any attributes.


Error: Unsupported attribute

  on .terraform\modules\sa\main.tf line 86, in resource "azurerm_storage_share" "storage_share":
  86:         permissions = acl.value.access_policy.permissions
    |----------------
    | acl.value is object with 3 attributes

This object does not have an attribute named "access_policy".


Error: Unsupported attribute

  on .terraform\modules\sa\main.tf line 86, in resource "azurerm_storage_share" "storage_share":
  86:         permissions = acl.value.access_policy.permissions
    |----------------
    | acl.value is "a-id"

This value does not have any attributes.
Run Code Online (Sandbox Code Playgroud)

据我了解,这里的问题是动态块内的 for_each 要么格式错误,要么行为不当: acl.value 似乎既被视为字符串“a-id”,又带有三个属性(?)。

Terraform版本 0.12.26 Azurerm版本 2.26.0

任何见解将不胜感激。

相关问题: 在使用 for_each 创建的资源内使用 for_each 的动态块

Mat*_*ard 5

通过在动态块中使用 进行迭代for_each = each.value.acl,您可以迭代类型中的值object。看来您确实想迭代它们acl本身。您需要将类型调整为:

variable "storage_share_map" {
  type = map(object({
    quota = number,
    acl = list(object({
      ...
    }))
  })),
}
Run Code Online (Sandbox Code Playgroud)

您可以从错误消息中看出,当前它正在迭代id和 then access_policy,并且未能找到每个属性所请求的两个属性,这就是为什么您有 2*2=4 错误。

您可以相应地调整您的输入:

storage_share_map = { 
  my-share-2 = {
    quota = 123,
    acl = [{
      id = "a-id",
      access_policy = {
        expiry      = "ISO8061 UTC TIME"
        permissions = "rwdl"
        start       = "ISO8601 UTC TIME"
      },
    }],
  }
Run Code Online (Sandbox Code Playgroud)

这将实现您想要的行为。

请注意,Terraform 0.12 有时在嵌套对象类型规范方面存在问题,因此省略aclwith[]可能会导致在某些情况下崩溃。