terraform - for_each 中带有动态块的 Terraform 错误
问题描述
我正在尝试使用 azuremrm 资源storage_share实例化一个 azure storage_share 地图。按照设计,我需要能够用同一个块实例化多个存储共享;这些股票中的每一个都可能有也可能没有“acl”部分。
我正在考虑将 for_each 与动态块结合使用来解决此问题,如相关的 SE 问题中所示:
主文件
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 返回以下输出:
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
}
}
}
}
推荐阅读
- php - 如何在数据库中插入多个输入的数组?
- javascript - 将 google 脚本值传递给 javascript datepicker
- python - git-pylint-commit-hook 不检查所有项目文件
- java - .isBlank() 在 Java NetBeans 中无法识别
- php - SQL 查询未捕获的 PDOException 问题
- javafx - 修改 TreeView 的 cellfactory
- python - 使用字典的多级 QTreeView
- android - 首次从 Play 商店安装应用后,FirebaseAuth 验证不起作用
- java - makeToast 无法在另一个类中解决
- bash - 如何使用 sed 和 cat 将多行从一个文件添加到另一个文件