mazen160 / tfquery

tfquery: Run SQL queries on your Terraform infrastructure. Query resources and analyze its configuration using a SQL-powered framework.

Home Page:https://mazinahmed.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Invalid terraform state error

calvernaz opened this issue Β· comments

Hi πŸ‘‹ , thanks for doing this.

I got this error reporting invalid terraform state file:

 9, in validate_tfstate
    raise ValueError("Invalid tfstate file")
ValueError: Invalid tfstate file

and the terraform state file:

{
    "version": 3,
    "serial": 1,
    "lineage": "<uuid>",
    "backend": {
        "type": "s3",
        "config": {
            "access_key": null,
            "acl": "private",
            "assume_role_policy": null,

my understanding is the error comes from the validation expecting a key named "terraform_version" instead of "version". However every reference after that seems to be looking for "version", e.g:

def validate_tfstate(tfstate):
    if "terraform_version" not in tfstate.keys():
        raise ValueError("Invalid tfstate file")
        return False
    if tfstate["version"] < 3:
        raise ValueError("Unsupported tfstate version")
        return False
    return True

Do you think this could be a version issue and should accept both?

Hi @calvernaz

Thanks your reporting this issue! I was not able to encounter it before in my tests. All of the testing Terraform environments had terraform_version set.

The check for terraform_version is made to assert if the provided file is an actual Terraform state file. The value is not being used anywhere else.

May I ask what is the Terraform version you're using? I wonder why it's not generating a terraform_version key in the state file.

Hi @calvernaz
Will be closing the ticket for now, but please, when you get a chance to review it, feel free to re-open it.

The goal of checking terraform_version is to validate if this Json file is a Terraform state file, if it doesn't have this value, tfquery would assume that it's invalid. version can be find much more commonly, that's why it's really hard to determine if it's a valid tfstate file based on it.