Hi Team, we have a requirement where we have to automate tfvars file creation . could any one suggest the best possible way to do the same or any one implemented this in there project.
If you are trying to write a
.tfvars file, and find this difficult because your programming language does not have a convenient HCL writer library, then you should take advantage of Terraform also supporting
.tfvars.json files, fulfilling the same purpose, but being written in JSON rather than HCL.
I would echo the recommendation to use
The JSON form of a “tfvars” file is actually not really a special format at all but rather just a plain JSON document where:
- The top-level value is a JSON object.
- All of the properties of that top-level JSON object have names that match variables declared in the root module.
- The JSON values you assign to each top-level property are convertible to match the type constraints declared for each of the variables.
All standard JSON-generating libraries should hopefully be able to produce a document in this format. Then you can just write that result to a file with the
.tfvars.json suffix and Terraform will use the suffix to detect that you intend that file to be interpreted as JSON rather than as the normal syntax.