gpt4 book ai didi

Terraform 0.13 - 模块、for_each 和提供者

转载 作者:行者123 更新时间:2023-12-05 03:49:34 24 4
gpt4 key购买 nike

已更新

我正在尝试使用 Terraform 在 Azure 中配置多个 SQL 数据库。

我的子模块具有以下配置 SQL 数据库的代码:

providers.tf

// default provider
provider "azurerm" {
alias = "main"
features {}
}

// The provider that can access the storage account to store diagnostics
provider "azurerm" {
alias = "storage_account"
features {}
}

sql_db.tf

resource "azurerm_mssql_database" "default" {
name = var.name
base_name = var.base_name
...
tags = var.tags

provider = azurerm.main
}

数据.tf

data "azurerm_storage_account" "storage" {
name = var.storage_account_name
resource_group_name = var.storage_account_rg
provider = azurerm.storage_account
}

我在我的 ma​​in.tf 文件中调用这个模块如下,我想使用 for_each 提供多个 SQL 数据库:

module "sql_db" {
for_each = var.sql_db

source = "...../sql_db.git"

base_name = each.value.base_name
name = each.value.name

providers = {
azurerm.main = azurerm.main
azurerm.storage_account = azurerm.storage_account
}
}

provider "azurerm" {
features {}
version = "=2.20.0"
}

// default provider
provider "azurerm" {
alias = "main"
features {}
}

provider "azurerm" {
alias = "storage_account"
features {}
}

当我运行计划时,出现以下错误:

Error: Module does not support for_each

on main.tf line 35, in module "sql_db":
35: for_each = var.sql_db

Module "sql_db" cannot be used with for_each because it contains a nested
provider configuration for "azurerm.main", at
.terraform\modules\sql_db\providers.tf:2,10-19.

This module can be made compatible with for_each by changing it to receive all
of its provider configurations from the calling module, by using the
"providers" argument in the calling module block.


Error: Module does not support for_each

on main.tf line 35, in module "sql_db":
35: for_each = var.sql_db

Module "sql_db" cannot be used with for_each because it contains a nested
provider configuration for "azurerm.storage_account", at
.terraform\modules\sql_db\providers.tf:8,10-19.

This module can be made compatible with for_each by changing it to receive all
of its provider configurations from the calling module, by using the
"providers" argument in the calling module block.

最佳答案

简单的回答是,它不受支持。来自Terraform documentation :

A module containing its own provider configurations is not compatible with the for_each, count, and depends_on arguments that were introduced in Terraform v0.13.

HashiCorp 绝对坚持永远不能动态声明提供者,这就是为什么他们不允许在 provider 中使用 for_each/count block ,也不是包含 provider block 的模块上的 for_each/count

关于Terraform 0.13 - 模块、for_each 和提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63915229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com