Professional Documents
Culture Documents
Terraform Providers & Lock File
Terraform Providers & Lock File
Terraform providers are downloaded and installed during the terraform init stage of the
Terraform workflow. They can be downloaded from the public registry, or a local registry or
mirror. The provider is downloaded, when it’s not present in the .terraform subdirectory. When
it is already present, it is not downloaded again. The version number is also checked. Changing
the version number in the configuration files would cause that version of the provider to be
downloaded.
To save bandwidth and speed up the runtime, however, the use of an optional plugin cache
can be specified using plugin_cache_dir in the CLI configuration file which configures per
user CLI behaviors (terraform.rc or .terraformrc).
E.g.
Provider Configuration
Provider configurations should be declared in the root module of your Terraform project. They
can be declared in any .tf file. I would recommend either putting them in the main.tf, creating
a providers.tf file specifically for the Providers and nothing else, or alternatively
a versions.tf file which would include the required_providers block and specify the Terraform
version. Any child modules receive their provider configuration from the root module.
Providers are configured using a provider’s block. Some providers require certain information
to be configured such as endpoint URLs or cloud regions.
What is the Terraform lock file?
The Terraform lockfile as already mentioned is a file used by Terraform to track the desired
state of your infrastructure. It’s almost equivalent to the package-lock.json file in the realm of
npm as a package manager. Let's delve a bit deeper: Each Terraform project involves
dependencies on code components that extend beyond its immediate codebase. These
external dependencies hinge on Terraform Providers and Modules, which might reside outside
of the project. Before Terraform 0.14, there was any way to manage the versions of these
dependencies. But later (=> 0.14) the Dependency Lock File was introduced for this purpose.
This file .terraform.lock.hcl is generated by Terraform when you run the terraform
init command. This file serves as a reference point across all executions, aiding in the
evaluation of compatible dependencies with the current configuration. It is used to lock the
versions of the provider plugins and modules being used in your Terraform configuration,
ensuring that subsequent runs of terraform apply or terraform plan use the same versions. This,
in turn, facilitates the meticulous selection of fitting dependency versions for seamless
utilization.