View combined tfvars files output

I know terraform ‘merges’ tfvars files when passed multiple tfvars files, and I understand that in the case of the same variable existing in multiple files, it will use the value from the last file.

Is there a simple process at which, given a number of tfvars files, I can view the ‘combined’ output that terraform is using?

We’re looking at to take a ‘point in time’ capture of the config we are building with, and ideally if terraform can provide this (or it’s a simple merge process I can replicate) then we’d like to capture the merged output of the various files we are providing?

You could use something like this: find ./vars -type f -name "*tfvars*" -exec cat {} \;. It does’t merge the values though.

Below produces the json output of the plan. But the json output is really ugly though.

tfp $(find vars -type f -name "*tfvars*" -exec echo -n "-var-file {} " \;) -out=plan && tf show -json plan | tee plan.json | jq

my aliases:

tf = terraform
tfp = terraform plan

Hi @djsmiley2k,

Terraform does not have any built-in features for doing this, in part because Terraform doesn’t really actually ever construct a “merged .tfvars file” and instead produces an in-memory data structure that contains input variable values from a variety of different sources that might not even be .tfvars files at all.

However, a while ago I did write a separate utility that can wrangle multiple .tfvars files into a single .tfvars file while also simultaneously filtering out any values that are not needed for a particular Terraform module: terraform-filter-vars.

Thinking about your goal of capturing a set of input variables I might try to implement that like this:

  • First run terraform-filter-vars with all of the various input .tfvars files you want to consider, and the module you’re intending to use them with.
  • Save the result of terraform-filter-vars as your capture of what you ran Terraform with.
  • Run terraform plan while passing the new merged .tfvars file rather than the multiple original files, thereby ensuring that you ran Terraform with exactly what you saved, and thus avoid any potential problems if Terraform’s own merging behavior differs somehow from terraform-filter-vars. (I don’t believe it does, but I’ve not checked closely.)

I made this small tool just to solve a problem I had once and so it is not an official HashiCorp project and I’m unlikely to update it any further beyond the current v1.0.0 release that’s published with the GitHub repository. However, if it already does something useful for your purposes then I don’t intend to break it, and if it’s close but not quite what you want then perhaps you could fork it and improve it to better meet your needs.

1 Like