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

如何通过Azure Bicep将单个API关联至多个API产品?

将API关联到多个APIM产品的Bicep解决方案

问题说明

你通过循环创建了多个APIM产品,但在尝试将API与这些产品关联时,因Product是资源数组无法直接作为单个父资源使用,导致代码运行失败。

解决方案

要实现API与多个产品的关联,需通过for循环遍历产品数组,为每个产品单独创建API关联资源。同时需先修正产品创建代码中的拼写错误。

修正后的完整api.bicep代码

// 定义API资源
resource apiDefinition 'Microsoft.ApiManagement/service/apis@2020-12-01' = {
  name: apiName
  parent: apimService
  properties: {
    path: apiName
    description: '${apiName} API'
    displayName: '${apiName} API'
    format: 'openapi+json'
    value: openapi
    subscriptionRequired: true
    type: 'http'
    protocols: [ 'https' ]
  }
}

// 循环创建多个产品(修正拼写错误:prodcuct → product)
resource Product 'Microsoft.ApiManagement/service/products@2020-12-01' = [for product in products: {
  name: product
  parent: apimService
  properties: {
    approvalRequired: true
    description: 'product for ${apiName} apis'
    displayName: '${product}-Product'
    state: 'published'
    subscriptionRequired: true
  }
}]

// 循环为每个产品创建API关联
resource apiProductLink 'Microsoft.ApiManagement/service/products/apis@2020-12-01' = [for (product, index) in products: {
  name: apiDefinition.name
  parent: Product[index] // 指向对应索引的产品资源
}]

关键要点

  1. 修正拼写错误:原代码中name: prodcuct是笔误,需改为name: product,否则产品资源无法正常创建。
  2. 循环创建关联资源:通过[for (product, index) in products]遍历产品数组,每个迭代中引用对应索引的Product资源作为父资源,为每个产品生成独立的API关联。
  3. 关联资源命名:关联资源的name需设置为API的名称(即apiDefinition.name),符合APIM资源的层级命名规则。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 00:15:22