Tfvars: Layering

Tfvars Structure

Tfvar files should be place within the config/stacks/MOD/tfvars folder. Example:

└── tfvars
    ├── base.tfvars
    ├── dev.tfvars
    └── prod.tfvars

You don’t have to specify the -var-file option, the tfvars files are automatically processed and used via layering.

The tfvar files are processed and “layered”. Example:

TS_ENV=dev  terraspace up demo -y # merges base and dev
TS_ENV=prod terraspace up demo -y # merges base and prod

The tfvars files should generally be within the config/stacks folder.


Terraspace builds tfvars to add layering support. Example:

$ terraspace build demo
$ cd .terraspace-cache/us-west-2/dev/stacks/demo/
$ ls *.tfvars

Layering combines the base layer with the TS_ENV specific layer. Another example:

$ TS_ENV=prod terraspace build demo
$ cd .terraspace-cache/us-west-2/prod/stacks/demo/
$ ls *.tfvars

Terraform automatically loads all files in the directory with any variation of *.auto.tfvars. So these built tfvars files automatically load.

Tip: Seeing Layering Clearly

To see the layers being used, use the option.


Terraspace.configure do |config| = true

This will show found layers, which is what you want to see most of the time. To see all possible layers, see: Debugging Layering Docs.

Other Ways to Set Variables

There are other ways to specify tfvar files:

The variables and layering examples here only cover the tip of the iceburg. Terraspace has rich layering support. It allows you to use the same infrastructure code and create multiple environments in different regions, accounts, providers, etc. More details in: Full Layering.

More tools: