需要在 terraform 中包含值或使用多个循环来存储变量
need to include value in terraform or use multiple loops to store variable
env = ["dev", "qa"]
services = ["net", "go", "two", "three", "four", "five"]
resource "azurerm_api_management_api" "api" {
for_each = toset([for val in setproduct(var.env, var.services): "${val[0]}-${val[1]}"])
name = "${each.key}"
resource_group_name = azurerm_resource_group.xx.name
api_management_name = azurerm_api_management.xxx.name
revision = "1"
path = "${each.key}"
display_name = "${each.key}"
service_url = "https://${(xxxx))}.com"
protocols = ["https"]
}
我想在 service_url service_url = "https://${(xxxx))}.com 中加入 ${val[0]},即 https://dev.com, https://qa.com
我认为在你的情况下 re-organize 和 for_each
从 list
到 map
会更容易:
resource "azurerm_api_management_api" "api" {
for_each = {for val in setproduct(var.env, var.services): "${val[0]}-${val[1]}" => {env = val[0], service = val[1]} }
name = "${each.key}"
resource_group_name = azurerm_resource_group.xx.name
api_management_name = azurerm_api_management.xxx.name
revision = "1"
path = "${each.key}"
display_name = "${each.key}"
service_url = "https://${each.value.env}.com"
protocols = ["https"]
}
env = ["dev", "qa"]
services = ["net", "go", "two", "three", "four", "five"]
resource "azurerm_api_management_api" "api" {
for_each = toset([for val in setproduct(var.env, var.services): "${val[0]}-${val[1]}"])
name = "${each.key}"
resource_group_name = azurerm_resource_group.xx.name
api_management_name = azurerm_api_management.xxx.name
revision = "1"
path = "${each.key}"
display_name = "${each.key}"
service_url = "https://${(xxxx))}.com"
protocols = ["https"]
}
我想在 service_url service_url = "https://${(xxxx))}.com 中加入 ${val[0]},即 https://dev.com, https://qa.com
我认为在你的情况下 re-organize 和 for_each
从 list
到 map
会更容易:
resource "azurerm_api_management_api" "api" {
for_each = {for val in setproduct(var.env, var.services): "${val[0]}-${val[1]}" => {env = val[0], service = val[1]} }
name = "${each.key}"
resource_group_name = azurerm_resource_group.xx.name
api_management_name = azurerm_api_management.xxx.name
revision = "1"
path = "${each.key}"
display_name = "${each.key}"
service_url = "https://${each.value.env}.com"
protocols = ["https"]
}