Terraform – Reference for_each from a module output

I am trying to use Terraform to create multiple storage containers, and then upload multiple blobs to each container.

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.

Storage Container Module (Works)

resource "azurerm_storage_container" "azure" { 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 "azurerm_storage_container_name" { value = toset(keys(azurerm_storage_container.azure)) } 

Child Module (Works)

module "storage_container" { source = "C:/TerraformModules/modules/azurerm/azurerm_storage_container" storage_account_name = module.storage_account.azurerm_storage_account_name storage_containers = var.STORAGE_CONTAINER_NAMES tags = var.TAGS } 

Code to upload blob (doesn't work for trying to upload into each container)

variable "STORAGE_CONTAINER_DEFAULT_BLOBS" { description = "The default blobs in each storage container" type = list(string) } STORAGE_CONTAINER_DEFAULT_BLOBS = ["one", "two", "three"] resource "azurerm_storage_blob" "storage_blob" { 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 = "Block" source_content = "blob file" } 

If I were to set the container name in `storage_container_name`, it works and the container gets each blob. But I'm not able to reference the container from the module.

I have this error:

Error: Invalid index on storage_blobs.tf line 5, in resource "azurerm_storage_blob" "storage_blob": 5: storage_container_name = module.storage_container[each.value].azurerm_storage_container_name |---------------- | each.value is "two" | module.storage_container is object with 1 attribute "azurerm_storage_container_name" The given key does not identify an element in this collection value. 
submitted by /u/philthynz
[link] [comments]

Read more here: https://www.reddit.com/r/Terraform/comments/mmk42q/terraform_reference_for_each_from_a_module_output/

Content Attribution

This content was originally published by /u/philthynz at Terraform, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: