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)
Looks like by default Terraform tries to directly read
terraform.tfstatewhich matches the output ofterraform state pull. However,terraform show -jsonproduces a different JSON, whereresourcesare 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:
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 -jsonat 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.tfstateTerraform 0.12
B:
show -jsonC:
state pull&terraform.tfstateThe problem right now is that TI tries to useterraform show -jsonbut it can not actually parse that output (!!!)Edit: the above statement is false. For 0.11 we support
state pullorterraform.tfstate(the same format). For 0.12 we only supportshow -json, but notterraform.tfstate. Yet, we still try to read that file directly, because even ifTF_STATEis not set, we default toterraform.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 inshow -jsonor 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 -jsonat all and usestate pullthat will match the format ofterraform.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
$ TF_STATE=. terraform-inventory --inventory
$ terraform-inventory --version
$ terraform --version
$ cat terraform.tfstate