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