Tfvars: Dynamic

The tfvars files get processed by ERB templating, providing some dynamic control.

Example 1: output

A useful helper is output, it allows you to grab the output value from another stack and use it as an input variable for another stack.


vpc_id = <%= output("vpc.vpc_id")" %>

Terraspace also uses this information to build the dependency graph and deploys the dependent stacks in the correct order with terraspace all up. Related useful docs:

Example 2: General

One example is maybe you want to use TS_ENV in your variables dynamically with the ERB.


name = "<%= Terraspace.env %>-instance"


TS_ENV=dev terraspace build demo

Results in:

name = "dev-instance"


TS_ENV=prod terraspace build demo

Results in:

name = "prod-instance"

Example 3: Instance Option

The terraspace CLI options are also available. Here’s an interesting use of the options with the --instance option.


name = "<%= options[:instance] %>-server"


terraspace up server --instance bob

Results in:

name = "bob-server"


terraspace up server --instance kevin

Results in:

name = "kevin-server"


Using ERB in the tfvar files is a pretty clean way of adding a little bit of dynamism. A good thing about the approach is that it keeps your Terraform module code purely native. When possible, it is recommended to use the ERB power that terraspace provides responsibly though.