Tfvars: Layering
Tfvars Structure
Tfvar files should be place within the app/stacks/MOD/tfvars
folder. Example:
app/stacks/demo
├── main.tf
├── tfvars
│ ├── base.tfvars
│ ├── dev.tfvars
│ └── prod.tfvars
└── variables.tf
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 app/stacks
folder, as stacks can include business-specific logic.
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:
- Specify the terraspace
--var-files
option on-the-fly. - Specify var files with CLI Args.
- Create a
config/terraform/terraform.tfvars
file that will gets built. This is one way to set a global tfvars file. For more control over project-wide tfvars see: Project-Level Layering - Specify tfvar files in the top-level
seed
folder. Covered more in Additional Lookups. - Use the Terraform native
TF_VAR_name
env variables. Covered here: Terraform Env Vars
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.