'Terraform 'Failed to instantiate provider' for GCP error on Mac OS X

Yesterday, Terraform was working fine on my Mac OS X laptop. Today I started using it, and I'm getting an error I've never seen before. I am using the google cloud provider.

I run a terraform init and that goes ok, but when I run terraform apply I get an error that says Failed to instantiate provider:

terraform apply                                                        

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".

2 problems:

- Failed to instantiate provider "registry.terraform.io/hashicorp/google" to
obtain schema: fork/exec
.terraform/providers/registry.terraform.io/hashicorp/google/3.60.0/darwin_amd64/terraform-provider-google_v3.60.0_x5:
input/output error
- Failed to instantiate provider "registry.terraform.io/hashicorp/random" to
obtain schema: fork/exec
.terraform/providers/registry.terraform.io/hashicorp/random/3.1.0/darwin_amd64/terraform-provider-random_v3.1.0_x5:
input/output error

It's telling me to run terraform init however I HAVE done that. I've been googling a while with no luck. Does anyone know how to resolve this error?



Solution 1:[1]

Cause

Provider source addresses starting with registry.terraform.io/-/ are a special way Terraform marks legacy addresses where the true namespace is unknown. For providers that were automatically installed in Terraform 0.12, Terraform 0.13 can automatically determine the new addresses for these using a lookup table in the public Terraform Registry, but for in-house providers you will need to provide the appropriate mapping manually (this can be caused by upgrading your terraform version).

The terraform state replace-provider subcommand allows re-assigning provider source addresses recorded in the Terraform state, and so we can use this command to tell Terraform how to reinterpret the “legacy” provider addresses as properly-namespaced providers that match with the provider source addresses in the configuration.

Solution

When using Terraform CLI with the default local backend, the error may be resolved by replacing the provider in the state using the below steps.

If the state file is stored in a backend other than local, the backend must be configured before the appropriate commands may be run. If the state file is stored locally, this step may be skipped.

If needed, we provide a guide for configuring the backend. As a note, it is not necessary to have the Terraform configuration files from the workspace downloaded into the local directory when performing this task. Once the remote backend is configured, the following steps will replace the providers in the state file.

First, initialize Terraform.

$terraform init

Once Terraform has been initialized, the following syntax may be used to replace the providers in the state file. You will be asked to confirm the change, which will only proceed if answered with “yes”.

$terraform state replace-provider registry.terraform.io/-/google 
registry.terraform.io/hashicorp/google

Once the commands have been run, verify that a new version of the state is created by navigating to the workspace on Terraform Enterprise or Terraform Cloud UI, clicking on States and checking for the latest version created “seconds ago”.

For comprehensive guides on how to use CLI with Terraform Cloud and how to solve this problem you can visit this page https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/version_3_upgrade#provider

Solution 2:[2]

I renamed the .terraform folder to something else and ran tf init again and then tf plan and it worked fine. Earlier it was pointing to existing plugins for some reason they are not being referenced properly.

So any providers you have in code, you can simply get them by starting as fresh. Remove old plugins and start with tf init

Solution 3:[3]

(I had this problem and resolved it today.)

If the init successfully but the plan fails, can be found in the current file directory In the .terraform/plugin directory, check the plug-ins. If there are no missing plug-ins, ll -a check the file size of each plug-in, and then check the file size according to the system architecture of the current environment https://releases.hashicorp.com/ Download the corresponding plug-ins and compare their sizes. If they are inconsistent, it means that the plug-ins you pulled during init do not match your current system architecture. If so, you can load the plug-in in the way of local warehouse

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 itIsNaz
Solution 2 Nandakishore
Solution 3