Terraform content parser to extract variables before plan

Is there any library using which we can extract information about variables used in terraform scripts?

I see “terraform-config-inspect” as a utility but it gives only from that folder which is used in that but does not go into child modules. We can do that recursively but looking for a readymade library? or any other approach to get all variables used in terraform script that I can get before running the plan?

I can also write an antlr4 parser to do that by simply reading the variables block in all files under that folder but in that, I won’t know unused files/modules which are present inside the folder.

If you want to know about variables you can input into a Terraform execution, then the ones in the top level folder are the only ones.

All variables in child modules, are solely provided by arguments in those module blocks.

Thanks @maxb for the response,

Does terraform-config-inspect have any limitations or restrictions in fetching variables for different terraform scripts, folders, or files? or any terraform versions
or it will work with all terraform versions and scripts

That question is so broad, I don’t have an answer to it.

No issues Thanks @maxb

Another query :slight_smile: Does terraform config inspect perform validation also like the “terraform validate” command or do we need to do validation first and then run “terraform-config-inspect explicitly”?

terraform-config-inspect is only capable of shallow syntax analysis.

It does not include any expression evaluation logic, and so it cannot implement anything equivalent to terraform validate. If terraform-config-inspect returns errors during loading then they will be only syntax errors, and not semantic errors.

Thanks @apparentlymart