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

使用Bicep部署Azure容器注册表时遇数组索引越界错误求助

问题排查:ACR部署输出数组索引越界错误

错误现象

  • 单个ACR资源acrprccivivek00创建正常,但批量循环创建的ACR对应的输出ctrName触发部署失败,提示数组索引4越界,错误码DeploymentOutputEvaluationFailed。

错误详情

6:30:16 AM - The deployment 'ContainerRegistriesDeployment' failed with error(s).
Showing 1 out of 1 error(s).
Status Message: The template output 'ctrName' is not valid:
The language expression property array index '4' is out of bounds.. (Code:DeploymentOutputEvaluationFailed)

部署命令

New-AzResourceGroupDeployment -Name 'ContainerRegistriesDeployment' 
-TemplateFile 'D:\LearnBicep\outputkeyword.bicep' 
-TemplateParameterFile 'D:\LearnBicep\allowedkeyword_arraysdemo.parameters.json' 
-ResourceGroupName 'vivekchak-rg' -Mode Incremental

Bicep模板代码

@allowed([
  'prod'
  'dev'
])
param environmentType string

var containerRegistrySku = (environmentType == 'prod') ? 'Premium' : 'Standard'

resource containerRegistry 'Microsoft.ContainerRegistry/registries@2023-07-01' = {
  name: 'acrprccivivek00'
  location: 'centralindia'
  sku: {
    name: containerRegistrySku
  }
}

output ctrproperties string = containerRegistry.id

resource containerRegistry1 'Microsoft.ContainerRegistry/registries@2023-07-01' = [ for i in range(1,4) : {
  name: 'acrprccivivek0${i}'
  location: 'centralindia'
  sku: {
    name: containerRegistrySku
  }
}]

output ctrName array = [
  for i in range(1,4) : containerRegistry1[i].name
]

参数文件内容

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "environmentType": {
      "value": "dev"
    }
  }
}

错误原因

Bicep中range(1,4)生成的是包含[1,2,3]的数组,循环创建的containerRegistry1资源数组采用0起始索引:

  • containerRegistry1[0]对应循环变量i=1的资源
  • containerRegistry1[1]对应i=2的资源
  • containerRegistry1[2]对应i=3的资源

但输出循环中直接用containerRegistry1[i].name,当i=3时,尝试访问索引3,而数组仅包含0、1、2三个有效索引,直接触发越界错误。

修复方案

以下三种方式任选其一即可:

方式1:遍历资源数组直接取值

最简洁的写法,直接遍历创建好的资源数组:

output ctrName array = [
  for registry in containerRegistry1 : registry.name
]

方式2:使用数组索引遍历

通过数组长度生成索引范围,匹配资源数组的0起始索引:

output ctrName array = [
  for idx in range(0, length(containerRegistry1)) : containerRegistry1[idx].name
]

方式3:调整索引偏移量

保持原循环变量的同时,通过i-1将1起始的循环变量转换为0起始的数组索引:

output ctrName array = [
  for i in range(1,4) : containerRegistry1[i-1].name
]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 05:13:24