Provider caching and mirrors in Terraform - Commands & Configuration
provider_installation {
filesystem_mirror {
path = "/usr/local/share/terraform/providers"
include = ["registry.terraform.io/hashicorp/*"]
}
direct {
exclude = ["registry.terraform.io/hashicorp/*"]
}
}
This configuration file tells Terraform to look for providers first in a local folder /usr/local/share/terraform/providers for all HashiCorp providers. If not found there, it will try to download directly from the official registry.
provider_installation: Main block to configure provider sources.
filesystem_mirror: Defines a local folder mirror for providers matching the include pattern.
direct: Fallback to download providers directly from the registry if not found in the mirror.
terraform init
ls /usr/local/share/terraform/providers
terraform providers
If you remember nothing else from this pattern, remember: configuring provider caching and mirrors lets Terraform reuse providers locally to save time and improve reliability.
Practice
Solution
Step 1: Understand provider caching concept
Provider caching means saving provider plugins on your local machine so Terraform doesn't need to download them every time.Step 2: Identify the benefit of caching
This local storage speeds up Terraform runs by avoiding repeated downloads.Final Answer:
To store provider plugins locally and speed up Terraform runs -> Option BQuick Check:
Provider caching = local storage for speed [OK]
- Thinking caching deletes providers
- Confusing caching with encryption
- Assuming caching forces downloads
Solution
Step 1: Recall Terraform provider mirror blocks
Terraform usesfilesystem_mirrorblocks to define local directories for caching providers.Step 2: Confirm correct block name
Thedirectblock is for direct downloads, not caching. Other options are invalid.Final Answer:
filesystem_mirror-> Option CQuick Check:
Local cache directory = filesystem_mirror [OK]
- Confusing direct with caching block
- Using non-existent block names
- Assuming provider_cache is valid
provider_installation {
filesystem_mirror {
path = "/cache/providers"
}
direct {
exclude = ["hashicorp/aws"]
}
}What happens when Terraform needs the
hashicorp/aws provider?Solution
Step 1: Analyze the filesystem_mirror and direct blocks
Thefilesystem_mirrorcaches providers locally except those excluded indirect.Step 2: Understand the exclude setting
Thedirectblock excludeshashicorp/aws, so Terraform will not use the cache for it.Step 3: Determine provider source
Terraform downloadshashicorp/awsdirectly from the internet.Final Answer:
Terraform downloadshashicorp/awsdirectly from the internet -> Option AQuick Check:
Exclude means direct download [OK]
- Assuming excluded providers use cache
- Thinking exclusion causes errors
- Believing providers are ignored if excluded
filesystem_mirror path but Terraform still downloads providers from the internet. What is the most likely cause?Solution
Step 1: Check filesystem_mirror path validity
If the path is wrong or Terraform cannot access it, caching won't work and providers download from the internet.Step 2: Rule out other causes
Terraform supports caching, CLI must be installed, and version absence doesn't force downloads if cache is valid.Final Answer:
Thefilesystem_mirrorpath is incorrect or inaccessible -> Option AQuick Check:
Invalid cache path causes downloads [OK]
- Assuming Terraform lacks caching support
- Ignoring filesystem permissions
- Blaming missing provider version
Solution
Step 1: Understand correct block usage
filesystem_mirrordefines the local cache path;directdefines providers to download directly.Step 2: Check correct syntax and order
provider_installation { filesystem_mirror { path = "/team/cache" } direct { exclude = ["customcorp/custom"] } }correctly setsfilesystem_mirrorwith path anddirectwith exclude list for custom providers.Step 3: Identify incorrect options
Other options misuse keys or swap path and exclude incorrectly.Final Answer:
provider_installation { filesystem_mirror { path = "/team/cache" } direct { exclude = ["customcorp/custom"] } }-> Option DQuick Check:
Cache path in filesystem_mirror, exclude in direct [OK]
- Swapping path and exclude keys
- Putting exclude in filesystem_mirror
- Misplacing blocks order
Ready to prove it?
Pass the test and this pattern moves unknown
