# Using nested .tfvars.json input in TF code

**URL:** <https://discuss.hashicorp.com/t/using-nested-tfvars-json-input-in-tf-code/74149>\
**Category:** Terraform\
**Tags:** azure\
**Created:** [March 31, 2025, 12:11pm UTC](https://discuss.hashicorp.com/t/using-nested-tfvars-json-input-in-tf-code/74149 "2025-03-31T12:11:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![phoenix-rosiest-4m](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/phoenix-rosiest-4m/32/43453_2.png) [@phoenix-rosiest-4m](https://discuss.hashicorp.com/u/phoenix-rosiest-4m)\
**Post date:** [March 31, 2025, 12:11pm UTC](https://discuss.hashicorp.com/t/using-nested-tfvars-json-input-in-tf-code/74149/1 "2025-03-31T12:11:07Z")

</div>

Hello everyone,

I’m an absolute beginner when it comes to Terraform and I’m trying to understand why I’m running into the following issue. Here’s the thing: I want to create one or more Azure File Shares within an existing Storage Account.

I have created a `fileshares.tfvars.json` file in the following format:

```auto
{
  "storage_accounts": {
    "sa_A": {
      "fileshare_A": {
        "sys_id": "SI1",
        "quota": 100
      },
      "fileshare_B": {
        "sys_id": "SI2",
        "quota": 250
      }
    },
    "sa_B": {
      "fileshare_C": {
        "sys_id": "SI3",
        "quota": 100
      }
    }
  }
}

```

I’ve declared my variable in `variables.tf`

```auto
variable "storage_accounts" {
  description = "Storage Accounts"
  type = any
}

```

The Storage Account itself has already been created so I’ve flattened the file share properties as follows:

```auto
locals {
  fileshares = flatten([
    for storage_account, shares in var.storage_accounts : [
      for share_name, share_props in shares : {
        storage_account_name = storage_account
        share_name = share_name
        sys_id = share_props.sys_id
        quota = share_props.quota
      }
    ]
  ])
}

```

And the resource code in `storage.tf` looks like this:

```auto
resource "azurerm_storage_share" "afstransac" {
  for_each = {
    for share in local.fileshares : "${share.storage_account_name}.${share.share_name}.${share.sys_id}.${share.quota}" => share
  }
 
  name = each.value.share_name
  storage_account_name = each.value.storage_account_name
  enabled_protocol = "NFS"
  quota = each.value.quota
}

```

However when I run the plan, it comes back with the following error and I have no idea what’s going wrong:

```auto
54│ Error: No value for required variable
55│
56│ on variables.tf line 31:
57│ 31: variable "storage_accounts" {
58│
59│ The root module input variable "storage_accounts" is not set, and has no
60│ default value. Use a -var or -var-file command line argument to provide a
61│ value for this variable.
62╵
63Operation failed: failed running terraform plan (exit 1)
64
65─────────────────────────────────────────────────────────────────────────────
66Note: You didn't use the -out option to save this plan, so Terraform can't
67guarantee to take exactly these actions if you run "terraform apply" now.
68Error: Process completed with exit code 1.

```

My question is if anyone can help me with this to see what I’m doing wrong and I’m sure my code can be optimized as well…

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![jbardin](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jbardin/32/3325_2.png) [@jbardin](https://discuss.hashicorp.com/u/jbardin)\
**Post date:** [March 31, 2025, 1:14pm UTC](https://discuss.hashicorp.com/t/using-nested-tfvars-json-input-in-tf-code/74149/2 "2025-03-31T13:14:06Z")

</div>

Hi @phoenix-rosiest-4m

What is the command you used? Did you add the flag suggested in the error output, namely `-var-file fileshares.tfvars.json`?

---

<div class="post-metadata">

**Author:** ![phoenix-rosiest-4m](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/phoenix-rosiest-4m/32/43453_2.png) [@phoenix-rosiest-4m](https://discuss.hashicorp.com/u/phoenix-rosiest-4m)\
**Post date:** [March 31, 2025, 2:42pm UTC](https://discuss.hashicorp.com/t/using-nested-tfvars-json-input-in-tf-code/74149/3 "2025-03-31T14:42:40Z")

</div>

@jbardin thanks for pointing me into the right direction. I should’ve just named the file `fileshares.auto.tfvars.json` and then the variables are added directly (using TFE).

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/hashicorp/original/1X/79bf76adfd92a75631996933098f9ce29d37abf3.svg) [@system](https://discuss.hashicorp.com/u/system)\
**Post date:** [September 27, 2025, 2:43pm UTC](https://discuss.hashicorp.com/t/using-nested-tfvars-json-input-in-tf-code/74149/4 "2025-09-27T14:43:33Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
