无法对 azurerm_api_management_api 使用 terraform for_each 块

问题描述 投票:0回答:1

我有两个开放的 api 文档,我们称它们为 openapione.yaml,另一个是 openapitwo.yaml。现在我正在使用 for_each 块,但不知何故它不适用于我的 terraform 代码。我想要的只是它应该每次为单独的 openapi 文档选择各自的值。

    resource "azurerm_api_management_api" "sharatapimgmntapi" {
      for_each = var.apimanagement
      name                = each.value["name"]
      resource_group_name = azurerm_resource_group.windows-function-rg.name
      api_management_name = azurerm_api_management.apim.name
      revision            = each.value["revision"]
      display_name        = each.value["display_name"]
      path                = each.value["path"]
      protocols           = each.value["protocols"]
    
      import {
        content_format = each.value["content_format"]
        content_value  = each.value["content_value"]
    
      }
    
    }
    
    
Below is the variable file.
    
    variable "apimanagement" {
      type = map(any)
      default = {
        api_management_1 = {
          name = "flight-health"
          revision            = "1"
          display_name        = "flt-health"
          path                = "extranet/v1"
          protocols           = ["https"]
          content_format      = "openapi"
          content_value       = 'file("${path.module}/openapione.yaml")'
    
        }

        api_management_2 = {
          name = "availabilities"
          revision            = "2"
          display_name        = "avail"
          path                = "extranet/v1"
          protocols           = ["http"]
          content_format      = "openapi"
          content_value       = 'file("${path.module}/openapitwo.yaml")'
    
        }
      }
    }
azure terraform azure-api-management
1个回答
0
投票

您无法按照您想要的方式调用变量定义中的内置函数。可以使用以下代码修复此问题:

resource "azurerm_api_management_api" "sharatapimgmntapi" {
  for_each            = var.apimanagement
  name                = each.value["name"]
  resource_group_name = azurerm_resource_group.windows-function-rg.name
  api_management_name = azurerm_api_management.apim.name
  revision            = each.value["revision"]
  display_name        = each.value["display_name"]
  path                = each.value["path"]
  protocols           = each.value["protocols"]
    
  import {
    content_format = each.value["content_format"]
    content_value  = file("${path.module}/${each.value["content_value"]")
  }
}

并将变量修复为如下所示:

variable "apimanagement" {
  type = map(any)
  default = {
    api_management_1 = {
      name           = "flight-health"
      revision       = "1"
      display_name   = "flt-health"
      path           = "extranet/v1"
      protocols      = ["https"]
      content_format = "openapi"
      content_value  = "openapione.yaml"
    }
    api_management_2 = {
      name           = "availabilities"
      revision       = "2"
      display_name   = "avail"
      path           = "extranet/v1"
      protocols      = ["http"]
      content_format = "openapi"
      content_value  = "openapitwo.yaml"
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.