Dependency Lock File
Note: This page is about a feature of Terraform 0.14 and later. Prior versions of Terraform did not track dependency selections at all, so the information here is not relevant to those versions.
Hands-on: Try the Lock and Upgrade Provider Versions tutorial on HashiCorp Learn.
A Terraform configuration may refer to two different kinds of external dependency that come from outside of its own codebase:
- Providers, which are plugins for Terraform that extend it with support for interacting with various external systems.
- Modules, which allow splitting out groups of Terraform configuration constructs (written in the Terraform language) into reusable abstractions.
Both of these dependency types can be published and updated independently from Terraform itself and from the configurations that depend on them. For that reason, Terraform must determine which versions of those dependencies are potentially compatible with the current configuration and which versions are currently selected for use.
Version constraints within the configuration itself determine which versions of dependencies are potentially compatible, but after selecting a specific version of each dependency Terraform remembers the decisions it made in a dependency lock file so that it can (by default) make the same decisions again in future.
At present, the dependency lock file tracks only provider dependencies. Terraform does not remember version selections for remote modules, and so Terraform will always select the newest available module version that meets the specified version constraints. You can use an exact version constraint to ensure that Terraform will always select the same module version.
Lock File Location
The dependency lock file is a file that belongs to the configuration as a
whole, rather than to each separate module in the configuration. For that reason
Terraform creates it and expects to find it in your current working directory
when you run Terraform, which is also the directory containing the .tf
files
for the root module of your configuration.
The lock file is always named .terraform.lock.hcl
, and this name is intended
to signify that it is a lock file for various items that Terraform caches in
the .terraform
subdirectory of your working directory.
Terraform automatically creates or updates the dependency lock file each time
you run the terraform init
command. You should
include this file in your version control repository so that you can discuss
potential changes to your external dependencies via code review, just as you
would discuss potential changes to your configuration itself.
The dependency lock file uses the same low-level syntax as the main Terraform
language, but the dependency lock file is not itself a Terraform language
configuration file. It is named with the suffix .hcl
instead of .tf
in
order to signify that difference.
Dependency Installation Behavior
When terraform init
is working on installing all of the providers needed for
a configuration, Terraform considers both the version constraints in the
configuration and the version selections recorded in the lock file.
If a particular provider has no existing recorded selection, Terraform will select the newest available version that matches the given version constraint, and then update the lock file to include that selection.
If a particular provider already has a selection recorded in the lock file,
Terraform will always re-select that version for installation, even if a
newer version has become available. You can override that behavior by adding
the -upgrade
option when you run terraform init
, in which case Terraform
will disregard the existing selections and once again select the newest
available version matching the version constraint.
If a particular terraform init
call makes changes to the lock file, Terraform
will mention that as part of its output:
When you see this message, you can use your version control system to review the changes Terraform has proposed in the file, and if they represent changes you made intentionally you can send the change through your team's usual code review process.
Checksum verification
Terraform will also verify that each package it installs matches at least one of the checksums it previously recorded in the lock file, if any, returning an error if none of the checksums match:
This checksum verification is intended to represent a
trust on first use
approach. When you add a new provider for the first time you can verify it
in whatever way you choose or any way you are required to by relevant
regulations, and then trust that Terraform will raise an error if a future
run of terraform init
encounters a non-matching package for the same
provider version.
There are two special considerations with the "trust on first use" model:
If you install a provider from an origin registry which provides checksums that are signed with a cryptographic signature, Terraform will treat all of the signed checksums as valid as long as one checksum matches. The lock file will therefore include checksums for both the package you installed for your current platform and any other packages that might be available for other platforms.
In this case, the
terraform init
output will include the fingerprint of the key that signed the checksums, with a message like(signed by a HashiCorp partner, key ID DC9FC6B1FCE47986)
. You may wish to confirm that you trust the holder of the given key before committing the lock file containing the signed checksums, or to retrieve and verify the full set of available packages for the given provider version.If you install a provider for the first time using an alternative installation method, such as a filesystem or network mirror, Terraform will not be able to verify the checksums for any platform other than the one where you ran
terraform init
, and so it will not record the checksums for other platforms and so the configuration will not be usable on any other platform.To avoid this problem you can pre-populate checksums for a variety of different platforms in your lock file using the
terraform providers lock
command, which will then allow future calls toterraform init
to verify that the packages available in your chosen mirror match the official packages from the provider's origin registry.
Understanding Lock File Changes
Because the dependency lock file is primarily maintained automatically by Terraform itself, rather than being updated manually by you or your team, your version control system may show you that the file has changed.
There are a few different types of changes that Terraform can potentially make to your lock file, which you may need to understand in order to review the proposed changes. The following sections will describe these common situations.
Dependency on a new provider
If you add a new entry to the
provider requirements for any module in your
configuration, or if you add an external module that includes a new provider
dependency itself, terraform init
will respond to that by selecting the
newest version of that provider which meets all of the version constraints
in the configuration, and it will record its decision as a new provider
block in the dependency lock file.
The new lock file entry records several pieces of information:
version
: the exact version that Terraform selected based on the version constraints in the configuration.constraints
: all of the version constraints that Terraform considered when making this selection. (Terraform doesn't actually use this information to make installation decisions, but includes it to help explain to human readers how the previous decision was made.)hashes
: a number of checksums that are all considered to be valid for packages implementing the selected version of this provider on different platforms. The meaning of these hashes is explained more under New provider package checksums below.
New version of an existing provider
If you run terraform init -upgrade
to ask Terraform to consider newer provider
versions that still match the configured version constraints, Terraform may
then select a newer version for a provider and update its existing provider
block to reflect that change.
The primary effect of selecting a new provider version is to change the
value of version
in the provider
block. If the upgrade came along with
a change to the configured version constraints, Terraform will also record
that change in the constraints
value.
Because each version has its own set of distribution packages, switching to
a new version will also tend to replace all of the values in hashes
, to
reflect the checksums of the packages for the new version.
New provider package checksums
A more subtle change you may see in a provider
block is the addition of
new checksums that were not previously recorded, even though nothing else
in the provider
block has changed:
The addition of a new checksum into the hashes
value represents Terraform
gradually transitioning between different hashing schemes. The h1:
and
zh:
prefixes on these values represent different hashing schemes, each
of which represents calculating a checksum using a different algorithm.
We may occasionally introduce new hashing schemes if we learn of limitations
in the existing schemes or if a new scheme offers some considerable
additional benefit.
The two hashing schemes currently supported are:
zh:
: a mnemonic for "zip hash", this is a legacy hash format which is part of the Terraform provider registry protocol and is therefore used for providers that you install directly from an origin registry.This hashing scheme captures a SHA256 hash of each of the official
.zip
packages indexed in the origin registry. This is an effective scheme for verifying the official release packages when installed from a registry, but it's not suitable for verifying packages that come from other provider installation methods, such as filesystem mirrors using the unpacked directory layout.h1:
: a mnemonic for "hash scheme 1", which is the current preferred hashing scheme.Hash scheme 1 is also a SHA256 hash, but is one computed from the contents of the provider distribution package, rather than of the
.zip
archive it's contained within. This scheme therefore has the advantage that it can be calculated for an official.zip
file, an unpacked directory with the same contents, or a recompressed.zip
file which contains the same files but potentially different metadata or compression schemes.Due to the limited scope of the
zh:
scheme, Terraform will opportunistically add in the correspondingh1:
checksums as it learns of them, which is what caused the addition of a secondh1:
checksum in the example change shown above.
Terraform will add a new hash to an existing provider only if the hash is
calculated from a package that also matches one of the existing hashes. In
the above example, Terraform installed a hashicorp/azurerm
package for a
different platform than that which produced the original h1:
checksum, but was
able to match it against one of the zh:
checksums recorded previously.
After confirming the zh:
checksum match, Terraform then recorded the
corresponding h1:
checksum in order to gradually migrate from the old scheme
to the new scheme.
When installing a particular provider for the first time (where there is no
existing provider
block for it), Terraform will pre-populate the hashes
value with any checksums that are covered by the provider developer's
cryptographic signature, which usually covers all of the available packages
for that provider version across all supported platforms. However, because
the provider registry protocol still uses the zh:
scheme, the initial set
will consist primarily of hashes using that scheme, which Terraform will then
upgrade opportunistically as you install the packages on different platforms.
If you wish to avoid ongoing additions of new h1:
hashes as you work with
your configuration on new target platforms, or if you are installing providers
from a mirror that therefore can't provide official signed checksums, you
can ask Terraform to pre-populate hashes for a chosen set of platforms
using
the terraform providers lock
command:
The above command will download and verify the official packages for all of
the required providers across all four of the given platforms, and then record
both zh:
and h1:
checksums for each of them in the lock file, thus avoiding
the case where Terraform will learn about a h1:
equivalent only at a later
time. See the terraform providers lock
documentation for more information on
this command.
Providers that are no longer required
If you remove the last dependency on a particular provider from your
configuration, then terraform init
will remove any existing lock file entry
for that provider.
If you add a new requirement for the same provider at a later date and run
terraform init
again, Terraform will treat it as if it were
an entirely new provider
and so will not necessarily select the same version that was previously
selected and will not be able to verify that the checksums remained unchanged.
Note: In Terraform v1.0 and earlier, terraform init
does not
automatically remove now-unneeded providers from the lock file, and instead
just ignores them. If you removed a provider dependency while using an
earlier version of Terraform and then upgraded to Terraform v1.1 or later
then you may see the error "missing or corrupted provider plugins", referring to
the stale lock file entries. If so, run terraform init
with the new Terraform
version to tidy those unneeded entries and then retry the previous operation.