Terraform - 使用 for_each 和元组中的字符串列表
Terraform - Use for_each with a list of strings inside tuple
我正在尝试创建多个数据工厂链接服务,一个帐户内的每个数据库一个。
variables.tf
variable "cosmosdbs" {
type = any
}
vars.tfvars
cosmosdbs = [
{
name = "account1"
resource_group = "group-name1"
databases = ["database"]
},
{
name = "account2"
resource_group = "group-name2"
databases = ["database1","database2","database3"]
}
]
main.tf
data "azurerm_cosmosdb_account" "cosmosdbs" {
for_each = {for r in var.cosmosdbs: r.name => r}
name = each.key
resource_group_name = each.value.resource_group
}
module "linked_cosmosdbs" {
source = "../"
for_each = {for r in var.cosmosdbs: r.name => r}
name = each.value.databases
resource_group_name = module.rg.name
data_factory_id = module.adf.id
description = "Connection to CosmosDB (Terraformed)"
integration_runtime_name = local.resource_name
account_endpoint = data.azurerm_cosmosdb_account.cosmosdbs[each.key].endpoint
account_key = data.azurerm_cosmosdb_account.cosmosdbs[each.key].primary_key
database = each.value.databases
}
显然这行不通,因为我想遍历元组内的每个映射,并为每个数据库创建一个属于每个帐户的链接服务。
您必须展平您的变量,例如:
locals {
cosmosdbs_flat = merge([
for val in var.cosmosdbs: {
for database in val["databases"]:
"${val.name}-${database}" => {
name = val.name
resource_group = val.resource_group
database = database
}
}
]...) # please do NOT remove the dots
}
然后
module "linked_cosmosdbs" {
source = "../"
for_each = local.cosmosdbs_flat
name = each.value.database
resource_group_name = module.rg.name
data_factory_id = module.adf.id
description = "Connection to CosmosDB (Terraformed)"
integration_runtime_name = local.resource_name
account_endpoint = data.azurerm_cosmosdb_account.cosmosdbs[each.value.name].endpoint
account_key = data.azurerm_cosmosdb_account.cosmosdbs[each.value.name].primary_key
database = each.value.database
}
我正在尝试创建多个数据工厂链接服务,一个帐户内的每个数据库一个。
variables.tf
variable "cosmosdbs" {
type = any
}
vars.tfvars
cosmosdbs = [
{
name = "account1"
resource_group = "group-name1"
databases = ["database"]
},
{
name = "account2"
resource_group = "group-name2"
databases = ["database1","database2","database3"]
}
]
main.tf
data "azurerm_cosmosdb_account" "cosmosdbs" {
for_each = {for r in var.cosmosdbs: r.name => r}
name = each.key
resource_group_name = each.value.resource_group
}
module "linked_cosmosdbs" {
source = "../"
for_each = {for r in var.cosmosdbs: r.name => r}
name = each.value.databases
resource_group_name = module.rg.name
data_factory_id = module.adf.id
description = "Connection to CosmosDB (Terraformed)"
integration_runtime_name = local.resource_name
account_endpoint = data.azurerm_cosmosdb_account.cosmosdbs[each.key].endpoint
account_key = data.azurerm_cosmosdb_account.cosmosdbs[each.key].primary_key
database = each.value.databases
}
显然这行不通,因为我想遍历元组内的每个映射,并为每个数据库创建一个属于每个帐户的链接服务。
您必须展平您的变量,例如:
locals {
cosmosdbs_flat = merge([
for val in var.cosmosdbs: {
for database in val["databases"]:
"${val.name}-${database}" => {
name = val.name
resource_group = val.resource_group
database = database
}
}
]...) # please do NOT remove the dots
}
然后
module "linked_cosmosdbs" {
source = "../"
for_each = local.cosmosdbs_flat
name = each.value.database
resource_group_name = module.rg.name
data_factory_id = module.adf.id
description = "Connection to CosmosDB (Terraformed)"
integration_runtime_name = local.resource_name
account_endpoint = data.azurerm_cosmosdb_account.cosmosdbs[each.value.name].endpoint
account_key = data.azurerm_cosmosdb_account.cosmosdbs[each.value.name].primary_key
database = each.value.database
}