VivekAnandChakravarthy
VivekAnandChakravarthy

Reputation: 677

The template output 'ctrName' is not valid: The language expression property array index '4' is out of bounds

Unable to understand the error during the deployment of Azure Container Registries using BICEP Template:

Error:

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)

PowerShell Command to deploy:

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

outputkeyword.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
  ]

allowedkeyword_arraysdemo.parameters.json

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

In the outputkeyword.bicep file, the first resource block is working good as it is creating the ACR acrprccivivek00 and it is failing on the 2nd resource block.

And I'm unable to understand what is the error in ctrName which is output array.

Upvotes: 0

Views: 46

Answers (2)

wenbo
wenbo

Reputation: 1451

First:

There are some things worth noting here, below sample output the different output count.

output o1 array = [for i in range(1,4): 'a${i}' ]

output o2 array = [for i in range(0,3): 'b${i}' ]

enter image description here

The laster Number 3 or 4 is not the last number in the array, but actually the numberOfElements, link

The range here is diff from Python language.

enter image description here


Second:

Back to you case, when you deploy containerRegistry1 with for loop, it will form an array whose length is the numberOfElements of range.

In output you should treat containerRegistry1 as an array, index it from 0, and specify the numberOfElements in range. (to avoid confusion, you d better use startIndex 0 in both resource and output.)

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

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

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

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

enter image description here

Upvotes: 0

Thomas
Thomas

Reputation: 29482

You need to start index at 0 if you want to get the list of containerRegistry1. To make it simpler to understand you could do something like that:

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

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

Upvotes: 1

Related Questions