Local variables to parse json file

Hi All,

I am trying to use a local variable using jsondecode function to parse a json file. I am not sure about the code which i should use to pass the attributes for the resources.

I am trying to create VMs, Nic, disk etc using the below code, but stuck at the first step while createing nic card.

locals{
json_data = jsondecode(file("main.tfvars.json"))}


resource "azurerm_network_interface" "sn-net-inf" {
 for_each = local.json_data
  name                = format("%s-nic",each.key)
  location            = each.local.value.location
  resource_group_name = each.local.value.resource_group_name
  ip_configuration {
    name      = "IPConfig1"
    subnet_id = each.value.subnet
    private_ip_address            = each.local.value.ip_config
    private_ip_address_allocation = "static"
  }
}

Below is the sample of json…

"VirtualMachines": {

    "ECPDB": {

      "OS": "SUSE Linux 15 SP1",

      "type" : "Linux",

      "resource_group_name": "poc_rg",

      "location": "eastus",

      "virtual_network": "hub-net",

      "subnet":"app-subnet",

      "ip_config":"10.1.1.1",

      "Disk": [

        {

          "Name": "OS Disk",
...

Many thanks in advance.

@janapb Some of the lines you have here aren’t valid:

location = each.local.value.location
resource_group_name = each.local.value.resource_group_name

each only has key and value bits … I’m not sure, so I’ll suggest that maybe you’re trying to do something like:

location = local.json_data[each.key].location

Which would look up the each.key key’d value in your map, and access its location attribute.

@pselle,

Thanks for spotting the error. I have adjusted the code and observer the below error. I noticed the key name only VirtualMachines were picked by the code and i removed it for testing purpose.

resource “azurerm_network_interface” “sn-net-inf” {

for_each = local.json_data

name = format("%s-nic",each.key)

location = local.json_data[each.key].location

resource_group_name = local.json_data[each.key].resource_group_name

ip_configuration {

name      = "IPConfig1"

subnet_id = local.json_data[each.key].subnet.id

private_ip_address            = local.json_data[each.key].ip_config

private_ip_address_allocation = "static"

}

}

Error: Unsupported attribute

on vm_linux.tf line 59, in resource “azurerm_network_interface” “sn-net-inf”:
59: subnet_id = local.json_data[each.key].subnet.id
|----------------
| each.key is “ECPDB”
| local.json_data is object with 1 attribute “ECPDB”

This value does not have any attributes.

Could you please share your thoughts?

My assumption here is that something is up with the parsing of your JSON, or the structure … I’d debug by making some output {} values where you access values within your JSON. Using the lookup function could help if you have defaults you want to fall back to (however, you might want the error, in order to demonstrate when traversing the object has failed).

Hello pselle,

I have fixed the issue finally. The JSON format seems to be correct… I had an issue in the data block of the subsets. It took a while for me to figure out… thanks a lot for your support and advice.

Cheers

1 Like