terraform-inventory: Error reading tfstate file: 0.12 format error: ; pre-0.12 format error

After destroying our openstack instance with terraform v0.12.10 then terraform-inventory v0.9 crash with following message and our pipeline cannot continue: Error reading tfstate file: 0.12 format error: <nil>; pre-0.12 format error: <nil> (nil error means no content/modules found in the respective format) Of course, we understand terraform.tfstate is empty, but it did not happen with terraform v0.11.14 and terraform-inventory v0.8 Below is terraform.tfstate: { "version": 4, "terraform_version": "0.12.10", "serial": 12, "lineage": "24894aa7-228d-1dc3-0120-42a4be3cdc57", "outputs": {}, "resources": [] }

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 17
  • Comments: 19 (2 by maintainers)

Most upvoted comments

Looks like by default Terraform tries to directly read terraform.tfstate which matches the output of terraform state pull. However, terraform show -json produces a different JSON, where resources are nested under .values.root_module, like the program expects for a 0.12+ file.

I was able to make it work by setting a var:

export TF_STATE=.

By pointing terraform-inventory to a directory instead of a file, it takes a completely different code path that properly utilizes terraform show -json.

I am not sure what the best fix would be here. Maybe terraform-inventory should default to terraform show -json at all times?

I build terraform-inventory from source code

Same error: terraform-inventory --list Error reading tfstate file: 0.12 format error: <nil>; pre-0.12 format error: <nil> (nil error means no content/modules found in the respective format)

Hi again everyone.

I have summarized the format differences below.


Terraform 0.11

A: state pull & terraform.tfstate

{
  "version": 3,
  "terraform_version": "0.11.14",
  "serial": 1,
  "lineage": "3c9951c1-3229-ca05-2cf3-25d8472a9d51",
  "modules": [
	{
	  "path": [
		"root"
	  ],
	  "outputs": {},
	  "resources": {
		"digitalocean_droplet.hanginx.0": {...}
	  }
	}
  ]
}

Terraform 0.12

B: show -json

{
  "format_version": "0.1",
  "terraform_version": "0.12.24",
  "values": {
	"root_module": {
	  "resources": [
		{...}
      ]
    }
  }
}

C: state pull & terraform.tfstate

{
  "version": 4,
  "terraform_version": "0.12.24",
  "serial": 6,
  "lineage": "1239f061-72f1-7e7b-161a-cb4bf72005f1",
  "outputs": {},
  "resources": [
	{...}
  ]
}

The problem right now is that TI tries to use terraform show -json but it can not actually parse that output (!!!)

Edit: the above statement is false. For 0.11 we support state pull or terraform.tfstate (the same format). For 0.12 we only support show -json, but not terraform.tfstate. Yet, we still try to read that file directly, because even if TF_STATE is not set, we default to terraform.tfstate.

I did not dig too deep here so I do not know if we actually have a hard requirement for something that is not present in state pull, but present in show -json or if it was just used as a new feature that could help us identify the version.

Anyway, I suggest to either drop support for 0.11 (0.12 has been out since May 2019 and people on 0.11 can keep using older versions of TI) OR detect version by parsing terraform version, terraform.tfstate, etc.

Having done that, stop calling show -json at all and use state pull that will match the format of terraform.tfstate.

I suggest this in order to avoid having to maintain 3 parsers: one for 0.11, one for 0.12 show -json, one for 0.12 state files.

@adammck we really need your thoughts here.

I’m getting the same error. The TF_STATE=. workaround does not work for me. I tried terraform-inventory 0.9 and HEAD -> master as of Feb 29 (c7e468f33c6f4195cd9d8e97fa0e8d54b5c61c29).

$ terraform-inventory --inventory

Error reading tfstate file: 0.12 format error: <nil>; pre-0.12 format error: <nil> (nil error means no content/modules found in the respective format)

$ TF_STATE=. terraform-inventory --inventory

Error reading Terraform state: 0.12 format error: <nil>; pre-0.12 format error: <nil> (nil error means no content/modules found in the respective format)

$ terraform-inventory --version

terraform-inventory version 0.9

$ terraform --version

Terraform v0.12.21
+ provider.aws v2.51.0
+ provider.random v2.2.1

$ cat terraform.tfstate

{
  "version": 4,
  "terraform_version": "0.12.21",
  "serial": 32,
  "lineage": "72c6e67a-e500-430a-341e-dab290bbf342",
  "outputs": {},
  "resources": []
}