Problem
When running terraform init or other Terraform commands, you may encounter the following error indicating a failure to query provider packages.
Failed to query available provider packages Could not retrieve the list of available versions for provider hashicorp/*****: provider registry registry.terraform.io does not have a provider named registry.terraform.io/hashicorp/***** Did you intend to use *****/****? If so, you must specify that source address in each module which requires that provider. To see which modules are currently depending on hashicorp/******, run the following command: terraform providers
Cause
This error typically occurs when a module in your configuration requires a specific provider but does not explicitly declare it in a required_providers block within the module's own configuration files. Terraform requires this declaration so it knows where to download the provider from.
As the error message suggests, you can run the terraform providers command to identify which modules have implicit provider dependencies.
$ terraform providers
The output will show which modules are dependent on specific providers.
Solution
To resolve this issue, you must add a terraform block with a required_providers definition to each module that depends on the provider. This explicitly tells Terraform the source address for the provider within the context of that module.
Add the following block to the main.tf or other relevant configuration file inside the affected module, specifying the correct source and version for the required provider.
terraform {
required_providers {
# Replace "provider1" with the actual provider name, e.g., "confluent"
provider1 = {
# Replace with the correct provider source address
source = "example/example-provider"
# Specify a valid version constraint
version = "~> 1.42.0"
}
}
}After adding this block to each required module, run terraform init again. Terraform should now be able to correctly initialize the providers.
Additional Information
- For more details on provider requirements within modules, please refer to the official Terraform documentation on Provider Requirements.
- The solution is also discussed in this GitHub issue comment.