State Locking Coordination
Terramate does not implement its own state-locking mechanism. It acts as an orchestration layer that generates HCL and invokes Terraform binaries. Consequently, state locking is handled entirely by the configured Terraform backend (e.g., S3 with DynamoDB, GCS, or Terraform Cloud). Terramate does not serialize Terraform calls to prevent overlapping locks unless the stacks are explicitly linked via a dependency graph or executed with a serial flag.
Locking Behavior: Independent vs. Dependent Stacks
The behavior differs based on how the stacks are related and how the command is executed:
- Independent Stacks: If stacks are independent and executed via
terramate run, Terramate may invoke them concurrently. If these stacks share a remote backend and the same state key, they will contend for the same lock. The outcome is determined by the backend's native logic: one process will acquire the lock, and the other will either wait or fail with a "State is already locked" error.
- Dependent Stacks: When stacks reference each other (creating a dependency graph), Terramate ensures sequential execution. A dependent stack will not begin its Terraform invocation until the prerequisite stack has completed. This naturally prevents lock contention between dependent stacks, provided they are executed as part of the same orchestration chain.
Atomicity and Parallelism
Terramate provides no guarantees for atomicity across a chain of dependent stacks. While it ensures the order of execution, it does not wrap multiple stack operations in a single distributed transaction. If a chain of three stacks is running and the second stack fails, the first remains applied, and the third will not execute. There is no automatic rollback mechanism for previously successful stacks in the chain.
Verification and Safety
To ensure stability in complex hierarchies, use the following scoped approaches:
- Force Serialization: Use
terramate run --serial to disable concurrency and ensure every Terraform invocation happens one after another, regardless of the dependency graph.
- Backend Validation: Verify that each stack in your hierarchy uses a unique state key. Sharing a single state file across multiple stacks is a high-risk configuration that leads to frequent lock contention and potential state corruption.
Diagnostic Detail Needed: Are your nested stacks configured to share a single state file (same backend key), or do they use unique keys within a shared backend bucket?