Monorepo vs multi-repo for Terraform - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to manage infrastructure changes grows when using one big repository versus many smaller ones in Terraform.
How does the number of repositories affect the work Terraform does?
Analyze the time complexity of applying Terraform configurations in two setups.
# Monorepo example
module "network" {
source = "./modules/network"
}
module "compute" {
source = "./modules/compute"
}
# Multi-repo example
# Separate repos for network and compute modules
This shows a single repo with multiple modules versus separate repos each with their own module.
Look at what Terraform does repeatedly when applying changes.
- Primary operation: Terraform plan and apply commands for each repo or module.
- How many times: Once per repo in multi-repo, once for all modules in monorepo.
As the number of modules or repos grows, the work changes differently.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | ~10 applies in multi-repo, 1 apply with 10 modules in monorepo |
| 100 | ~100 applies in multi-repo, 1 apply with 100 modules in monorepo |
| 1000 | ~1000 applies in multi-repo, 1 apply with 1000 modules in monorepo |
Multi-repo grows linearly with number of repos; monorepo runs once regardless of module count.
Time Complexity: O(n)
This means the time grows directly with the number of repositories in multi-repo, but stays mostly constant in monorepo.
[X] Wrong: "More repos always mean faster Terraform runs because each is smaller."
[OK] Correct: Running Terraform separately for many repos adds overhead each time, making total time grow with repo count.
Understanding how repo structure affects Terraform run time helps you design infrastructure code that scales well and stays manageable.
"What if we split a monorepo into fewer but larger repos? How would the time complexity change?"
Practice
monorepo for Terraform code?Solution
Step 1: Understand monorepo concept
A monorepo stores all Terraform code in a single repository, allowing easy sharing and reuse.Step 2: Compare options
All Terraform code is stored in one place, making sharing easier correctly describes this advantage. Other options describe multi-repo or unrelated features.Final Answer:
All Terraform code is stored in one place, making sharing easier -> Option AQuick Check:
Monorepo = single repo for all code [OK]
- Confusing monorepo with multi-repo
- Thinking monorepo isolates teams completely
- Assuming monorepo enforces different Terraform versions
multi-repo setup?Solution
Step 1: Understand multi-repo structure
Multi-repo means splitting Terraform code into multiple repositories, often by environment or team.Step 2: Evaluate options
Split Terraform code into separate repositories per environment or team correctly describes this. Store all Terraform modules and environments in one repository describes monorepo. Use a single Terraform state file for all repositories is incorrect because state files are usually separate per repo. Combine Terraform and application code in the same repository mixes concerns.Final Answer:
Split Terraform code into separate repositories per environment or team -> Option BQuick Check:
Multi-repo = multiple repos for Terraform code [OK]
- Mixing monorepo and multi-repo definitions
- Assuming one state file for all repos
- Combining unrelated code in Terraform repos
terraform apply in the root directory without specifying a workspace or path?Solution
Step 1: Understand default Terraform behavior in monorepo
Runningterraform applyin root without workspace or path targets the default state and configuration.Step 2: Analyze options
Terraform applies changes only to the default environment is correct because Terraform applies only the default environment unless configured otherwise. Terraform applies changes to both environments at once is incorrect as Terraform does not apply multiple environments simultaneously by default. Terraform throws an error due to multiple state files is wrong; no error occurs unless misconfigured. Terraform automatically detects and applies changes per environment is false; Terraform does not auto-detect environments.Final Answer:
Terraform applies changes only to the default environment -> Option AQuick Check:
Default apply targets one environment [OK]
- Assuming Terraform applies all environments automatically
- Expecting errors without misconfiguration
- Thinking Terraform auto-detects multiple environments
terraform apply in the wrong repository. What is the best way to fix this mistake?Solution
Step 1: Identify the impact of wrong apply
Runningterraform applyin the wrong repo creates resources unintentionally.Step 2: Choose corrective action
Runterraform destroyin that repository to remove unintended resources is best: runningterraform destroyremoves those resources safely. Delete the entire repository and start over is extreme and unnecessary. Ignore it; Terraform will not create any resources without approval is wrong; apply creates resources after approval. Merge the repositories to avoid confusion is unrelated to fixing the mistake.Final Answer:
Runterraform destroyin that repository to remove unintended resources -> Option CQuick Check:
Destroy removes unintended resources safely [OK]
- Deleting repos instead of destroying resources
- Ignoring accidental resource creation
- Thinking merging repos fixes mistakes
Solution
Step 1: Analyze sharing and isolation needs
The team wants easy sharing of modules but isolated environment configs.Step 2: Evaluate options for balance
Use a multi-repo with one repo for shared modules and separate repos per environment fits best: shared modules in one repo, environments isolated in separate repos. Use a monorepo for all code including modules and environments mixes all code, reducing isolation. Use a single repo with all environments but duplicate modules in each folder duplicates modules, causing maintenance issues. Use multi-repo but copy modules into each environment repo manually copies modules manually, risking drift.Final Answer:
Use a multi-repo with one repo for shared modules and separate repos per environment -> Option DQuick Check:
Shared modules repo + separate env repos = best balance [OK]
- Mixing modules and environments in one repo
- Duplicating modules causing maintenance headaches
- Copying modules manually risking drift
