terraform 中的提供者变量是否可能?

Is provider variable possible in terraform?

是否仍然无法在 Terraform v0.12.6 中使用变量提供程序?在 *.tfvars 我有列表变量 supplier

supplier = ["azurerm.core-prod","azurerm.core-nonprod"]

和 provider.tf 中定义的提供商:

provider "azurerm" {
  ...
  alias           = "core-prod"
}

provider "azurerm" {
  ...
  alias = "core-nonprod"

然后我想在 *.tf 中引用它。下面的示例是 'data',但同样适用于 'resource'

data "azurerm_public_ip" "pip" {
  count = "${var.count}"
   ....
   provider = "${var.supplier[count.index]}"
} 

有什么解决方法? 错误:提供程序配置引用无效,表明提供程序参数需要提供程序类型名称,可以选择后跟句点,然后是配置别名

无法将资源与提供者动态关联。类似于在静态类型的编程语言中,您通常无法在运行时动态切换特定符号以引用不同的库,Terraform 需要在表达式评估成为可能之前将资源块绑定到提供程序配置。

可以做的是编写一个模块,期望从​​其调用者接收提供者配置,然后静态地select每个模块实例的提供者配置:

provider "azurerm" {
  # ...

  alias = "core-prod"
}

module "provider-agnostic-example" {
  source = "./modules/provider-agnostic-example"

  providers = {
    # This means that the default configuration for "azurerm" in the
    # child module is the same as the "core-prod" alias configuration
    # in this parent module.
    azurerm = azurerm.core-prod
  }
}

在这种情况下,模块 本身 与提供者无关,因此它可以在您的生产和非生产设置中使用,但模块的任何特定用途都必须指定它是为了什么。

一种常见的方法是为每个环境单独配置,共享模块代表环境的任何共同特征,但也有机会代表它们之间可能需要存在的任何差异。在最简单的情况下,这可能只是两个配置,仅由一个 module 块和一个 provider 块组成,每个块都有一些不同的参数代表该环境的配置,以及共享模块包含所有 resourcedata 块。在更复杂的系统中,可能会使用 module composition techniques.

将多个模块集成在一起