Search code examples
jenkinsterraformterraform-provider-aws

Constant "Error: Could not load plugin: Plugin reinitialization required" in Jenkins job


I'm having this issue in Jenkis job:

+ terraform --version
Terraform v0.15.5

+ rm -rf .terraform/
+ rm -f .terraform.lock.hcl
+ terraform init -upgrade -reconfigure
Upgrading modules...
...

Initializing the backend...

Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.

Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 3.0"...
- Using hashicorp/aws v3.75.2 from the shared cache directory

...

Terraform has been successfully initialized!

...

+ terraform plan -var-file qa-up.tfvars -out=tfplan
╷
│ Error: Could not load plugin
│ 
│ 
│ Plugin reinitialization required. Please run "terraform init".
│ 
│ Plugins are external binaries that Terraform uses to access and manipulate
│ resources. The configuration provided requires plugins which can't be
│ located,
│ don't satisfy the version constraints, or are otherwise incompatible.
│ 
│ Terraform automatically discovers provider requirements from your
│ configuration, including providers used in child modules. To see the
│ requirements and constraints, run "terraform providers".
│ 
│ failed to instantiate provider "registry.terraform.io/hashicorp/aws" to
│ obtain schema: the cached package for registry.terraform.io/hashicorp/aws
│ 3.75.2 (in .terraform/providers) does not match any of the checksums
│ recorded in the dependency lock file


script returned exit code 1

The providers (local):

$ terraform providers

Providers required by configuration:
.
├── provider[registry.terraform.io/hashicorp/aws]
└── module.elasticache
    └── provider[registry.terraform.io/hashicorp/aws] ~> 3.0

Providers required by state:

provider[registry.terraform.io/hashicorp/aws]

But once run locally (assuming terraform version is also 0.15.5), there is no such issue. What could be the reason of it and how to fix it?


Solution

  • This issue tends to happen when there are multiple CPU architectures being used. The terraform init command will download the provider binary for the underlying CPU architecture and create a corresponding .terraform.lock.hcl file. In order to make it work in a mixed environment, there is a command that can be used to avoid the issue [1]:

    terraform providers lock \
      -platform=windows_amd64 \ # 64-bit Windows
      -platform=darwin_amd64 \  # 64-bit macOS
      -platform=linux_amd64     # 64-bit Linux
    

    For M1 MacOS it would be -platform=darwin_arm64. Since it seems there is some kind of caching involved, removing the old lock file and using the new one should help.


    [1] https://developer.hashicorp.com/terraform/cli/commands/providers/lock#specifying-target-platforms