James Duffy

Warning: Terraform Provider Moved

// By Updated:
featured-image.png

Recently I had an issue where Terraform did not seem to be loading the correct provider. I was getting the following result from a terraform init -upgrade even though I had updated the provider name in versions.tf

1
2
3
4
5
6
7
8
Warning: Additional provider information from registry

The remote registry returned warnings for
registry.terraform.io/terraform-providers/cloudflare:
- For users on Terraform 0.13 or greater, this provider has moved to
cloudflare/cloudflare. Please update your source in required_providers.

Terraform has been successfully initialized!

What appeared to be the issue is that changing the provider source in versions.tf like below did not result in the underlying resources using the new provider.

1
2
- source = "terraform-providers/cloudflare"
+ source = "cloudflare/cloudflare"

This makes sense once you realize what Terraform is doing. You wouldn’t want someone to accidentally swap the source of a provider to something different entirely.

You can see this in Terraform’s state. Go ahead and pull your state to stdout by running terraform state pull and you will find that each resource includes a provider.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
  "module": "module.cloudflare",
  "mode": "managed",
  "type": "cloudflare_zone",
  "name": "main",
  "provider": "provider[\"registry.terraform.io/terraform-providers/cloudflare\"]",
  "instances": [
    {
      ...
    }
  ]
},

Now you can go through the trouble of pulling your state file and manually making the changes and pushing it again, but the correct way to update it is to use the following command:

1
terraform state replace-provider OLD_PROVIDER NEW_PROVIDER

Or for my cloudflare example:

1
terraform state replace-provider terraform-providers/cloudflare cloudflare/cloudflare

Terraform will show you what resources you will be changing with this command and ask you to confirm.