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

Bicep中替代ARM JSON的copyIndex()及Azure防火墙DNAT规则转换

Bicep替代ARM copyIndex()生成序列命名方案
  • 在Bicep中无需依赖转译工具,直接通过for循环结合索引变量即可实现序列命名效果,示例如下:
    // 生成指定数量的存储账户,命名为storage0、storage1...
    param storageCount int = 2
    
    resource storageAccounts 'Microsoft.Storage/storageAccounts@2023-01-01' = [for index in range(0, storageCount): {
      name: 'storage${index}'
      location: resourceGroup().location
      sku: {
        name: 'Standard_LRS'
      }
      kind: 'StorageV2'
    }]
    
    这里通过range(0, storageCount)生成连续索引,用字符串插值storage${index}拼接命名,比ARM的concat('storage', string(copyIndex()))更简洁直观。
Azure防火墙DNAT规则集合destinationAddresses转Bicep代码

假设原ARM JSON中destinationAddresses片段如下:

"rules": [
  {
    "name": "dnat-rule-1",
    "properties": {
      "destinationAddresses": ["10.1.0.4", "10.1.0.5"],
      // 其他规则属性...
    }
  }
]

对应的Bicep代码中,destinationAddresses直接以数组形式定义即可,若需动态生成也可结合参数或变量:

resource firewallPolicy 'Microsoft.Network/firewallPolicies@2023-05-01' = {
  name: 'fw-policy'
  location: resourceGroup().location
  properties: {
    ruleCollections: [
      {
        name: 'dnat-collection'
        properties: {
          ruleCollectionType: 'FirewallPolicyNatRuleCollection'
          priority: 100
          rules: [
            {
              name: 'dnat-rule-1'
              properties: {
                sourceAddresses: ['*']
                // 直接转换为Bicep数组格式
                destinationAddresses: ['10.1.0.4', '10.1.0.5']
                destinationPorts: ['80']
                translatedAddress: '192.168.1.10'
                translatedPort: '8080'
                ipProtocols: ['TCP']
              }
            }
          ]
        }
      }
    ]
  }
}

若需动态传入地址列表,可通过参数实现:

param destinationAddresses array = ['10.1.0.4', '10.1.0.5']

// 在规则中直接引用参数数组
destinationAddresses: destinationAddresses

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 12:17:46