【发布时间】:2021-01-03 00:01:16
【问题描述】:
更新
我正在尝试使用 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
}
data.tf
data "azurerm_storage_account" "storage" {
name = var.storage_account_name
resource_group_name = var.storage_account_rg
provider = azurerm.storage_account
}
我在我的 main.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.
【问题讨论】:
-
var.sql_db的结构是什么? -
您在模块中定义了两个提供程序,但只从根模块传入其中一个模块。该错误抱怨它没有从调用模块接收所有提供程序配置。是否还有什么阻止您传入存储帐户提供程序?
-
我会从
resource "azurerm_mssql_database" "default"中删除provider = azurerm.main并设置为module.sql_db.providers={ azurerm = azurerm.main }。除非您出于不同原因将其他提供程序传递给同一模块。 -
我已经用更多细节更新了这个问题。我实际上在子模块中使用了多个 azurerm 提供程序。
标签: terraform terraform-provider-azure