'terraform nested for each loop in azure storage account

I would want to create multiple storage acounts and inside each of those sotrage accounts some containers. If I would want 3 storage account i would always want to create container-a and container-b in those 3 storage accounts

So for example would be. Storage account list ["sa1","sa2","sa3"].

resource "azurerm_storage_account" "storage_account" {
  count = length(var.list) 
  name = var.name
  resource_group_name = module.storage-account-resource-group.resource_group_name[0]
  location = var.location
  account_tier = var.account_tier
  account_kind = var.account_kind
  

then container block

resource "azurerm_storage_container" "container" {
  depends_on = [azurerm_storage_account.storage_account]
  count =  length(var.containers)
  name                  = var.containers[count.index].name
  container_access_type = var.containers[count.index].access_type
  storage_account_name  = azurerm_storage_account.storage_account[0].name

container variables:

variable "containers" {
  type = list(object({
    name        = string
    access_type = string
  }))
  default     = []
  description = "List of storage account containers."
}

list variable

variable "list" {
  type        = list(string)
  description = "the env to deploy. ['dev','qa','prod']"

This code will create only one container in the first storage account "sa1" but not in the others two "sa2" and "sa3". I read I need to use 2 for each to iterate in both list of storage account and continaers, but not sure how should be the code for it.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source