最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

azure - Allow injecting different provider aliases into a custom terraform module - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Feb 18 at 3:17 Vinay B 2,5312 gold badges3 silver badges12 bronze badges Recognized by Microsoft Azure Collective asked Feb 3 at 15:07 ccoutinhoccoutinho 4,6266 gold badges50 silver badges57 bronze badges 4
  • What's the point in using a module if you want to inject a different provider? – Paolo Commented Feb 3 at 15:29
  • Sometimes I use that module within the same subscription as the other resources, so provider injection is not required. But sometimes I need to declare a provider alias because the resources provisioned by the module target a different subscription. – ccoutinho Commented Feb 3 at 15:37
  • Yes this is the right approach according to the official docs: developer.hashicorp/terraform/language/providers/…. The unrelated warning message is also correct that you should generally declare required providers in your terraform block. – Matthew Schuchard Commented Feb 3 at 15:41
  • @MatthewSchuchard I followed your link up to developer.hashicorp/terraform/language/meta-arguments/…, where it is mentioned that, on module declaration: "if you specify a providers argument, it cancels this default behavior, and the child module will only have access to the provider configurations you specify". But it isn't said that the child module should declare a required_providers block! – ccoutinho Commented Feb 3 at 15:55
Add a comment  | 

1 Answer 1

Reset to default 1

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.

发布评论

评论列表(0)

  1. 暂无评论