Terraform: AWS Instance Data Resource failing via JSON syntax

Created on 24 Mar 2017  ยท  5Comments  ยท  Source: hashicorp/terraform

It works great if I specify via the HCL syntax, but via JSON it fails. I'm using the same format that I've used for multiple other data resources.

Terraform Version

Terraform v0.9.1

Affected Resource(s)

  • data.aws_instance

Terraform Configuration Files

{
  "data": {
    "aws_instance": {
      "primary_server": {
        "filter": {
          "name":   "tag:Group",
          "values": ["${var.name}"]
        },

        "filter": {
          "name":   "instance-state-code",
          "values": [
            "16",
            "0"
          ]
        }
      }
    }
  }
}

the equivalent HCL that works is:

data "aws_instance" "primary_server" {
  filter {
    name   = "tag:Group"
    values = ["${var.name}"]
  }

  filter {
    name   = "instance-state-code"
    values = ["16", "0"]
  }
}

Panic Output

Error loading modules: module api_server: Error loading .terraform/modules/914728d2d5bd97c9a4372840157d825d/clusters.tf.json: position -: 'data' must be followed by exactly two strings: a type and a name
Failed to load root config module: Error loading modules: module api_server: Error loading .terraform/modules/914728d2d5bd97c9a4372840157d825d/clusters.tf.json: position -: 'data' must be followed by exactly two strings: a type and a name

bug core

Most helpful comment

@apparentlymart Hello, the wrap example does not work in the current version, any ideas? Thank you.

Error: Error loading go/src/github.com/hashicorp/terraform/iam.tf.json: -: "data" must be followed by a name

All 5 comments

Hi @thegranddesign! Sorry for the errors here.

The way we map JSON to a HCL syntax tree is currently a little opaque and inconsistent. We're planning to work on that in a forthcoming Terraform version, but for now I fiddled with your example until I could make it work:

{
  "data": [{
    "aws_instance": [{
      "primary_server": {
        "filter": {
          "name":   "tag:Group",
          "values": ["${var.name}"]
        },

        "filter": {
          "name":   "instance-state-code",
          "values": [
            "16",
            "0"
          ]
        }
      }
    }]
  }]
}

Basically what I did here was wrap the first two levels in a JSON array, which is what HCL would normally do automatically in that scenario. I'm not sure yet why it's not working in this particular scenario, but hopefully that is a suitable workaround until we can make things work in a more predictable way here.

I'm going to close this issue for now not because this isn't an issue but because there will be a more general issue in the not-too-distant future that will encompass this and many other weird idiosyncrasies of the JSON parsing. Thanks for taking the time to open this issue!

/cc @jbardin since this might be a good test case for forthcoming JSON work

@apparentlymart I forgot to ping you back and say thank you very much for looking at this and giving me a solution. <3

@apparentlymart Hello, the wrap example does not work in the current version, any ideas? Thank you.

Error: Error loading go/src/github.com/hashicorp/terraform/iam.tf.json: -: "data" must be followed by a name

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jrnt30 picture jrnt30  ยท  3Comments

pawelsawicz picture pawelsawicz  ยท  3Comments

ketzacoatl picture ketzacoatl  ยท  3Comments

larstobi picture larstobi  ยท  3Comments

rkulagowski picture rkulagowski  ยท  3Comments