Tfvars: Layering

Tfvars Structure

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

config/stacks/demo
└── 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.

Examples

Terraspace builds tfvars to add layering support. Example:

$ terraspace build demo
$ cd .terraspace-cache/us-west-2/dev/stacks/demo/
$ ls *.tfvars
1-base.auto.tfvars  2-dev.auto.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
1-base.auto.tfvars  2-prod.auto.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 config.layering.show option.

config/app.rb

Terraspace.configure do |config|
  config.layering.show = true
end

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: