Referencing objects created from another resource block (storage accounts)

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 }
  location                 = var.resource_group_config[0].region
  name                     = "${}${random_id.randomID.dec}"
  resource_group_name      =[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 }
  #storage_account_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[]
  element(azurerm_storage_account.storage_acct[*].id, 0)
  element(azurerm_storage_account.storage_acct[*], 0)
  element(azurerm_storage_account.storage_acct[name], 0)
Getting a variety of errors - 


│ 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.


│ 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.

Solved my own problem.

Because of how I called hte for-each meta function - using “name” as the index -

for_each                 = { for each in var.storage_account_configs : => each }

You need to use the value of the “name” key as the index.

storage_account_name = azurerm_storage_account.storage_acct["files"].name

This makes sense and also opens up the possibility of checking the value to align the type of storage resource created with the Storage Account configuration.