I have a use case where we are planning to store our terraform files into HCL JSON format.
Terraform understands both of these formats equally, so it should not typically be necessary to convert between them.
Terraform supports both formats by using a separate parser for each rather than by converting from one to the other, so there is no built in way to convert the files on disk.
@apparentlymart which parser terraform is using internally to convert ?
Terraform does not convert. It just directly supports both formats, without converting one into the other.
This seems pretty unusual. Can you elaborate?
.tf.json format is really intended only for programs which need to dynamically generate input to Terraform, and cannot easily serialize HCL.
.tf.json format is hard for humans to read - it is not intended that they ever need to.
.tf.json format actually loses some information which is provided in regular
.tf HCL. The lost information can be reconstructed by Terraform, but only using built-in knowledge of the core parts of the Terraform language, and by referring to information exposed by each provider, about the schema of configuration blocks specific to a provider.
I cannot think of any case where it is appropriate to convert existing
.tf.json. It should only be generated by custom tools dynamically building input to Terraform which was never expressed as a
.tf file at all.
@maxb I am doing automations for my use case. It is easier for me to use any programming language to manipulate JSON file.
Let me explain through example:
Let say I have a .tf file which has hundreds of resources. If I need to modify or add new resource or new fields at some point, it is hard to iterate through default .tf file.
If I have any JSON file It will be very easier for me to do those tasks.
There are online tools available that can convert Terraform files to JSON format. You can find these by searching for “Terraform to JSON converter” in your preferred search engine.
Please make sure to use reputable and trusted tools or websites for this purpose.