I have developed the my_azurerm_module
terraform module and I need to inject a different provider alias - my_alias
- when declaring it, in some cases (like below):
module "my_azurerm_module" {
source = "../modules/my-azurerm-module"
providers = {
azurerm = azurerm.my_alias
}
(...)
}
Whenever I had to inject the provider, I started having a Warning:
There is no explicit declaration for local provider name "azurerm" in │ module.dns_txt_record_website, so Terraform is assuming you mean to pass a │ configuration for "hashicorp/azurerm".
I was able to suppress the Warning by declaring azurerm
as a required provider within the module:
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
}
}
}
Is this the right approach to achieve what I am trying to achieve? Why?
Terraform's heuristic of guessing that you meant to use a provider in the hashicorp/
namespace when you don't include required_providers
is intended as a backward-compatibility mechanism for modules that were written for Terraform v0.12 and earlier, which only supported HashiCorp-published providers.
Because of that, various features that were added after Terraform v0.13 either require or strongly encourage you to write a required_providers
block so that it's clear to a reader of your module exactly which provider the module is intended to work with.
The mechanism for explicitly passing provider configurations between modules was refined in a version of Terraform after v0.12, and so when you use that Terraform assumes you are intending to write a module that doesn't need to be compatible with older Terraform versions (since they would understand this syntax somewhat differently) and so uses this warning to encourage you to adopt the modern practice of explicitly declaring your module's provider dependencies.
Although it's not immediately relevant to your situation, another example of such a change is that Terraform v1.8's support for provider-defined functions (that is: functions that are implemented as part of a provider plugin, rather than in Terraform Core) also requires that you include a required_providers
block for each provider if you intend to call any of its functions. This is, again, an example of a feature whose use makes your module incompatible with Terraform v0.12 and therefore the backward-compatibility concern no longer applies.
You can find out more about the required_providers
block in Provider Requirements.