Reputation: 153
When writing examples for my Terraform module, I got the error: "Module contains provider configuration" "Providers cannot be configured within modules using count, for_each or depends_on."
I got this error when I tried to add a depends_on
block to the module's declaration to avoid trying to run the module plan before the creation of the resource group needed to deploy the resources inside the module.
If I don't add the depends_on
block it also breaks, because it can't find the declared resource group that should be created before the module runs to populate the required resource group data source.
I find it is at least uncomfortable to require the removal of the providers
block or to remove all the data sources.
I couldn't find any details on this error, or on how to fix it.
Specific line that raises this error inside Terraform's code.
Upvotes: 11
Views: 17578
Reputation: 16574
Because the creators made it that way
In my case I required to wait until the parent module completion to then lookup a resource (created in the parent) in the child
The child module should not contain provider definition
In my case my modules before the fix were
## Parent (parent.tf)
module "the-parent" {
source = "../my-child"
}
## Child (child.tf)
provider "azuread" {
baz = true
}
module "my-child" {
foo = "bar"
}
The fix was move the provider from the child to the parent
## Parent (parent.tf)
provider "azuread" {
baz = true
}
module "the-parent" {
source = "../my-child"
}
## Child (child.tf)
module "my-child" {
foo = "bar"
}
To fix, the child will be free of any provider {...}
In my case child has terraform. required_providers {...} but not provider {...}
Error: Module is incompatible with count, for_each, and depends_on
on terraform.tf line 666, in module "my-child":
666: depends_on = [module.my-parent]
The module at module.my-child is a legacy module which contains its own
local provider configurations, and so calls to it may not use the count,
for_each, or depends_on arguments.
If you also control the module "../modules/my-child", consider updating
this module to instead expect provider configurations to be passed by its
caller.
Upvotes: 0
Reputation: 837
What you have probably looks similar to code below, right?
Root module (eg. terraform.tf):
---
... some code...
module "child_module" {
count = var.children_no
source = "./modules/childmodule.tf"
...
}
and
Child module (childmodule.tf)
---
... some code ...
provider "any_provider" {
...
}
So... the sad thing is... you can not do that. If module have any provider at all, you can not 'count' it. :/
That's basically what the error said. You either have to get your provider out of module or get out count
from root module.
Upvotes: 1
Reputation: 51
From the error message, it sounds like there is a Provider conflict. This can occur when a module is called using a different provider to that declared in the parent.
Example
.
├── main.tf
├── outputs.tf
├── provider.tf
├── runtime.yaml
└── variables.tf
If in the above provider it declares the following:
provider "google" {
project = var.gcp_project_id
region = var.gcp_region
zone = var.gcp_zone
}
provider "google-beta" {
project = var.gcp_project_id
region = var.gcp_region
zone = var.gcp_zone
}
If you then reference a module, the expectation is the module references the above parent Provider configuration. If the module declares their own configuration and it differs from the above, Terraform will report an error.
To fix the issue you should respect the parent Provider. Remove the provider declaration from the module and then the dependency should then be compatible.
Also note when you update the providers - you will need to perform a terraform init
. I removed the pre-existing Terraform hidden directories to ensure it correctly picks up the new settings.
Upvotes: 3