Hi @bhadrim,
This behavior has been one where different users have being surprised by the behavior either way: as you say, in older versions Terraform would sometimes (if the newly-requested version doesn’t match the version constraint) ignore what was available locally and go download a new version, which several folks found confusing because they believed that they’d clearly told Terraform to never access the network for that particular plugin.
Consequently, Terraform’s current default behavior is that if you have a provider installed into one of the implied mirror directories then Terraform will assume that you only intend to use local versions of that provider, e.g. because you’ve modified it in some way compared to the upstream and you only want to use your own build.
There are a few ways you can customize this behavior. One way is to continue on the path you were already considering and explicitly tell Terraform to consult both the local mirror directory and origin registries:
provider_installation {
filesystem_mirror {
path = "/home/yourself/.terraform.d/plugins"
}
direct {}
}
This is different from the automatic configuration Terraform will build for itself in the absense of any explicit configuration, based only on what you have in your implied mirror directory:
provider_installation {
filesystem_mirror {
path = "/home/yourself/.terraform.d/plugins"
include = ["hashicorp/google"]
}
direct {
exclude = ["hashicorp/google"]
}
}
Because the first explicit example above doesn’t include those include
and exclude
configurations that Terraform implicitly inserts when doing automatic behavior, it’ll consult both the local directory and the origin registry and choose whichever one has the newest version matching the constraint.
However, you also mentioned that you are concerned about “wasting network traffic”, which makes me think that your main motivation here might be to avoid re-downloading the same plugins multiple times. In that case, you might be better served by leaving the provider installation methods entirely unmodified (no provider_installation
block or local mirror directories at all) and instead enabling a plugin cache directory:
plugin_cache_dir = "$HOME/.terraform.d/plugin-cache"
If you add that configuration (and create the directory it mentions) then terraform init
will treat that directory as a read-through cache for any plugins it needs to install, regardless of the underlying installation method. That means that the first time you select a new version it’ll download it into the cache and then link it from the cache into your working directory. On subsequent installs of the same provider it’ll notice that the cache is already populated and just re-use the existing package directory from local disk.
Once you’re on a newer Terraform version that uses an explicit dependency lock file, both the local mirror technique and the local cache technique unfortunately currently prevent Terraform’s automatic verification of the full set of checksums for a provider already in the cache, because that mechanism requires a network request to the origin registry. Therefore you may need to use terraform providers lock
to write multiple platform-specific checksums into the lock file if your team uses Terraform across more than one operating system and CPU architecture.