Utility (or code) to parse .tf files and output

I’m looking for a way to take a set of input files and have them parse, replacing variable placeholders with their values, evaluating expressions and replacing them with the result, etc. and have the result output. My intention is to take this output and perform some custom actions against it via code I author. Having the content pre-parsed would be very helpful.

A command line utility, or pointers to an existing library would be very helpful. Thanks!

You don’t mention what language you’re planning on doing this with.

You could either use a generic templating system like jinja or golang’s builtin templating (write files as foo.tf.tmpl or similar, render the templates, then use terraform to validate / fmt the output of that), or look at libraries to parse / write HCL. For Python, there are a couple of libraries (like python-hcl2 and pyhcl that can parse hcl, but none that I know of which can write it. In Golang, of course there is hcl package - github.com/hashicorp/hcl/v2 - Go Packages

1 Like