如何使用 Terraform for_each 资源块和计算资源块
How to use Terraform for_each resource block and count resource block
我有一个 azurerm_postgresql_flexible_server 资源使用 count 和 azurerm_postgresql_flexible_server_configuration 使用 for each。请找到以下代码。
config= [{
"name" = shared_preload_libraries,
"values" = ["EXAMPLE1", "EXAMPLE2"]
},
{
"name" = "azure.extensions"
"values" = ["EXAMPLE1", "EXAMPLE2", "EXAMPLE3"]
}]
locals {
flat_config = merge([
for single_config in var.config: {
for value in single_config.values:
"${single_config.name}-${value}" => {
"name" = single_config.name
"value" = value
}
}
]...)
}
下面是我的 for_each 资源
resource "azurerm_postgresql_flexible_server_configuration" "example" {
for_each = local.flat_config
name = each.value.name
server_id = azurerm_postgresql_flexible_server.example.id
value = each.value.value
}
目前我有两个 azurerm_postgresql_flexible_server 资源。 azurerm_postgresql_flexible_server.example[0] 和 azurerm_postgresql_flexible_server.example[1]。能否请您告诉我是否有可能包含一些替代选项,例如计数?
Splat 表达式无效。
如果您想为 azurerm_postgresql_flexible_server_configuration
的每个实例应用您的两个 azurerm_postgresql_flexible_server
,您还需要一个 额外的扁平化级别:
locals {
flat_config = merge(flatten([
for single_config in var.config: [
for value in single_config.values: {
for idx, server in azurerm_postgresql_flexible_server.example:
"${single_config.name}-${value}-${idx}" => {
"name" = single_config.name
"value" = value
"flexible_server" = server
}
}
]
])...)
}
然后
resource "azurerm_postgresql_flexible_server_configuration" "example" {
for_each = local.flat_config
name = each.value.name
server_id = each.value.flexible_server.id
value = each.value.value
}
我有一个 azurerm_postgresql_flexible_server 资源使用 count 和 azurerm_postgresql_flexible_server_configuration 使用 for each。请找到以下代码。
config= [{
"name" = shared_preload_libraries,
"values" = ["EXAMPLE1", "EXAMPLE2"]
},
{
"name" = "azure.extensions"
"values" = ["EXAMPLE1", "EXAMPLE2", "EXAMPLE3"]
}]
locals {
flat_config = merge([
for single_config in var.config: {
for value in single_config.values:
"${single_config.name}-${value}" => {
"name" = single_config.name
"value" = value
}
}
]...)
}
下面是我的 for_each 资源
resource "azurerm_postgresql_flexible_server_configuration" "example" {
for_each = local.flat_config
name = each.value.name
server_id = azurerm_postgresql_flexible_server.example.id
value = each.value.value
}
目前我有两个 azurerm_postgresql_flexible_server 资源。 azurerm_postgresql_flexible_server.example[0] 和 azurerm_postgresql_flexible_server.example[1]。能否请您告诉我是否有可能包含一些替代选项,例如计数?
Splat 表达式无效。
如果您想为 azurerm_postgresql_flexible_server_configuration
的每个实例应用您的两个 azurerm_postgresql_flexible_server
,您还需要一个 额外的扁平化级别:
locals {
flat_config = merge(flatten([
for single_config in var.config: [
for value in single_config.values: {
for idx, server in azurerm_postgresql_flexible_server.example:
"${single_config.name}-${value}-${idx}" => {
"name" = single_config.name
"value" = value
"flexible_server" = server
}
}
]
])...)
}
然后
resource "azurerm_postgresql_flexible_server_configuration" "example" {
for_each = local.flat_config
name = each.value.name
server_id = each.value.flexible_server.id
value = each.value.value
}