Syntax for lists and maps in Terraform JSON

877 views Asked by At

I am looking for the correct syntax in Terraform JSON to write list or maps. There seems to be very few documentation on terraform as JSON (.tf.json)

I am currently getting the following error:

Error loading config: Error loading /var/tmp/base.tf.json: Error reading config for aws_instance[web]: Invalid dot index found: 'var.global.ami'. Values in maps and lists can be referenced using square bracket indexing, like: 'var.mymap["key"]' or 'var.mylist[1]'. in:

${var.global.ami}

For the following code:

{
  "resource": {
    "aws_instance": {
      "web": {
        "ami": "${var.global.ami}",
        "count": 2,

        }
      }
   }
}

However when I tweak my code into

    {
  "resource": {
    "aws_instance": {
      "web": {
        "ami": "${var.global["ami"]}",
        "count": 2,

        }
      }
   }
}

I then get the error:

Error loading config: Error parsing /var/tmp/base.tf.json: 5:33: illegal char

Also does anyone know terraform HCL to terraform JSON convertor?

Thanks

1

There are 1 answers

0
James Thorpe On BEST ANSWER

Nearly there - you just need to escape the quotes within your JSON:

"resource": {
  "aws_instance": {
    "web": {
      "ami": "${var.global[\"ami\"]}",
      "count": 2,
     }
  }
}