philthy
philthy

Reputation: 978

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)

**In a variables.tf file**
variable "STORAGE_CONTAINER_DEFAULT_BLOBS" {
  description = "The default blobs in each storage container"
  type        = list(string)
}

**In a vars.auto.tfvars file**
STORAGE_CONTAINER_DEFAULT_BLOBS = ["one", "two", "three"]

**In a main.tf file**
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.

What I need to achieve:

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 = # How to reference the storage accounts created with the `storage_container ` module? #
  type                   = "Block"
  source_content         = "blob file"
}

Upvotes: 2

Views: 3516

Answers (1)

Marcin
Marcin

Reputation: 238081

storage_container_name takes only one value, not a list of values. So if you have 3 STORAGE_CONTAINER_DEFAULT_BLOBS and n number of var.storage_containers you have to iterate n*3 times in azurerm_storage_blob.

Thus, you can try the following with setproduct:

resource "azurerm_storage_blob" "storage_blob" {

  for_each               = {for idx, val in setproduct(module.storage_container.azurerm_storage_container_name, var.STORAGE_CONTAINER_DEFAULT_BLOBS): idx=>val}

  name                   = each.value[1]
  storage_account_name   = module.storage_account.azurerm_storage_account_name
  storage_container_name = each.value[0]
  type                   = "Block"
  source_content         = "blob file"
}

Upvotes: 1

Related Questions