You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform通过azurerm_api_management_api动态创建APIM API报错求助

动态创建Azure APIM API的Terraform配置问题与解决方法

问题概述

通过Terraform的azurerm_api_management_api资源,从local.tf动态传入name、display_name等值批量创建APIM API时,触发"Unsupported block type"错误,提示不支持apiValues动态块。需要实现仅维护API规格与名称,后续可部署到Azure Pipeline的方案。

报错信息

Error: Unsupported block type
│
│   on api-management\api_management_api.tf line 6, in resource "azurerm_api_management_api" "apim_api":
│    6:   dynamic apiValues{
│
│ Blocks of type "apiValues" are not expected here.

原配置代码

资源配置代码

resource "azurerm_api_management_api" "apim_api" {
    revision            = "1"
    resource_group_name = var.resource_group_name
    api_management_name = azurerm_api_management.apim.name

  dynamic apiValues{
    for_each = local.apiDetails
    content{
        name                = apiValues.value.name
        display_name        = apiValues.value.display_name
        path                = ""
        protocols           = ["http","https"]
        service_url         = "http://spring-boot-redis.azurewebsites.net"
        import {
            content_format = "openapi-link"
            content_value  = "./SpringBootRedis.yaml"
  }
    }
  }
}

locals.tf代码

locals {
  apiDetails = [
    {
      name = "spring-boot-redis"
      display_name = "Spring Boot Redis"
    }
  ]
}

解决方案

错误原因是azurerm_api_management_api资源不支持自定义的apiValues动态块,正确的批量创建方式是对资源本身使用for_each参数,遍历本地定义的API列表生成多个API实例。

修改后的配置代码

资源配置代码

resource "azurerm_api_management_api" "apim_api" {
  for_each            = { for api in local.apiDetails : api.name => api }
  revision            = "1"
  resource_group_name = var.resource_group_name
  api_management_name = azurerm_api_management.apim.name

  name                = each.value.name
  display_name        = each.value.display_name
  path                = ""
  protocols           = ["http", "https"]
  service_url         = "http://spring-boot-redis.azurewebsites.net"

  import {
    content_format = "openapi-link"
    content_value  = "./SpringBootRedis.yaml"
  }
}

locals.tf代码(可按需扩展)

locals {
  apiDetails = [
    {
      name = "spring-boot-redis"
      display_name = "Spring Boot Redis"
    }
    # 新增API只需在此添加对象即可
  ]
}

关键说明

  • 用for_each遍历local.apiDetails,以API的name作为唯一标识键,确保每个API实例的唯一性
  • 动态字段通过each.value引用,固定配置(如revision、协议、OpenAPI导入规则等)直接定义在资源块内
  • 后续维护仅需修改local.apiDetails中的API列表,无需调整资源结构,适配Azure Pipeline的自动化部署需求

内容的提问来源于stack exchange,提问作者Satya Panigrahy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 20:20:26