We have this code for Storage Accounts creation:
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"]
}
Now I need to create diagnostic settings for each 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
}
}
I don't understand what I should put in "target_resourse_id" to have it interactively work with "for_each" block that I have in storage_share resource. I tried this variant, but it didn't work:
locals {
storage_accounts_output = {
id = [for i in azurerm_storage_share.storage_account_shares : i.id]
}
}
and then added target_resource_id = local.storage_accounts_output.id[index]
You need to use the each.key
value to refer to instances of storage_account_shares
resources, like this:
target_resource_id = azurerm_storage_share.storage_account_shares[each.key].id
Alternatively, you could use the for_each chaining method described in the official documentation by modifying your code to look like this:
resource "azurerm_monitor_diagnostic_setting" "storagemain_logs" {
for_each = azurerm_storage_share.storage_account_shares
name = "storagemain-logs"
target_resource_id = each.value.id