Skip to content

Lock Files, Plugin Cache and using several architectures fails terraform init #29958

@LDVSOFT

Description

@LDVSOFT

So, since I am using several stacks of Terraform configuration quite regularly and their plugins in use are mostly the same, I didn't want providers to be downloaded for each one, so I enabled plugin cache directory (see below). Also, at some point in time I started using lock files instead of relying on using version = "= x.y" to always reuse the same version.

Since we use Terraform on several machines, and some developers are using different architectures (I'm using Linux Amd64, my colleague is using Darwin Amd64, more architectures might come with M1), I expect lock files to support being used on several architectures. Documentation states that even though h1s only for current architectures are populated, since zhs are always present they are enough to check either plugin for a new architecture already matches known hashes, so that Terraform can append new h1 hash on a first download. Sounds great, actually (or one can assume we can remember h1s directly, but it's not my expertise).

The only problem is that, somehow, Terraform always does a little different thing for me:

  1. When I run terraform init that resolved to a plugin version that is not in the cache, Terraform actually does it's job: it downloads the plugin, populates the lock file with all zhs and a single h1. If I will use this lock file on a different machine everything works as expected.
  2. When I run terraform init for the same plugin version in another directory, it is accessed from the cache, and only h1 hash is written into the lock file.
  3. When my colleague will run terraform init on the second stack with my lock file (if I commit it into VCS), they will get an error.

As I have found out, instead of trying to use different hardware I can emulate the behaviour by just running terraform providers lock -platform=darwin_amd64, where platform can be anything but not the current one.

Terraform Version

Terraform v1.0.11
on linux_amd64
+ provider registry.terraform.io/hashicorp/null v3.1.0

Also I've seen this on a lot of versions, since, I think, around 0.15.

Terraform Configuration Files

There is nothing relevant beyond version requirements:

terraform {
  required_version = ">= 1.0.4"
  required_providers {
    null = {
      source  = "hashicorp/null"
      version = "~> 3.1"
    }
  }
}

Relevant ~/.terraformrc:

plugin_cache_dir = "$HOME/.cache/terraform/plugin-cache"

Debug Output

This Gist includes all logs

Expected Behavior

terraform init should populate lock file well enough for future use or switch to redownloading plugin in case lock file doesn't provide enough information.

Actual Behavior

Terraform disrupts developer experience by forcing to do terraform providers lock -platform=… with every platform in use so that terraform init wouldn't fail in the future.

Steps to Reproduce

Here I assume that I'm running on linux_amd64, and I'm using darwin_amd64 and darwin_arm64 as different achitectures. However, I experienced this bug with other combinations, I don't think exact platforms are relevant here.

  1. Create ~/.terraformrc file with line above, create given directory / check that it's empty.
  2. Create an empty directory a with versions.tf file as above.
  3. terraform init in it. Should work like a charm.
  4. Open .terraform.lock.hcl to see that there are a lot of hash lines.
  5. terraform providers lock -platform=darwin_amd64. Works, lock file is updated with a new h1 hash.
  6. Create an empty direcory b with the same versions.tf file.
  7. terraform init in it. Should work.
  8. However, terraform.lock.hcl only contains a single h1 hash.
  9. terraform providers lock -platform=darwin_arm64. Fails:
    ╷
    │ Error: Could not retrieve providers for locking
    │
    │ Terraform failed to fetch the requested providers for darwin_arm64 in order
    │ to calculate their checksums: some providers could not be installed:
    │ - registry.terraform.io/hashicorp/null: the current package for
    │ registry.terraform.io/hashicorp/null 3.1.0 doesn't match any of the
    │ checksums previously recorded in the dependency lock file.
    ╵
    

Additional Context

I am sorry, this can be already discussed in another Cache+Lock-related issue, but I haven't seen exactly my case being described. I am opening this one with the exact reproducible case.

References

For a lot of time I was following #28041 for this problem, but it seems to me it has lost the direction I am interested in.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugconfirmeda Terraform Core team member has reproduced this issueexplaineda Terraform Core team member has described the root cause of this issue in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions