tfmodcache
pip install tfmodcache==0.1.0
A local, shared module cache for Terraform and OpenTofu. terraform init downloads the same modules again for every repository and every CI job; tfmodcache keeps one copy on disk and lays it out where Terraform looks, so the second run and every run after it costs no network at all.
There is a plugin cache for providers (TF_PLUGIN_CACHE_DIR). There is none for modules. This is that missing cache, and nothing else.
Use it
Warm the cache and run init in one step:
$ tfmodcache init
linked 4 modules (4 downloaded, 0 already cached)
tfmodcache: running terraform init
Terraform's own output follows, unaffected by this tool.
Run it again, in this repository or in any other one that uses the same modules:
$ tfmodcache init
linked 4 modules (0 downloaded, 4 already cached)
Commands
tfmodcache init [DIR] [-- ARGS]lays the modules out from the cache, then runsterraform initwithARGS.tfmodcache link [DIR]lays the modules out and writes the manifest, without running anything.tfmodcache warm [DIR]fills the cache only, and touches nothing inside the project.tfmodcache list [--json]shows what the cache holds.tfmodcache purge --allandtfmodcache purge --older-than DAYSremove cached modules.tfmodcache pathprints the cache directory.
Useful flags: --offline never touches the network, --cache-dir PATH moves the cache, --symlink links modules instead of copying them, --binary tofu picks OpenTofu.
In continuous integration
The cache is one directory, so any CI cache action can keep it between runs.
- uses: actions/cache@v4
with:
path: ~/.cache/tfmodcache
key: tfmodcache-${{ hashFiles('**/*.tf') }}
- run: pip install tfmodcache==0.1.0
- run: tfmodcache init -- -input=false
What it caches, and what it leaves alone
Cached: registry modules (namespace/name/provider, public or private registry, exact version) and git modules (git::, github.com/..., git@..., with or without ?ref= and //subdir). Nested modules are followed, including modules declared inside a cached module.
Left to Terraform, on purpose, with a message on stderr saying so:
- version constraints that are not one exact version, such as
~> 5.0. Terraform owns constraint solving, and a cache that quietly resolved a range differently would be worse than no cache. - source kinds this tool does not implement, such as
s3::orgcs::. - providers.
TF_PLUGIN_CACHE_DIRalready covers them, and this tool never touches them. .tf.jsonconfiguration files.
Anything left alone is downloaded by Terraform exactly as before. The worst case of installing this tool is the behaviour you already had.
Limits
The module layout and the manifest this tool writes follow the format Terraform documents. The module registry protocol has been checked against a real registry. Running terraform init itself against the manifest this tool produces has not been verified end to end yet. If the manifest is ever wrong, Terraform falls back to downloading the module itself, so the worst case stays the behaviour you had before installing this tool.
Where things live
The cache defaults to ~/.cache/tfmodcache, or $XDG_CACHE_HOME/tfmodcache, or $TFMODCACHE_HOME when set. Inside a project, tfmodcache writes only .terraform/modules, which is the directory Terraform itself owns.
Requirements
Python 3.9 or newer, no third party dependencies. git on PATH is needed for git module sources.
Licence
MIT. Written by Younes Z.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tfmodcache-0.1.0.tar.gz.
File metadata
- Download URL: tfmodcache-0.1.0.tar.gz
- Upload date:
- Size: 25.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3dc63e4cd56bd8b4cdeeac10e9a9ef421b7a38a66711cf452d21fec92b60984
|
|
| MD5 |
8d0c17b1ba1739cb413b6e28aec2c381
|
|
| BLAKE2b-256 |
42fdcafcef529cde1dc72e6ab1df85f4041059138a3e52c4e826b075a0d12c3d
|
File details
Details for the file tfmodcache-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tfmodcache-0.1.0-py3-none-any.whl
- Upload date:
- Size: 21.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a697a3ce7872771cfa219c206e70434283bf79e342ad3d715941e79f818d03b
|
|
| MD5 |
129e66364c9a1234f746de939dd0615e
|
|
| BLAKE2b-256 |
4f2943bf395d6b82462c10cbea40dd64db8acf627468f421ca7892a59f423b90
|