# Module tries to instantiate wrong provider: failed to instantiate provider "xxx" to obtain schema: unavailable provider

**URL:** <https://discuss.hashicorp.com/t/module-tries-to-instantiate-wrong-provider-failed-to-instantiate-provider-xxx-to-obtain-schema-unavailable-provider/47716>\
**Category:** Terraform\
**Created:** [December 7, 2022, 6:41am UTC](https://discuss.hashicorp.com/t/module-tries-to-instantiate-wrong-provider-failed-to-instantiate-provider-xxx-to-obtain-schema-unavailable-provider/47716 "2022-12-07T06:41:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![papanito](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/papanito/32/19476_2.png) [@papanito](https://discuss.hashicorp.com/u/papanito)\
**Post date:** [December 7, 2022, 6:41am UTC](https://discuss.hashicorp.com/t/module-tries-to-instantiate-wrong-provider-failed-to-instantiate-provider-xxx-to-obtain-schema-unavailable-provider/47716/1 "2022-12-07T06:41:14Z")

</div>

I have a module `./modules/monitoring`

```auto
/
+- modules
| +- monitoring
| + backend.tf
| + main.tf
| + variables.tf
+- subdir1
| + main.tf
| ...
+- subdir2
| + main.tf
| ...
...

```

It contains the following `backend.tf`

```auto
terraform {
  required_providers {
    kubectl = {
      source = "gavinbunney/kubectl"
      version = "~>1.14.0"
    }
  }
}

```

which contains a [kubectl\_manifest](https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/resources/kubectl_manifest)-resource:

```auto
resource "kubectl_manifest" "monitoring_alertmanagerconfig_project" {
...

```

I use the module as follows:

```auto
module "monitoring" {
  source = "../modules/monitoring"
  clusterid = rancher2_cluster.cluster.id
  project_namespaces = local.project_namespaces
  namespaces = local.namespaces
}

```

`tf plan`gives me this error

```auto
│ Error: failed to read schema 
for kubectl_manifest.monitoring_alertmanagerconfig_project 
in registry.terraform.io/hashicorp/kubectl: failed to instantiate provider 
"registry.terraform.io/hashicorp/kubectl" to obtain schema: 
unavailable provider "registry.terraform.io/hashicorp/kubectl"

```

---

<div class="post-metadata">

**Author:** ![maxb](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/maxb/32/26655_2.png) [@maxb](https://discuss.hashicorp.com/u/maxb)\
**Post date:** [December 7, 2022, 8:26am UTC](https://discuss.hashicorp.com/t/module-tries-to-instantiate-wrong-provider-failed-to-instantiate-provider-xxx-to-obtain-schema-unavailable-provider/47716/2 "2022-12-07T08:26:37Z")

</div>

> [@papanito](#):
>
> It contains the following `backend.tf`

It’s a bit weird (though not likely to be a cause of your issue) to be writing configuration that is not about the backend, in a file called `backend.tf`.

> [@papanito](#):
>
> ```auto
> source = "../modules/monitoring"
> 
> ```

Are you really using the module you think you are, because this says `..` not `.` so you would be using another module from the parent directory instead of the one you showed in your directory listing.

When I copy the code you showed into local files, and fix the `..` to `.`, and run `terraform init`, it finds the expected provider just fine for me.

---

<div class="post-metadata">

**Author:** ![papanito](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/papanito/32/19476_2.png) [@papanito](https://discuss.hashicorp.com/u/papanito)\
**Post date:** [December 7, 2022, 8:50am UTC](https://discuss.hashicorp.com/t/module-tries-to-instantiate-wrong-provider-failed-to-instantiate-provider-xxx-to-obtain-schema-unavailable-provider/47716/3 "2022-12-07T08:50:22Z")

</div>

> [@papanito](#):
>
> `source = "../modules/monitoring"`

while simplifying my example I made it apparently too simple. The `..` makes sense, I will update the example.

> It’s a bit weird (though not likely to be a cause of your issue) to be writing configuration that is not about the backend, in a file called backend.tf.

I changed that but still the same

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [December 7, 2022, 7:29pm UTC](https://discuss.hashicorp.com/t/module-tries-to-instantiate-wrong-provider-failed-to-instantiate-provider-xxx-to-obtain-schema-unavailable-provider/47716/4 "2022-12-07T19:29:15Z")

</div>

Hi @papanito,

It sounds like your other modules are missing their declaration that they depend on `gavinbunney/kubectl`.

For backward compatibility with modules that were written before Terraform supported third-party providers Terraform assumes that any undeclared dependency is for an official provider (in the `hashicorp/` namespace), but modern Terraform modules should always specify all of the providers they depend on.

If I’ve guessed correctly what the problem is then the solution would be to write a similar `required_providers` block in each module. However, shared modules should not typically constrain the upper bound in their version constraints, because that tends to make things complicated if you want to upgrade later (you’ll need to update all of the modules at once) and so I would recommend setting the shared modules to use only `>=`-style constraints (instead of `~>` constraints) and to specify the minimum provider version that you know the provider is compatible with. (Version constraints are intended for specifying provider version compatibility, not for specifying version _selections_.)

---

<div class="post-metadata">

**Author:** ![papanito](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/papanito/32/19476_2.png) [@papanito](https://discuss.hashicorp.com/u/papanito)\
**Post date:** [December 8, 2022, 3:11pm UTC](https://discuss.hashicorp.com/t/module-tries-to-instantiate-wrong-provider-failed-to-instantiate-provider-xxx-to-obtain-schema-unavailable-provider/47716/5 "2022-12-08T15:11:17Z")

</div>

> [@apparentlymart](#):
>
> I would recommend setting the shared modules to use only `>=` -style constraints

Thanks changed it.

> [@apparentlymart](#):
>
> It sounds like your other modules are missing their declaration that they depend on `gavinbunney/kubectl`.

Well for now I only have one module.

> [@apparentlymart](#):
>
> For backward compatibility with modules that were written before Terraform supported third-party providers Terraform assumes that any undeclared dependency is for an official provider (in the `hashicorp/` namespace), but modern Terraform modules should always specify all of the providers they depend on.

The affected dependency seems clearly - at least based on the error - the one I explicitly declared.

---

<div class="post-metadata">

**Author:** ![papanito](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/papanito/32/19476_2.png) [@papanito](https://discuss.hashicorp.com/u/papanito)\
**Post date:** [December 8, 2022, 3:15pm UTC](https://discuss.hashicorp.com/t/module-tries-to-instantiate-wrong-provider-failed-to-instantiate-provider-xxx-to-obtain-schema-unavailable-provider/47716/6 "2022-12-08T15:15:34Z")

</div>

In meanwhile I got it working. I am not entirely sure what fixed the issues, cause the `backend.tf` did not change. Maybe related to my tries to do a `terraform init -reconfigure` and/or `terraform init -upgrade`.

I may post more details once I understand what changed

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [December 8, 2022, 5:04pm UTC](https://discuss.hashicorp.com/t/module-tries-to-instantiate-wrong-provider-failed-to-instantiate-provider-xxx-to-obtain-schema-unavailable-provider/47716/7 "2022-12-08T17:04:49Z")

</div>

In the filesystem tree you shared there are three modules:

- `modules/monitoring`
- `subdir1`
- `subdir2`

When I say “all modules” I mean all three of those, and any other directories that have `.tf` files in them. Any directory with at least one `.tf` file is a Terraform module.

---

<div class="post-metadata">

**Author:** ![papanito](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/papanito/32/19476_2.png) [@papanito](https://discuss.hashicorp.com/u/papanito)\
**Post date:** [December 9, 2022, 8:44am UTC](https://discuss.hashicorp.com/t/module-tries-to-instantiate-wrong-provider-failed-to-instantiate-provider-xxx-to-obtain-schema-unavailable-provider/47716/8 "2022-12-09T08:44:23Z")

</div>

> [@apparentlymart](#):
>
> When I say “all modules” I mean all three of those, and any other directories that have `.tf` files in them. Any directory with at least one `.tf` file is a Terraform module.

Got it. They all have the providers properly defined. I initially had everything in each module, and then moved “monitoring” to a reusable module.
