Terragrunt for DRY configurations in Terraform - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
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?"
Practice
Solution
Step 1: Understand Terragrunt's role
Terragrunt is designed to help reuse and share Terraform code, making it easier to manage infrastructure without repeating code.Step 2: Compare options
Options B, C, and D describe incorrect uses or misunderstandings of Terragrunt's purpose.Final Answer:
To reuse Terraform configurations and avoid repeating code -> Option AQuick Check:
Terragrunt = DRY Terraform code reuse [OK]
- Thinking Terragrunt replaces Terraform
- Believing Terragrunt changes Terraform language
- Confusing Terragrunt with application deployment tools
Solution
Step 1: Identify Terragrunt syntax for including configs
Terragrunt uses theincludeblock withpath = find_in_parent_folders()to reuse parent configs.Step 2: Differentiate from Terraform syntax
Options A, C, and D are Terraform syntax: A is a module block, C is backend config, D is a resource block, not Terragrunt includes.Final Answer:
include { path = find_in_parent_folders() } -> Option BQuick Check:
Terragrunt include uses find_in_parent_folders() [OK]
- Using Terraform module syntax instead of Terragrunt include
- Confusing backend config with include
- Writing resource blocks inside Terragrunt files
include {
path = find_in_parent_folders()
}
inputs = {
region = "us-east-1"
env = "prod"
}What will happen when you run
terragrunt apply in this folder?Solution
Step 1: Understand include and inputs usage
Theincludeblock imports parent config. Theinputsblock adds or overrides variables for this folder.Step 2: Predict Terragrunt behavior on apply
Terragrunt merges parent config with local inputs, so region and env variables are set as given.Final Answer:
Terragrunt will apply Terraform code using the parent config and inputs region=us-east-1, env=prod -> Option DQuick Check:
Include + inputs = merged config applied [OK]
- Thinking inputs are ignored with include
- Assuming inputs cause errors
- Believing variables are not passed to Terraform
include {
path = find_in_parent_folders()
}
inputs = {
region = "us-west-2"
env = "dev"
}
terraform {
source = "../modules/app"
}When running
terragrunt apply, you get an error: "Error: Unsupported block type". What is the likely cause?Solution
Step 1: Check Terragrunt config block usage
Terragrunt requires theterraform { source = ... }block at the root level. It cannot be nested inside other blocks likeinputs.Step 2: Identify error cause
The error "Unsupported block type" usually means the block is misplaced or invalid in Terragrunt config.Final Answer:
Theterraformblock cannot be nested inside theinputsblock -> Option AQuick Check:
Misplaced terraform block causes error [OK]
- Placing terraform block inside inputs or include
- Wrong order of blocks causing syntax errors
- Incorrect source path causing unrelated errors
Solution
Step 1: Understand DRY with Terragrunt
Terragrunt allows sharing common config via a root config andincludeblocks in child folders.Step 2: Evaluate options for backend reuse
Create a root Terragrunt config with backend settings and useincludein each environment folder uses root config for backend, avoiding repetition. Copy the backend block into each environment's Terragrunt config repeats code, violating DRY. Define backend only in Terraform modules, not in Terragrunt is incorrect because backend is configured in Terragrunt for remote state. Use different backend types for each environment adds complexity without reuse.Final Answer:
Create a root Terragrunt config with backend settings and useincludein each environment folder -> Option CQuick Check:
Root config + include = DRY backend config [OK]
- Copying backend config to each environment
- Configuring backend only in Terraform modules
- Using different backends unnecessarily
