Dependencies Tfvars Considerations

Below are some things you should consider when using dependencies and tfvars. They may also help you understand how Terraspace dependencies and tfvars work.

Compile Time

Terraspace always evaluates all layered tfvars files at terraspace compile time. This means helpers like output and depends_on will always register the dependencies with Terraspace if they are called. Let’s say you comment out the HCL tfvar line like so:

# vpc_id = <%= output('vpc.vpc_id') %> # notice comment at the start of the line

Even though the HCL is commented out, the ERB with the output method is still being called. So the dependencies still register with Terraspace. You must delete the line or comment out the ERB call with <%# instead of <%= to instead to remove the dependency. Example:

# vpc_id = <%# output('vpc.vpc_id') %> # notice <%# vs <%=

Default Output Format

By default, the output value is formatted as JSON. The reason json is use is discussed in Complex Types.

Dependency Resolution Internals

Here’s a boiled down explanation of how Terraspace dependency resolution works.

Knowing a little bit about how Terraspace internals will you help understand the rest of the considerations on this page.

Dependency Must Be Defined in tfvars

Only tfvars files respect the use of the output and depends_on helpers. When terraspace all runs, it double processes tfvars files with ERB/ruby. The 1st pass figures out the dependency graph. The 2nd pass fetches the output values from the resolved graph. By design, Terraspace avoids the double processing of other files like So you must declare dependencies in tfvars. Related issue #115

OutputProxy Object

The output helper returns an OutputProxy object, not an actual JSON string. This is because the result can ultimately be any type of data, not just a string, so a proxy object is used. The proxy object also allows Terraspace to process things whether or not the statefile is available.

Since the return value from the output helper is a proxy object, this code will not work:

<%= JSON.load(output("stack.list_output")) %>

Instead, you can:

  1. Use HCL and locals to access elements in the data structure.
  2. Use Ruby to access elements before passing to the Input Variables.

Both methods are covered in the Complex Types Docs.

More tools: