title: Terraform - Reference for_each from a module output
body: <p>I am trying to use Terraform to create multiple storage containers, and then upload multiple blobs to each container.</p>
  <p>I have the part of creating multiple containers working, but can't figure out how to reference the for_each output of each container when uploading the blobs.</p>
  <p><strong>Storage Container Module</strong> (Works)</p>
  <pre><code>resource &quot;azurerm_storage_container&quot; &quot;azure&quot; {
    for_each              = toset(var.storage_containers)
    name                  = each.value
    storage_account_name  = var.storage_account_name
    container_access_type = var.storage_account_container_access_type
  }
  
  output &quot;azurerm_storage_container_name&quot; {
    value = toset(keys(azurerm_storage_container.azure))
  }
  </code></pre>
  <p><strong>Child Module</strong> (Works)</p>
  <pre><code>module &quot;storage_container&quot; {
    source = &quot;C:/TerraformModules/modules/azurerm/azurerm_storage_container&quot;
    storage_account_name = module.storage_account.azurerm_storage_account_name
    storage_containers   = var.STORAGE_CONTAINER_NAMES
    tags                 = var.TAGS
  }
  </code></pre>
  <p><strong>Code to upload blob</strong> (doesn't work for trying to upload into each container)</p>
  <pre><code>**In a variables.tf file**
  variable &quot;STORAGE_CONTAINER_DEFAULT_BLOBS&quot; {
    description = &quot;The default blobs in each storage container&quot;
    type        = list(string)
  }
  
  **In a vars.auto.tfvars file**
  STORAGE_CONTAINER_DEFAULT_BLOBS = [&quot;one&quot;, &quot;two&quot;, &quot;three&quot;]
  
  **In a main.tf file**
  resource &quot;azurerm_storage_blob&quot; &quot;storage_blob&quot; {
    for_each               = toset(var.STORAGE_CONTAINER_DEFAULT_BLOBS)
    name                   = each.value
    storage_account_name   = module.storage_account.azurerm_storage_account_name
    storage_container_name = module.storage_container[each.value].azurerm_storage_container_name
    type                   = &quot;Block&quot;
    source_content         = &quot;blob file&quot;
  }
  
  </code></pre>
  <p>If I were to set the container name in <code>storage_container_name</code>, it works and the container gets each blob. But I'm not able to reference the container from the module.</p>
  <p>I have this error:</p>
  <pre><code>Error: Invalid index
  
    on storage_blobs.tf line 5, in resource &quot;azurerm_storage_blob&quot; &quot;storage_blob&quot;:
     5:   storage_container_name = module.storage_container[each.value].azurerm_storage_container_name
      |----------------
      | each.value is &quot;two&quot;
      | module.storage_container is object with 1 attribute &quot;azurerm_storage_container_name&quot;
  
  The given key does not identify an element in this collection value.
  </code></pre>
  <p>What I need to achieve:</p>
  <pre><code>resource &quot;azurerm_storage_blob&quot; &quot;storage_blob&quot; {
    for_each               = toset(var.STORAGE_CONTAINER_DEFAULT_BLOBS)
    name                   = each.value
    storage_account_name   = module.storage_account.azurerm_storage_account_name
    storage_container_name = # How to reference the storage accounts created with the `storage_container ` module? #
    type                   = &quot;Block&quot;
    source_content         = &quot;blob file&quot;
  }
  </code></pre>