Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I unable to add a depends_on block to a module with provider configuration?

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.

like image 973
Leonardo Oliveira Avatar asked May 02 '21 22:05

Leonardo Oliveira


People also ask

How do I pass a provider in module Terraform?

Providers can be passed down to descendent modules in two ways: either implicitly through inheritance, or explicitly via the providers argument within a module block.

Is provider configuration block is mandatory in every configuration?

A provider configuration block is required in every Terraform configuration.

Is Provider block mandatory in Terraform?

Each Terraform module must declare which providers it requires, so that Terraform can install and use them. Provider requirements are declared in a required_providers block.

Can multiple Terraform providers be used within a single Terraform configuration file?

The fact that Terraform is not tied to a specific infrastructure or cloud provider makes it a powerful tool in multi-provider deployments. You are able to manage all resources using the same set of configuration files, sharing variables or defining dependencies between resources across providers.


Video Answer


1 Answers

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.

like image 88
ravenwing Avatar answered Oct 30 '22 07:10

ravenwing