# Create multiple Azure Storage Account with Multiple FileShare under each Storage Account

**URL:** <https://discuss.hashicorp.com/t/create-multiple-azure-storage-account-with-multiple-fileshare-under-each-storage-account/62030>\
**Category:** Azure\
**Created:** [January 24, 2024, 10:14am UTC](https://discuss.hashicorp.com/t/create-multiple-azure-storage-account-with-multiple-fileshare-under-each-storage-account/62030 "2024-01-24T10:14:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronaksheth85](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@ronaksheth85](https://discuss.hashicorp.com/u/ronaksheth85)\
**Post date:** [January 24, 2024, 10:14am UTC](https://discuss.hashicorp.com/t/create-multiple-azure-storage-account-with-multiple-fileshare-under-each-storage-account/62030/1 "2024-01-24T10:14:11Z")

</div>

I am trying to create multiple Azure Storage accounts with specific Azure File shares under each account using the variable type as map:object. I have created the following terraform code but getting the error :

**Code**

```auto
StorageAccount = {
  "dev" = {
    SName = "test"
    SAFileShare = [logs1", "logs3"]
  }
  "prd" = {
    SName = "test1"
    SAFileShare = ["logs3", "logs4"]

  }
}

variable "StorageAccount" {
  type = map(object({
    SName = string
    SAFileShare = list(string)
    SABlob = list(string)
  }))
}

# Storage Account
resource "azurerm_storage_account" "Storage" {
  for_each = var.StorageAccount
  name = each.value["SName"]
  location = var.Location
  resource_group_name = azurerm_resource_group.ResourceGroup.name
  account_tier = var.accountTier
  account_replication_type = var.accountReplicationType
}

# File Share 
resource "azurerm_storage_share" "FileShare" {
  for_each = var.StorageAccount
  name = each.value["SAFileShare"]
  storage_account_name = azurerm_storage_account.Storage[each.key].name
  quota = 50
}

```

**Error**

```auto
Error: Incorrect attribute value type
│
│ on main.tf line 4, in resource "azurerm_storage_share" "FileShare":
│ 4: name = each.value["SAFileShare"]
│ ├────────────────
│ │ each.value["SAFileShare"] is list of string with 2 elements
│
│ Inappropriate value for attribute "name": string required.

```

---

<div class="post-metadata">

**Author:** ![rtwolfe](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/rtwolfe/32/39999_2.png) [@rtwolfe](https://discuss.hashicorp.com/u/rtwolfe)\
**Post date:** [January 25, 2024, 7:01pm UTC](https://discuss.hashicorp.com/t/create-multiple-azure-storage-account-with-multiple-fileshare-under-each-storage-account/62030/2 "2024-01-25T19:01:23Z")

</div>

The error you are encountering is due to the way you are iterating over the `SAFileShare` list within each storage account. The `azurerm_storage_share` resource expects a single string for the `name` attribute, but you are providing a list of strings. To resolve this, you need to modify your `for_each` to iterate over each file share within each storage account.

---

<div class="post-metadata">

**Author:** ![rtwolfe](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/rtwolfe/32/39999_2.png) [@rtwolfe](https://discuss.hashicorp.com/u/rtwolfe)\
**Post date:** [January 25, 2024, 7:02pm UTC](https://discuss.hashicorp.com/t/create-multiple-azure-storage-account-with-multiple-fileshare-under-each-storage-account/62030/3 "2024-01-25T19:02:33Z")

</div>

Here’s a revised version of your Terraform code that should work:

variable “StorageAccount” {  
type = map(object({  
SName = string  
SAFileShare = list(string)  
}))  
}

# Storage Account

resource “azurerm\_storage\_account” “Storage” {  
for\_each = var.StorageAccount  
name = each.value.SName  
location = var.Location  
resource\_group\_name = azurerm\_resource\_group.ResourceGroup.name  
account\_tier = var.accountTier  
account\_replication\_type = var.accountReplicationType  
}

# File Share

resource “azurerm\_storage\_share” “FileShare” {  
for\_each = { for sa\_key, sa\_value in var.StorageAccount : sa\_key =\> sa\_value.SAFileShare… }  
name = each.value  
storage\_account\_name = azurerm\_storage\_account.Storage[each.key].name  
quota = 50  
}

---

<div class="post-metadata">

**Author:** ![decodeWithRam](https://avatars.discourse-cdn.com/v4/letter/d/7cd45c/32.png) [@decodeWithRam](https://discuss.hashicorp.com/u/decodeWithRam)\
**Post date:** [February 2, 2024, 6:28am UTC](https://discuss.hashicorp.com/t/create-multiple-azure-storage-account-with-multiple-fileshare-under-each-storage-account/62030/4 "2024-02-02T06:28:01Z")

</div>

Hii,  
here the foreach expresion is correct and for study purpose I ran the code but on fileshare name attribute its giving error as below:

```auto
on Az_fileShare.tf line 29, in resource "azurerm_storage_share" "FileShare":
│ 29: name = each.value
│ ├────────────────
│ │ each.value is list of string with 2 elements
│
│ Inappropriate value for attribute "name": string required.

```

if I give index as each.value[0] its working but in this case he wants fileshare to be for both strings, so how can he do??

---

<div class="post-metadata">

**Author:** ![decodeWithRam](https://avatars.discourse-cdn.com/v4/letter/d/7cd45c/32.png) [@decodeWithRam](https://discuss.hashicorp.com/u/decodeWithRam)\
**Post date:** [February 3, 2024, 8:26am UTC](https://discuss.hashicorp.com/t/create-multiple-azure-storage-account-with-multiple-fileshare-under-each-storage-account/62030/5 "2024-02-03T08:26:54Z")

</div>

Here’s the solution for your situation:

```auto
resource "azurerm_storage_share" "FileShare" {
  for_each = merge([for sa_key, sa_value in var.StorageAccount: { for val in sa_value["SAFileShare"] : val => sa_key }]...)
  name = each.key
  storage_account_name = azurerm_storage_account.Storage[each.value].name
  quota = 50
}

```

you can use merge function to loop twice in your map of object and do your iteration.  
the result of this merge function is as follow:

```auto
Test = {
      + logs1 = "dev"
      + logs2 = "dev"
      + logs3 = "prd"
      + logs4 = "prd"
    }

```

Thank you…
