I am trying to create storage resources - 2 Storage accounts, one for Blob, one for Azure Files. I need separate storage accounts because they are Premium.
I’m using object maps and for_each to create the resources. I’ve verified that individually the resource blocks work as expected. I am just having trouble adding the shares/containers to the Storage Accounts. I just can’t sort out the correct syntax for the reference. I did something similar when adding subnets to VNICs but that syntax isn’t working here.
# Create the Storage Account
resource "azurerm_storage_account" "storage_acct" {
#count = length(var.storage_account_configs)
# Use for_each
for_each = { for each in var.storage_account_configs : each.name => each }
location = var.resource_group_config[0].region
name = "${each.value.name}${random_id.randomID.dec}"
resource_group_name = azurerm_resource_group.storage-rg[0].name
account_kind = each.value.acct_kind
account_tier = each.value.account_tier
access_tier = each.value.access_temp
account_replication_type = each.value.replication
}
You can see I am casting a wide net - I feel like I’m close but missing something obvious.
# Create shares using a complex object
resource "azurerm_storage_share" "fileshare" {
#count = length(var.shares)
for_each = { for each in var.shares: each.name => each }
#storage_account_name = azurerm_storage_account.storage_acct.name
#storage_account_name = element(azurerm_storage_account.storage_acct[name], 0)
#storage_account_name = element(azurerm_storage_account.storage_acct[*], 0)
#storage_account_name = element(azurerm_storage_account.storage_acct[*].name, 0)
storage_account_name = values(azurerm_storage_account.storage_acct[0]).name
#storage_account_name = azurerm_storage_account.storage_acct[0].name
#storage_account_name = azurerm_storage_account.storage_acct[]
/*
azurerm_storage_account.storage_acct[each.key]
element(azurerm_storage_account.storage_acct[*].id, 0)
element(azurerm_storage_account.storage_acct[*], 0)
element(azurerm_storage_account.storage_acct[name], 0)
azurerm_storage_account.storage_acct[0].name
azurerm_storage_account.storage_acct.name
*/
Getting a variety of errors -
values(azurerm_storage_account.storage_acct[0]).name
╷
│ Error: Invalid index
│
│ on line 1:
│ (source code not available)
│
│ The given key does not identify an element in this collection value. An object only supports looking up attributes by name, not by numeric index.
╵
element(azurerm_storage_account.storage_acct[name], 0)
╷
│ Error: Invalid reference
│
│ on line 1:
│ (source code not available)
│
│ A reference to a resource type must be followed by at least one attribute access, specifying the resource name.
╵
azurerm_storage_account.storage_acct[each.key]
╷
│ Error: Reference to “each” in context without for_each
│
│ on line 1:
│ (source code not available)
│
│ The “each” object can be used only in “module” or “resource” blocks, and only when the “for_each” argument is set.
╵