需要在 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-organizefor_eachlistmap 会更容易:

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"]
}