我们有用于创建存储帐户的代码:
resource "azurerm_storage_share" "storage_account_shares" {
for_each = var.storage_share
name = each.value["name"]
storage_account_name = replace("${var.resource_name_prefix}${each.value["account_name"]}", "-", "")
quota = each.value["quota"]
enabled_protocol = each.value["protocol"]
}
现在我需要为每个 storage_share 创建诊断设置:
resource "azurerm_monitor_diagnostic_setting" "storagemain_logs" {
for_each = var.storage_share
name = "storagemain-logs"
target_resource_id = azurerm_storage_share.storage_account_shares.id
storage_account_id = azurerm_storage_account.storage_accounts.id
dynamic "log" {
iterator = entry
for_each = local.storages_categories
content {
category = entry.value
enabled = true
retention_policy {
enabled = true
days = 30
}
}
我不明白我应该在“target_resourse_id”中放入什么,以使其与我在 storage_share 资源中的“for_each”块交互工作。我试过这个变体,但没有用:
locals {
storage_accounts_output = {
id = [for i in azurerm_storage_share.storage_account_shares : i.id]
}
}
然后添加
target_resource_id = local.storage_accounts_output.id[index]
您需要使用
each.key
值来引用storage_account_shares
资源的实例,像这样:
target_resource_id = azurerm_storage_share.storage_account_shares[each.key].id
或者,您可以使用 官方文档 中描述的 for_each 链接方法,将代码修改为如下所示:
resource "azurerm_monitor_diagnostic_setting" "storagemain_logs" {
for_each = azurerm_storage_share.storage_account_shares
name = "storagemain-logs"
target_resource_id = each.value.id