Terraform 遍历值
Terraform iterate through values
新手问题:有什么方法可以将变量循环存储在提到的代码中吗?。下面是我的配置变量。
config= [{
"name" = shared_preload_libraries,
"values" = ["EXAMPLE1", "EXAMPLE2"]
},
{
"name" = "azure.extensions"
"values" = ["EXAMPLE1", "EXAMPLE2", "EXAMPLE3"]
}]
我需要以这样一种方式进行迭代,对于变量中的每个名称,相应的值应该在下面的代码中一一插入。
resource "azurerm_postgresql_flexible_server_configuration" "example" {
name = name
server_id = azurerm_postgresql_flexible_server.example.id
value = values
}
您必须按如下方式展平您的config
:
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
}
}
]...) # do NOT remove the dots
}
然后
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
}
新手问题:有什么方法可以将变量循环存储在提到的代码中吗?。下面是我的配置变量。
config= [{
"name" = shared_preload_libraries,
"values" = ["EXAMPLE1", "EXAMPLE2"]
},
{
"name" = "azure.extensions"
"values" = ["EXAMPLE1", "EXAMPLE2", "EXAMPLE3"]
}]
我需要以这样一种方式进行迭代,对于变量中的每个名称,相应的值应该在下面的代码中一一插入。
resource "azurerm_postgresql_flexible_server_configuration" "example" {
name = name
server_id = azurerm_postgresql_flexible_server.example.id
value = values
}
您必须按如下方式展平您的config
:
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
}
}
]...) # do NOT remove the dots
}
然后
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
}