Home > Back-end >  need to include value in terraform or use multiple loops to store variable
need to include value in terraform or use multiple loops to store variable

Time:02-23

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

i would like to have the ${val[0]} inside service_url service_url = "https://${(xxxx))}.com", ie https://dev.com, https://qa.com

CodePudding user response:

I think in your case it would be easier to re-organize the for_each, from list to 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"]
}
  • Related