Terragrunt for DRY configurations in Terraform - Time & Space Complexity
We want to understand how using Terragrunt affects the time it takes to apply infrastructure changes.
Specifically, how does the number of repeated operations grow when we reuse configurations with Terragrunt?
Analyze the time complexity of applying multiple Terraform modules with Terragrunt.
terraform {
source = "../modules/app"
}
inputs = {
environment = "prod"
region = "us-east-1"
}
This Terragrunt configuration reuses the same Terraform module with different inputs to avoid repeating code.
Look at what happens when we apply this setup for many environments.
- Primary operation: Terraform module apply for each environment.
- How many times: Once per environment, even though code is reused.
Each environment requires a separate apply operation, so the total work grows with the number of environments.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 applies |
| 100 | 100 applies |
| 1000 | 1000 applies |
Pattern observation: The number of operations grows directly with the number of environments.
Time Complexity: O(n)
This means the time to apply infrastructure grows in a straight line as you add more environments.
[X] Wrong: "Using Terragrunt means applying once covers all environments."
[OK] Correct: Each environment still needs its own apply, even if the code is shared. Terragrunt helps avoid repeating code, not repeated work.
Understanding how Terragrunt manages repeated infrastructure helps you explain efficient code reuse and its impact on deployment time.
"What if we combined all environments into one big Terraform apply? How would the time complexity change?"