github.com/hugorut/terraform@v1.1.3/website/docs/language/files/dependency-lock.mdx (about)

     1  ---
     2  page_title: Dependency Lock File (.terraform.lock.hcl) - Configuration Language
     3  description: >-
     4    Terraform uses the dependency lock file .teraform.lock.hcl to track and select
     5    provider versions. Learn about dependency installation and lock file changes.
     6  ---
     7  
     8  # Dependency Lock File
     9  
    10  -> **Note:** This page is about a feature of Terraform 0.14 and later. Prior
    11  versions of Terraform did not track dependency selections at all, so the
    12  information here is not relevant to those versions.
    13  
    14  > **Hands-on:** Try the [Lock and Upgrade Provider Versions](https://learn.hashicorp.com/tutorials/terraform/provider-versioning?in=terraform/configuration-language&utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS) tutorial on HashiCorp Learn.
    15  
    16  A Terraform configuration may refer to two different kinds of external
    17  dependency that come from outside of its own codebase:
    18  
    19  - [Providers](/language/providers/requirements), which are plugins for Terraform
    20    that extend it with support for interacting with various external systems.
    21  - [Modules](/language/modules), which allow
    22    splitting out groups of Terraform configuration constructs (written in the
    23    Terraform language) into reusable abstractions.
    24  
    25  Both of these dependency types can be published and updated independently from
    26  Terraform itself and from the configurations that depend on them. For that
    27  reason, Terraform must determine which versions of those dependencies are
    28  potentially compatible with the current configuration and which versions are
    29  currently selected for use.
    30  
    31  [Version constraints](/language/expressions/version-constraints) within the configuration
    32  itself determine which versions of dependencies are _potentially_ compatible,
    33  but after selecting a specific version of each dependency Terraform remembers
    34  the decisions it made in a _dependency lock file_ so that it can (by default)
    35  make the same decisions again in future.
    36  
    37  At present, the dependency lock file tracks only _provider_ dependencies.
    38  Terraform does not remember version selections for remote modules, and so
    39  Terraform will always select the newest available module version that meets
    40  the specified version constraints. You can use an _exact_ version constraint
    41  to ensure that Terraform will always select the same module version.
    42  
    43  ## Lock File Location
    44  
    45  The dependency lock file is a file that belongs to the configuration as a
    46  whole, rather than to each separate module in the configuration. For that reason
    47  Terraform creates it and expects to find it in your current working directory
    48  when you run Terraform, which is also the directory containing the `.tf` files
    49  for the root module of your configuration.
    50  
    51  The lock file is always named `.terraform.lock.hcl`, and this name is intended
    52  to signify that it is a lock file for various items that Terraform caches in
    53  the `.terraform` subdirectory of your working directory.
    54  
    55  Terraform automatically creates or updates the dependency lock file each time
    56  you run [the `terraform init` command](/cli/commands/init). You should
    57  include this file in your version control repository so that you can discuss
    58  potential changes to your external dependencies via code review, just as you
    59  would discuss potential changes to your configuration itself.
    60  
    61  The dependency lock file uses the same low-level syntax as the main Terraform
    62  language, but the dependency lock file is not itself a Terraform language
    63  configuration file. It is named with the suffix `.hcl` instead of `.tf` in
    64  order to signify that difference.
    65  
    66  ## Dependency Installation Behavior
    67  
    68  When `terraform init` is working on installing all of the providers needed for
    69  a configuration, Terraform considers both the version constraints in the
    70  configuration _and_ the version selections recorded in the lock file.
    71  
    72  If a particular provider has no existing recorded selection, Terraform will
    73  select the newest available version that matches the given version constraint,
    74  and then update the lock file to include that selection.
    75  
    76  If a particular provider already has a selection recorded in the lock file,
    77  Terraform will always re-select that version for installation, even if a
    78  newer version has become available. You can override that behavior by adding
    79  the `-upgrade` option when you run `terraform init`, in which case Terraform
    80  will disregard the existing selections and once again select the newest
    81  available version matching the version constraint.
    82  
    83  If a particular `terraform init` call makes changes to the lock file, Terraform
    84  will mention that as part of its output:
    85  
    86  ```
    87  Terraform has made some changes to the provider dependency selections recorded
    88  in the .terraform.lock.hcl file. Review those changes and commit them to your
    89  version control system if they represent changes you intended to make.
    90  ```
    91  
    92  When you see this message, you can use your version control system to
    93  [review the changes Terraform has proposed in the file](#understanding-lock-file-changes),
    94  and if they represent changes you made intentionally you can send the change
    95  through your team's usual code review process.
    96  
    97  ### Checksum verification
    98  
    99  Terraform will also verify that each package it installs matches at least one
   100  of the checksums it previously recorded in the lock file, if any, returning an
   101  error if none of the checksums match:
   102  
   103  ```
   104  Error: Failed to install provider
   105  
   106  Error while installing hashicorp/azurerm v2.1.0: the current package for
   107  registry.terraform.io/hashicorp/azurerm 2.1.0 doesn't match any of the
   108  checksums previously recorded in the dependency lock file.
   109  ```
   110  
   111  This checksum verification is intended to represent a
   112  _[trust on first use](https://en.wikipedia.org/wiki/Trust_on_first_use)_
   113  approach. When you add a new provider for the first time you can verify it
   114  in whatever way you choose or any way you are required to by relevant
   115  regulations, and then trust that Terraform will raise an error if a future
   116  run of `terraform init` encounters a non-matching package for the same
   117  provider version.
   118  
   119  There are two special considerations with the "trust on first use" model:
   120  
   121  - If you install a provider from an origin registry which provides checksums
   122    that are signed with a cryptographic signature, Terraform will treat all
   123    of the signed checksums as valid as long as one checksum matches. The lock
   124    file will therefore include checksums for both the package you installed for
   125    your current platform _and_ any other packages that might be available for
   126    other platforms.
   127  
   128    In this case, the `terraform init` output will include the fingerprint of
   129    the key that signed the checksums, with a message like
   130    `(signed by a HashiCorp partner, key ID DC9FC6B1FCE47986)`. You may wish to
   131    confirm that you trust the holder of the given key before committing the
   132    lock file containing the signed checksums, or to retrieve and verify the
   133    full set of available packages for the given provider version.
   134  
   135  - If you install a provider for the first time using an alternative
   136    installation method, such as a filesystem or network mirror, Terraform will
   137    not be able to verify the checksums for any platform other than the one
   138    where you ran `terraform init`, and so it will not record the checksums
   139    for other platforms and so the configuration will not be usable on any other
   140    platform.
   141  
   142    To avoid this problem you can pre-populate checksums for a variety of
   143    different platforms in your lock file using
   144    [the `terraform providers lock` command](/cli/commands/providers/lock),
   145    which will then allow future calls to `terraform init` to verify that the
   146    packages available in your chosen mirror match the official packages from
   147    the provider's origin registry.
   148  
   149  ## Understanding Lock File Changes
   150  
   151  Because the dependency lock file is primarily maintained automatically by
   152  Terraform itself, rather than being updated manually by you or your team,
   153  your version control system may show you that the file has changed.
   154  
   155  There are a few different types of changes that Terraform can potentially make
   156  to your lock file, which you may need to understand in order to review the
   157  proposed changes. The following sections will describe these common situations.
   158  
   159  ### Dependency on a new provider
   160  
   161  If you add a new entry to the
   162  [provider requirements](/language/providers/requirements) for any module in your
   163  configuration, or if you add an external module that includes a new provider
   164  dependency itself, `terraform init` will respond to that by selecting the
   165  newest version of that provider which meets all of the version constraints
   166  in the configuration, and it will record its decision as a new `provider`
   167  block in the dependency lock file.
   168  
   169  ```diff
   170  --- .terraform.lock.hcl	2020-10-07 16:12:07.539570634 -0700
   171  +++ .terraform.lock.hcl	2020-10-07 16:12:15.267487237 -0700
   172  @@ -6,6 +6,26 @@
   173     ]
   174   }
   175  
   176  +provider "registry.terraform.io/hashicorp/azurerm" {
   177  +  version     = "2.30.0"
   178  +  constraints = "~> 2.12"
   179  +  hashes = [
   180  +    "h1:FJwsuowaG5CIdZ0WQyFZH9r6kIJeRKts9+GcRsTz1+Y=",
   181  +    "h1:c/ntSXrDYM1mUir2KufijYebPcwKqS9CRGd3duDSGfY=",
   182  +    "h1:yre4Ph76g9H84MbuhZ2z5MuldjSA4FsrX6538O7PCcY=",
   183  +    "zh:04f0a50bb2ba92f3bea6f0a9e549ace5a4c13ef0cbb6975494cac0ef7d4acb43",
   184  +    "zh:2082e12548ebcdd6fd73580e83f626ed4ed13f8cdfd51205d8696ffe54f30734",
   185  +    "zh:246bcc449e9a92679fb30f3c0a77f05513886565e2dcc66b16c4486f51533064",
   186  +    "zh:24de3930625ac9014594d79bfa42d600eca65e9022b9668b54bfd0d924e21d14",
   187  +    "zh:2a22893a576ff6f268d9bf81cf4a56406f7ba79f77826f6df51ee787f6d2840a",
   188  +    "zh:2b27485e19c2aaa9f15f29c4cff46154a9720647610171e30fc6c18ddc42ec28",
   189  +    "zh:435f24ce1fb2b63f7f02aa3c84ac29c5757cd29ec4d297ed0618423387fe7bd4",
   190  +    "zh:7d99725923de5240ff8b34b5510569aa4ebdc0bdb27b7bac2aa911a8037a3893",
   191  +    "zh:7e3b5d0af3b7411dd9dc65ec9ab6caee8c191aee0fa7f20fc4f51716e67f50c0",
   192  +    "zh:da0af4552bef5a29b88f6a0718253f3bf71ce471c959816eb7602b0dadb469ca",
   193  +  ]
   194  +}
   195  +
   196   provider "registry.terraform.io/newrelic/newrelic" {
   197     version     = "2.1.2"
   198     constraints = "~> 2.1.1"
   199  ```
   200  
   201  The new lock file entry records several pieces of information:
   202  
   203  - `version`: the exact version that Terraform selected based on the version
   204    constraints in the configuration.
   205  - `constraints`: all of the version constraints that Terraform considered when
   206    making this selection. (Terraform doesn't actually use this information to
   207    make installation decisions, but includes it to help explain to human readers
   208    how the previous decision was made.)
   209  - `hashes`: a number of checksums that are all considered to be valid for
   210    packages implementing the selected version of this provider on different
   211    platforms. The meaning of these hashes is explained more under
   212    _[New provider package checksums](#new-provider-package-checksums)_ below.
   213  
   214  ### New version of an existing provider
   215  
   216  If you run `terraform init -upgrade` to ask Terraform to consider newer provider
   217  versions that still match the configured version constraints, Terraform may
   218  then select a newer version for a provider and update its existing `provider`
   219  block to reflect that change.
   220  
   221  ```diff
   222  --- .terraform.lock.hcl	2020-10-07 16:44:25.819579509 -0700
   223  +++ .terraform.lock.hcl	2020-10-07 16:43:42.785665945 -0700
   224  @@ -7,22 +7,22 @@
   225   }
   226  
   227   provider "registry.terraform.io/hashicorp/azurerm" {
   228  -  version     = "2.1.0"
   229  -  constraints = "~> 2.1.0"
   230  +  version     = "2.0.0"
   231  +  constraints = "2.0.0"
   232     hashes      = [
   233  -    "h1:EOJImaEaVThWasdqnJjfYc6/P8N/MRAq1J7avx5ZbV4=",
   234  -    "zh:0015b491cf9151235e57e35ea6b89381098e61bd923f56dffc86026d58748880",
   235  -    "zh:4c5682ba1e0fc7e2e602d3f103af1638f868c31fe80cc1a884a97f6dad6e1c11",
   236  -    "zh:57bac885b108c91ade4a41590062309c832c9ab6bf6a68046161636fcaef1499",
   237  -    "zh:5810d48f574c0e363c969b3f45276369c8f0a35b34d6202fdfceb7b85b3ac597",
   238  -    "zh:5c6e37a44462b8662cf9bdd29ce30523712a45c27c5d4711738705be0785db41",
   239  -    "zh:64548940a3387aa3a752e709ee9eb9982fa820fe60eb60e5f212cc1d2c58549e",
   240  -    "zh:7f46749163da17330bbb5293dc825333c86304baa0a7c6256650ac536b4567c8",
   241  -    "zh:8f8970f2df75ac43ffdd112055ee069d8bd1030f7eb4367cc4cf494a1fa802c3",
   242  -    "zh:9ad693d00dc5d7d455d06faba70e716bce727c6706f7293288e87fd7956b8fe0",
   243  -    "zh:b6e3cb55e6aec62b47edd0d2bd5e14bd6a2bcfdac65930a6e9e819934734c57b",
   244  -    "zh:d6a3f3b9b05c28ecf3919e9e7afa185805a6d7442fc4b3eedba749c2731d1f0e",
   245  -    "zh:d81fb624a357c57c7ea457ce543d865b39b12f26c2edd58a2f7cd43326c91010",
   246  +    "h1:bigGXBoRbp7dv79bEEn+aaju8575qEXHQ57XHVPJeB8=",
   247  +    "zh:09c603c8904ca4a5bc19e82335afbc2837dcc4bee81e395f9daccef2f2cba1c8",
   248  +    "zh:194a919d4836d6c6d4ce598d0c66cce00ddc0d0b5c40d01bb32789964d818b42",
   249  +    "zh:1f269627df4e266c4e0ef9ee2486534caa3c8bea91a201feda4bca525005aa0a",
   250  +    "zh:2bae3071bd5f8e553355c4b3a547d6efe1774a828142b762e9a4e85f79be7f63",
   251  +    "zh:6c98dfa5c3468e8d02e2b3af7c4a8a14a5d469ce5a642909643b413a17ca338b",
   252  +    "zh:7af78f61666fd45fbf428161c061ea2623162d601b79dc71d6a5158756853ffa",
   253  +    "zh:883c2df86ae9ba2a5c167cf5c2c7deca0239171a224d6d335f0fd6dd9c283830",
   254  +    "zh:a2028379078577d8ff5ecfca6e8a8b25a25ffb1686de0ee52a7fe8011783488b",
   255  +    "zh:abe6ef399552fd3861a454a839cd978c1d15735658fdc00f9054435aff0f4620",
   256  +    "zh:c30b1bf14077913c3cdf34979b1434dbb1353cb5995eb3956b191c50538b64a9",
   257  +    "zh:ca64ae2ad9793e5631e3b0b9327f7cb22cb5d8e9de57be7d85821791b1d5a375",
   258  +    "zh:fffe56904a38109bb8d613b02808a177c3ddfac19f03b3aac799281fea38f475",
   259     ]
   260   }
   261  ```
   262  
   263  The primary effect of selecting a new provider version is to change the
   264  value of `version` in the `provider` block. If the upgrade came along with
   265  a change to the configured version constraints, Terraform will also record
   266  that change in the `constraints` value.
   267  
   268  Because each version has its own set of distribution packages, switching to
   269  a new version will also tend to replace all of the values in `hashes`, to
   270  reflect the checksums of the packages for the new version.
   271  
   272  ### New provider package checksums
   273  
   274  A more subtle change you may see in a `provider` block is the addition of
   275  new checksums that were not previously recorded, even though nothing else
   276  in the `provider` block has changed:
   277  
   278  ```diff
   279  --- .terraform.lock.hcl	2020-10-07 17:24:23.397892140 -0700
   280  +++ .terraform.lock.hcl	2020-10-07 17:24:57.423130253 -0700
   281  @@ -10,6 +10,7 @@
   282     version     = "2.1.0"
   283     constraints = "~> 2.1.0"
   284     hashes = [
   285  +    "h1:1xvaS5D8B8t6J6XmXxX8spo97tAzjhacjedFX1B47Fk=",
   286       "h1:EOJImaEaVThWasdqnJjfYc6/P8N/MRAq1J7avx5ZbV4=",
   287       "zh:0015b491cf9151235e57e35ea6b89381098e61bd923f56dffc86026d58748880",
   288       "zh:4c5682ba1e0fc7e2e602d3f103af1638f868c31fe80cc1a884a97f6dad6e1c11",
   289  ```
   290  
   291  The addition of a new checksum into the `hashes` value represents Terraform
   292  gradually transitioning between different _hashing schemes_. The `h1:` and
   293  `zh:` prefixes on these values represent different hashing schemes, each
   294  of which represents calculating a checksum using a different algorithm.
   295  We may occasionally introduce new hashing schemes if we learn of limitations
   296  in the existing schemes or if a new scheme offers some considerable
   297  additional benefit.
   298  
   299  The two hashing schemes currently supported are:
   300  
   301  - `zh:`: a mnemonic for "zip hash", this is a legacy hash format which is
   302    part of the Terraform provider registry protocol and is therefore used for
   303    providers that you install directly from an origin registry.
   304  
   305    This hashing scheme captures a SHA256 hash of each of the official `.zip`
   306    packages indexed in the origin registry. This is an effective scheme for
   307    verifying the official release packages when installed from a registry, but
   308    it's not suitable for verifying packages that come from other
   309    [provider installation methods](/cli/config/config-file#provider-installation),
   310    such as filesystem mirrors using the unpacked directory layout.
   311  
   312  - `h1:`: a mnemonic for "hash scheme 1", which is the current preferred hashing
   313    scheme.
   314  
   315    Hash scheme 1 is also a SHA256 hash, but is one computed from the _contents_
   316    of the provider distribution package, rather than of the `.zip` archive
   317    it's contained within. This scheme therefore has the advantage that it can
   318    be calculated for an official `.zip` file, an unpacked directory with the
   319    same contents, or a recompressed `.zip` file which contains the same files
   320    but potentially different metadata or compression schemes.
   321  
   322    Due to the limited scope of the `zh:` scheme, Terraform will
   323    opportunistically add in the corresponding `h1:` checksums as it learns
   324    of them, which is what caused the addition of a second `h1:` checksum
   325    in the example change shown above.
   326  
   327  Terraform will add a new hash to an existing provider only if the hash is
   328  calculated from a package that _also_ matches one of the existing hashes. In
   329  the above example, Terraform installed a `hashicorp/azurerm` package for a
   330  different platform than that which produced the original `h1:` checksum, but was
   331  able to match it against one of the `zh:` checksums recorded previously.
   332  After confirming the `zh:` checksum match, Terraform then recorded the
   333  corresponding `h1:` checksum in order to gradually migrate from the old scheme
   334  to the new scheme.
   335  
   336  When installing a particular provider for the first time (where there is no
   337  existing `provider` block for it), Terraform will pre-populate the `hashes`
   338  value with any checksums that are covered by the provider developer's
   339  cryptographic signature, which usually covers all of the available packages
   340  for that provider version across all supported platforms. However, because
   341  the provider registry protocol still uses the `zh:` scheme, the initial set
   342  will consist primarily of hashes using that scheme, which Terraform will then
   343  upgrade opportunistically as you install the packages on different platforms.
   344  
   345  If you wish to avoid ongoing additions of new `h1:` hashes as you work with
   346  your configuration on new target platforms, or if you are installing providers
   347  from a mirror that therefore can't provide official signed checksums, you
   348  can ask Terraform to pre-populate hashes for a chosen set of platforms
   349  using
   350  [the `terraform providers lock` command](/cli/commands/providers/lock):
   351  
   352  ```
   353  terraform providers lock \
   354    -platform=linux_arm64 \
   355    -platform=linux_amd64 \
   356    -platform=darwin_amd64 \
   357    -platform=windows_amd64
   358  ```
   359  
   360  The above command will download and verify the official packages for all of
   361  the required providers across all four of the given platforms, and then record
   362  both `zh:` and `h1:` checksums for each of them in the lock file, thus avoiding
   363  the case where Terraform will learn about a `h1:` equivalent only at a later
   364  time. See the `terraform providers lock` documentation for more information on
   365  this command.
   366  
   367  ### Providers that are no longer required
   368  
   369  If you remove the last dependency on a particular provider from your
   370  configuration, then `terraform init` will remove any existing lock file entry
   371  for that provider.
   372  
   373  ```diff
   374  --- .terraform.lock.hcl	2020-10-07 16:12:07.539570634 -0700
   375  +++ .terraform.lock.hcl	2020-10-07 16:12:15.267487237 -0700
   376  @@ -6,26 +6,6 @@
   377     ]
   378   }
   379  
   380  -provider "registry.terraform.io/hashicorp/azurerm" {
   381  -  version     = "2.30.0"
   382  -  constraints = "~> 2.12"
   383  -  hashes = [
   384  -    "h1:FJwsuowaG5CIdZ0WQyFZH9r6kIJeRKts9+GcRsTz1+Y=",
   385  -    "h1:c/ntSXrDYM1mUir2KufijYebPcwKqS9CRGd3duDSGfY=",
   386  -    "h1:yre4Ph76g9H84MbuhZ2z5MuldjSA4FsrX6538O7PCcY=",
   387  -    "zh:04f0a50bb2ba92f3bea6f0a9e549ace5a4c13ef0cbb6975494cac0ef7d4acb43",
   388  -    "zh:2082e12548ebcdd6fd73580e83f626ed4ed13f8cdfd51205d8696ffe54f30734",
   389  -    "zh:246bcc449e9a92679fb30f3c0a77f05513886565e2dcc66b16c4486f51533064",
   390  -    "zh:24de3930625ac9014594d79bfa42d600eca65e9022b9668b54bfd0d924e21d14",
   391  -    "zh:2a22893a576ff6f268d9bf81cf4a56406f7ba79f77826f6df51ee787f6d2840a",
   392  -    "zh:2b27485e19c2aaa9f15f29c4cff46154a9720647610171e30fc6c18ddc42ec28",
   393  -    "zh:435f24ce1fb2b63f7f02aa3c84ac29c5757cd29ec4d297ed0618423387fe7bd4",
   394  -    "zh:7d99725923de5240ff8b34b5510569aa4ebdc0bdb27b7bac2aa911a8037a3893",
   395  -    "zh:7e3b5d0af3b7411dd9dc65ec9ab6caee8c191aee0fa7f20fc4f51716e67f50c0",
   396  -    "zh:da0af4552bef5a29b88f6a0718253f3bf71ce471c959816eb7602b0dadb469ca",
   397  -  ]
   398  -}
   399  -
   400   provider "registry.terraform.io/newrelic/newrelic" {
   401     version     = "2.1.2"
   402     constraints = "~> 2.1.1"
   403  ```
   404  
   405  If you add a new requirement for the same provider at a later date and run
   406  `terraform init` again, Terraform will treat it as if it were
   407  [an entirely new provider](#dependency-on-a-new-provider)
   408  and so will not necessarily select the same version that was previously
   409  selected and will not be able to verify that the checksums remained unchanged.
   410  
   411  -> **Note:** In Terraform v1.0 and earlier, `terraform init` does not
   412  automatically remove now-unneeded providers from the lock file, and instead
   413  just ignores them. If you removed a provider dependency while using an
   414  earlier version of Terraform and then upgraded to Terraform v1.1 or later
   415  then you may see the error "missing or corrupted provider plugins", referring to
   416  the stale lock file entries. If so, run `terraform init` with the new Terraform
   417  version to tidy those unneeded entries and then retry the previous operation.