for_each 中动态块的 Terraform 错误
Terraform error with dynamic block in for_each
我正在尝试使用 azuremrm 资源 storage_share 实例化天蓝色 storage_share 地图。按照设计,我需要能够用同一个块实例化多个存储共享;这些股票中的每一个可能有也可能没有“acl”部分。
我正在考虑使用 for_each 结合动态块来解决这个问题,如相关的 SE 问题:
Main.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
}
}
}
变量定义为:
variable "storage_share_map" {
type = map(object({
quota = number,
acl = object({
id = string,
access_policy = object({
expiry = string,
permissions = string,
start = string
})
}),
}))
default = {}
}
后来在我的测试中参数化为:
storage_share_map = {
my-share-2 = {
quota = 123,
acl = {
id = "a-id",
access_policy = {
expiry = "ISO8061 UTC TIME"
permissions = "rwdl"
start = "ISO8601 UTC TIME"
},
},
}
但是,在测试时,terraform returns 以下输出:
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.
据我了解,这里的问题是动态块中的 for_each 格式不正确或行为不当:acl.value 似乎都被视为字符串“a-id”和携带三个属性(?).
Terraform 版本 0.12.26
Azurerm 版本 2.26.0
如有任何见解,我们将不胜感激。
相关问题:
each.value.acl
请使用方括号。
Azure 存储共享块应如下所示:
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
}
}
}
}
通过使用 for_each = each.value.acl
在动态块中迭代,您将迭代 object
类型中的值。看来您真的想自己迭代 acl
。您需要将类型调整为:
variable "storage_share_map" {
type = map(object({
quota = number,
acl = list(object({
...
}))
})),
}
您可以从错误消息中看出,当前它正在遍历 id
,然后遍历 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"
},
}],
}
这将实现您想要的行为。
请注意,Terraform 0.12 有时会出现嵌套对象类型规范的问题,因此在某些情况下省略 acl
和 []
可能会导致崩溃。
我正在尝试使用 azuremrm 资源 storage_share 实例化天蓝色 storage_share 地图。按照设计,我需要能够用同一个块实例化多个存储共享;这些股票中的每一个可能有也可能没有“acl”部分。
我正在考虑使用 for_each 结合动态块来解决这个问题,如相关的 SE 问题:
Main.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
}
}
}
变量定义为:
variable "storage_share_map" {
type = map(object({
quota = number,
acl = object({
id = string,
access_policy = object({
expiry = string,
permissions = string,
start = string
})
}),
}))
default = {}
}
后来在我的测试中参数化为:
storage_share_map = {
my-share-2 = {
quota = 123,
acl = {
id = "a-id",
access_policy = {
expiry = "ISO8061 UTC TIME"
permissions = "rwdl"
start = "ISO8601 UTC TIME"
},
},
}
但是,在测试时,terraform returns 以下输出:
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.
据我了解,这里的问题是动态块中的 for_each 格式不正确或行为不当:acl.value 似乎都被视为字符串“a-id”和携带三个属性(?).
Terraform 版本 0.12.26 Azurerm 版本 2.26.0
如有任何见解,我们将不胜感激。
相关问题:
each.value.acl
请使用方括号。
Azure 存储共享块应如下所示:
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
}
}
}
}
通过使用 for_each = each.value.acl
在动态块中迭代,您将迭代 object
类型中的值。看来您真的想自己迭代 acl
。您需要将类型调整为:
variable "storage_share_map" {
type = map(object({
quota = number,
acl = list(object({
...
}))
})),
}
您可以从错误消息中看出,当前它正在遍历 id
,然后遍历 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"
},
}],
}
这将实现您想要的行为。
请注意,Terraform 0.12 有时会出现嵌套对象类型规范的问题,因此在某些情况下省略 acl
和 []
可能会导致崩溃。