Recently I tried upgrading from Terraform 0.12 to 1.0.1. I think the key here is leaving 0.12.
Anyway, there was a breaking change in 0.13 around how providers worked, and we got this cryptic error.
(Caveat: I don’t actually know how much of this has to do with a newrelic provider update as well or instead, but was what we got the week of 2021/07/12.)
Initializing provider plugins...
- Reusing previous version of newrelic/newrelic from the dependency lock file
- Finding latest version of hashicorp/newrelic...
- Using previously-installed newrelic/newrelic v2.23.0
╷
│ Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider hashicorp/newrelic: provider registry registry.terraform.io does not have a provider
│ named registry.terraform.io/hashicorp/newrelic
│
│ Did you intend to use newrelic/newrelic? If so, you must specify that source address in each module which requires that provider. To see which
│ modules are currently depending on hashicorp/newrelic, run the following command:
│ terraform providers
|
So I’d already added this at my root module but this error still occurred.
terraform {
required_providers {
newrelic = {
source = "newrelic/newrelic"
version = "2.23.0"
}
}
}
provider "newrelic" {
api_key = "12345"
account_id = "12345"
}
It took a while to find the right docs to understand this one.
The key message from above is the You must specify that source address in each module which requires that provider.
message. We are using modules, and the breaking change to 0.13 and above is that modules can specify required_providers
.
What they don’t tell you clearly, is that when trying to resolve “newrelic” provider by default, it will look for “hashicorp/newrelic”. However the actual provider source is “newrelic/newrelic”. So to make this work the optional required_providers
is actually mandatory, and actually needs the source = "newrelic/newrelic"
property.
terraform {
required_providers {
newrelic = {
source = "newrelic/newrelic"
}
}
}
Adding this to every module that uses newrelic solved the problem.
If you are doing this, you might also need to migrate your state over as well. Do this after running terraform init
from inside your config folder. This migrates the data in your state file to correctly map to the new provider.
terraform state replace-provider newrelic newrelic/newrelic
More details here: https://www.terraform.io/docs/cli/commands/state/replace-provider.html
If you do migrate 0.12 to 0.13 first, instead of stupidly jumping straight to 1.0.1, you can follow a more useful migration guide and the 0.13upgrade
command instead. 🤦♂️
https://www.terraform.io/docs/cli/commands/0.13upgrade.html https://www.terraform.io/upgrade-guides/0-13.html