This is a recurring issue I’m running into when upgrading from v0.12.29 to v0.13 (v0.13.4 currently in this case). I just want to preface that running the terraform state replace-provider
command DOES fix this, I’m here to try and find out why automatic upgrade does not work.
The snippets below are from a bigger project, but I want to note that I also see the same error on very simple projects with a single AWS resource.
I ran the terraform 0.13upgrade
command and the only change it made was adding the required_providers
to my versions.tf file.
terraform {
required_version = ">= 0.13"
required_providers {
archive = {
source = "hashicorp/archive"
}
aws = {
source = "hashicorp/aws"
}
template = {
source = "hashicorp/template"
}
}
}
Note that I have also tried this without the required_providers
section.
My pipeline validates and inits fine, but when it plans, I get this error:
Releasing state lock. This may take a few moments...
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".
3 problems:
- Failed to instantiate provider "registry.terraform.io/-/archive" to obtain
schema: unknown provider "registry.terraform.io/-/archive"
- Failed to instantiate provider "registry.terraform.io/-/aws" to obtain
schema: unknown provider "registry.terraform.io/-/aws"
- Failed to instantiate provider "registry.terraform.io/-/template" to obtain
schema: unknown provider "registry.terraform.io/-/template"
##[error]PowerShell exited with code '1'.
I’ve seen people have this error specifically on Terraform Cloud (which I am not using). The solution was to remove the bad provider path (ex: “registry.terraform.io/-/aws”) and replace it with the good one (ex: “registry.terraform.io/hashicorp/aws”). For clarity, here is what the provider.archive
looks like in state (some info redacted):
"resources": [
{
"mode": "data",
"type": "archive_file",
"name": "zip_function_name",
"provider": "provider.archive",
"instances": [
{
"schema_version": 0,
"attributes": {
"excludes": null,
"id": "xxxxx",
"output_base64sha256": "xxxxx",
"output_md5": "xxxxx",
"output_path": "./zip/my-file.zip",
"output_sha": "xxxxx",
"output_size": 721,
"source": [],
"source_content": null,
"source_content_filename": null,
"source_dir": null,
"source_file": "./script/my-script-file.py",
"type": "zip"
}
}
]
},
For more clarity, here is the output of my terraform providers
command:
Providers required by configuration:
.
├── provider[registry.terraform.io/hashicorp/archive]
├── provider[registry.terraform.io/hashicorp/aws]
├── provider[registry.terraform.io/hashicorp/template]
└── module.db
├── provider[registry.terraform.io/hashicorp/aws] >= 2.49.*, < 4.0.*
├── module.db_option_group
│ └── provider[registry.terraform.io/hashicorp/aws]
├── module.db_parameter_group
│ └── provider[registry.terraform.io/hashicorp/aws]
├── module.db_subnet_group
│ └── provider[registry.terraform.io/hashicorp/aws]
└── module.db_instance
└── provider[registry.terraform.io/hashicorp/aws]
Providers required by state:
provider[registry.terraform.io/-/archive]
provider[registry.terraform.io/-/aws]
provider[registry.terraform.io/-/template]
Like I said at the beginning, the following resolves my errors:
terraform state replace-provider "registry.terraform.io/-/aws" "registry.terraform.io/hashicorp/aws"
terraform state replace-provider "registry.terraform.io/-/template" "registry.terraform.io/hashicorp/template"
terraform state replace-provider "registry.terraform.io/-/archive" "registry.terraform.io/hashicorp/archive"
For reference, this changed the “provider” line to:
"provider": "provider[\"registry.terraform.io/hashicorp/archive\"]",
My question is why doesn’t the automatic upgrade work for us? From what I’ve read in the upgrade guide Terraform should be able to resolve the providers it knows about automatically. I’d expect it to know about aws
and the like.
Do we have a v0.12 setup that caused state issues for everything?
Any help or input in figuring this out would be much appreciated. I really don’t feel like manually updating thousands of state files as issues pop up.
Thank you!