1

I am trying to create multiple Azure Storage Accounts with set of containers counts, however I couldn't achieve due to multiple resources blocks. As of the below code that I wrote which creates multiple containers in the one storage account. How can I achieve same to create multiple containers in different storage accounts ?

resource "azurerm_resource_group" "rg" {
  name     = "test-rg"
  location = "eastus"
}


variable "storageaccountname" {
  type = string
  default = "storageaccount1"
}

 variable "containers_list" {
  type = list
  default = [{ name = "sa1container1", access_type = "private" }, {name = "sa1container2", access_type = "private" },{name = "sa1container3", access_type = "private" }]
 }


resource "azurerm_storage_account" "storageaccount" {
  name                     = var.storageaccountname
  resource_group_name      = azurerm_resource_group.rg.name
  location                 = azurerm_resource_group.rg.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

resource "azurerm_storage_container" "container" {
  count                 = length(var.containers_list)
  name                  = var.containers_list[count.index].name
  storage_account_name  = azurerm_storage_account.storageaccount.name
  container_access_type = var.containers_list[count.index].access_type
}
Uday Kiran
  • 487
  • 2
  • 9
  • 29

1 Answers1

2

I tried to reproduce the scenario in my environment :

Create list of names for storage and container .

Variables.tf

variable "containers_list" {
  type = list
  default = [{ name = "sa1container1", access_type = "private" }, {name = "sa1container2", access_type = "private" },{name = "sa1container3", access_type = "private" }]
 }

 variable "Storage_list" {
  type = list
  default = ["sa1stor1", "sa1stor2","sa1stor3"]
 }

Main.tf

Create storage account with each name listed in variable

resource "azurerm_storage_account" "storage_account" {
 // count                 = length(var.Storage_list)
  //name                  = var.Storage_list[count.index].name
  for_each = toset(var.Storage_list) 
  name=each.value
  resource_group_name = data.azurerm_resource_group.example.name
  location = data.azurerm_resource_group.example.location
  account_tier = "Standard"
  account_replication_type = "LRS"
}

Create container for each value of storage account according to the requirement.

resource "azurerm_storage_container" "container" {
  for_each              = {for idx, val in local.flat_list: idx => val}
  name                  = each.value[1].name
  container_access_type = each.value[1].access_type
  storage_account_name  = azurerm_storage_account.storage_account[each.value[0]].name
}

Create locals to setproduct for double loop

locals {
    flat_list = setproduct(var.Storage_list, var.containers_list)
}

Create container for each value of storage account according to the requirement.

resource "azurerm_storage_container" "container" {
  for_each              = {for idx, val in local.flat_list: idx => val}
  name                  = each.value[1].name
  container_access_type = each.value[1].access_type
  storage_account_name  = azurerm_storage_account.storage_account.each.value[0].name
}

Executed code:

enter image description here

Storage accounts created according to number of variables.

enter image description here

3 Containers as per variables set are created for each storage account.

enter image description here

Reference : terraform-nested-for-each-loop-in-azure-storage-account | SO

kavyaS
  • 8,026
  • 1
  • 7
  • 19