Post

How to convert yaml variable file for terraform to native hcl tfvars

How to convert a YAML variable file for Terraform to native HCL tfvars format using yq, jq, and json2hcl.

If there is ever a need to convert a YAML variable file for Terraform to native HCL tfvars format, you can use the following approach.

First, install the json2hcl tool via Homebrew. Then pipe the YAML file through yq to convert it to JSON, use jq to extract the relevant section, and finally pass it through json2hcl to produce HCL output.

1
2
brew install json2hcl
cat data/prod/root.yaml | yq -ojson | jq .azure_secret_engines | json2hcl
This post is licensed under CC BY 4.0 by the author.