Using json as input to create multiple servers

I am trying to use json as input in order to create multiple servers with all values as input in the json file.
“server_name”: “abctest01”,
“zone”: “us-west2-a”,
“data_disk_03”: “10”,
“data_disk_04”: “20”,
“disk_number”: “3”,
“os_disk”: “10”,
“instance_type”: “windows-2016”,
“location”: “us-west2”,
“machine_type”: “n1-standard-1”,
“ip_address”: “10.10.10.5”,
“backup”: “10.10.10.105”,
“data_disk_02”: “10”,
“data_disk_01”: “20”,
“ha_enabled”: “no”
.
I am using another module and using jsondecode to try to get these values as variables which I can then use.
I have tried using it in the below ways and its still throwing an error stating that value does not have attribute.
locals {
json_data = jsondecode(file(var.json_input_file_name))
}

server_name = local.json_data.server_name /
server_name= jsondecode(file(var.json_input_file_name)).server_name /
server_name = jsondecode(file(var.json_input_file_name)).server_name[0]

error:
4: server_name = jsondecode(file(var.json_input_file_name)).server_name[0]
|----------------
| var.json_input_file_name is “./development/gcp_server_input_test_disks.json”

This value does not have any attributes.

Any help to make it work is highly appreciated.

Hi @tejz1386,

This error suggests that the JSON syntax in your file describes something other than a JSON object, and therefore an attribute access like .server_name is invalid for that value.

To help understand how Terraform is interpreting your file, you could try viewing the result of that jsondecode operation by running terraform console and then entering the expression:

jsondecode(file("./development/gcp_server_input_test_disks.json"))

Since you said your JSON file contains data about multiple servers, perhaps what you have in your file is a JSON array rather than a JSON object, and therefore you’ll need to use one of Terraform’s repetition features to declare something for each element of that array. If you can share the output from terraform console for the expression above I may be able to give some more specific advice.

(When you share code snippets or Terraform output in this forum, please make sure to mark it as “Preformatted Text” using the <> icon in the editor toolbar, or else the forum will not preserve indentation and other formatting and therefore make it hard to read what you are sharing.)

Thanks for your reply, below is the error I am receiving while trying to access the values. I have tried different ways to read the value from the json file to the variable but failed so far.

Error: Unsupported attribute

  on modules/json_output/main.tf line 5, in locals:
   5:   a_server_name = local.json_data["servers"].server_name
    |----------------
    | local.json_data["servers"] is tuple with 1 element

This value does not have any attributes.


Error: Unsupported attribute

  on modules/json_output/main.tf line 6, in locals:
   6:   a_zone = local.json_data["servers"].zone
    |----------------
    | local.json_data["servers"] is tuple with 1 element

This value does not have any attributes.


Error: Unsupported attribute

  on modules/json_output/main.tf line 7, in locals:
   7:   a_data_disk_01 = local.json_data.servers.data_disk_01
    |----------------
    | local.json_data.servers is tuple with 1 element

This value does not have any attributes.


Error: Unsupported attribute

  on modules/json_output/main.tf line 8, in locals:
   8:   a_data_disk_02 = local.json_data.servers.data_disk_02
    |----------------
    | local.json_data.servers is tuple with 1 element

This value does not have any attributes.


Error: Unsupported attribute

  on modules/json_output/main.tf line 9, in locals:
   9:   a_data_disk_03 = local.json_data.servers.data_disk_03
    |----------------
    | local.json_data.servers is tuple with 1 element

This value does not have any attributes.


Error: Unsupported attribute

  on modules/json_output/main.tf line 10, in locals:
  10:   a_data_disk_04 = local.json_data.servers.data_disk_04
    |----------------
    | local.json_data.servers is tuple with 1 element

This value does not have any attributes.


Error: Unsupported attribute

  on modules/json_output/main.tf line 11, in locals:
  11:   a_os_disk      = local.json_data.servers.os_disk
    |----------------
    | local.json_data.servers is tuple with 1 element

This value does not have any attributes.


Error: Unsupported attribute

  on modules/json_output/main.tf line 12, in locals:
  12:   a_instance_type = local.json_data.servers.instance_type
    |----------------
    | local.json_data.servers is tuple with 1 element

This value does not have any attributes.


Error: Unsupported attribute

  on modules/json_output/main.tf line 13, in locals:
  13:   a_location = local.json_data.servers.location
    |----------------
    | local.json_data.servers is tuple with 1 element

This value does not have any attributes.

HI @apparentlymart
Were you able to have a look at the error ?