在main.bicep中为Azure流量管理器添加子资源类型端点的实现方法
实现方案
Bicep完全支持Microsoft.Network/trafficmanagerprofiles/endpoints子资源独立声明,你没有触发自动补全是因为使用了不存在的测试版API版本,改用稳定的2018-08-01版本即可,具体实现步骤如下:
- 调整现有模块输出
- 首先修改
trafficManagerProfiles.bicep,新增流量管理器资源的输出供父模板引用:output trafficManagerProfileId string = trafficManagerProfile.id output trafficManagerProfileName string = trafficManagerProfile.name - 其次修改
functions.bicep,将批量创建的Function App核心属性以数组形式输出:// 假设你模块内的Function App资源定义在functionApp数组变量/资源中 output functionApps array = [for app in functionApp: { id: app.id name: app.name defaultHostName: app.properties.defaultHostName location: app.location }]
- 首先修改
- 在
main.bicep中独立声明端点子资源
直接通过Bicep循环批量创建所有Function App对应的流量管理器端点,不需要修改原有流量管理器模块的内部逻辑,也不需要额外传参:// 原有流量管理器模块调用 module trafficManagerModule 'Network/trafficManagerProfiles.bicep' = { name: 'trafficManagerModule' params: { systemName: systemName environmentName: environmentName } } // 原有函数模块调用 module functionAppModule 'Web/functions.bicep' = { dependsOn: [ appServicePlanModule webApiStorageAccount ] name: 'functionAppModule' params: { environmentName: environmentName systemName: systemName azureRegion: azureRegion appServicePlanId: appServicePlanModule.outputs.id } } // 批量创建流量管理器端点 resource tmEndpoints 'Microsoft.Network/trafficManagerProfiles/endpoints@2018-08-01' = [for (funcApp, idx) in functionAppModule.outputs.functionApps: { parent: trafficManagerModule // 通过parent关键字绑定父资源,无需手动拼接名称 name: 'func-endpoint-${funcApp.name}' // 保证端点名称唯一即可 properties: { endpointStatus: 'Enabled' targetResourceId: funcApp.id target: funcApp.defaultHostName endpointLocation: funcApp.location weight: 1 priority: 1 // 其他属性按需配置即可 } }]
注意:如果你的流量管理器使用加权、优先级等特殊路由规则,按需调整weight和priority的取值即可。
内容的提问来源于stack exchange,提问作者Jan_V
相关产品推荐
相关产品推荐

