Terramate stack hierarchy limits on circular dependency detection
0 reputation · 28 Nov 2021, 12:58 UTC
0 reputation · 28 Nov 2021, 12:58 UTC
Terramate orchestrates Terraform configurations by managing stack hierarchies and generating repetitive code through templates. A critical aspect of this orchestration is the definition of dependencies between stacks to ensure the correct order of operations during deployment.
When defining complex architectures with deeply nested or interconnected stacks, there is a risk of introducing circular dependencies. While Terramate manages the generation phase, the specific behavior of the orchestrator when encountering recursive dependency loops in large-scale hierarchies remains unclear.
terramate generate command provide a deterministic validation check to prevent circular dependencies before file generation?31275 reputation · 28 Nov 2021, 20:27 UTC
The terramate generate command does not perform a standalone, deterministic validation of the entire dependency graph specifically to block file generation. Instead, Terramate focuses on the generation of configuration files based on templates. Circular dependencies typically manifest during the graph resolution phase—specifically when the orchestrator attempts to resolve variable interpolations or determine the deployment order for operations like terramate run terraform plan.
While Terramate manages the hierarchy through terramate.tm.hcl files, the following behaviors are observed in large-scale architectures:
generate or run command, as the tool must evaluate the inheritance chain for every stack.To verify how your specific version of Terramate handles cycles, you can perform a scoped test:
stack-a and stack-b.stack-a/terramate.tm.hcl, define a dependency on stack-b.stack-b/terramate.tm.hcl, define a dependency on stack-a.terramate generate and then attempt a deployment command (e.g., terramate run terraform plan) to observe where the cycle is intercepted.Diagnostic Detail Needed: Are you experiencing these issues during the generate phase (template rendering) or during the run phase (orchestration of Terraform)? The resolution path differs depending on whether the cycle is in the file structure or the execution graph.
Use comments to ask for clarification. Post a solution as an answer.
31,275 reputation · 28 Nov 2021, 16:22 UTC
It is important to distinguish between template generation and execution orchestration when diagnosing these issues. Because terramate generate primarily handles the expansion of templates into static files, it may not trigger a full graph validation. The actual cycle detection occurs when the engine constructs the Directed Acyclic Graph (DAG) to determine the order of operations.
To proactively identify circular dependencies before attempting a deployment, you can use the following approach:
terramate run terraform plan) across the affected stack range. The engine will attempt to resolve the DAG and should return a specific error identifying the cycle path.While there is no documented hard limit on nesting depth, performance typically scales linearly with the number of stacks and their inheritance depth, as the engine must evaluate the merged configuration for every node in the hierarchy.