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:LearnBicepoutputkeyword.bicep'
-TemplateParameterFile 'D:LearnBicepallowedkeyword_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.
2
Answers
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:
First:
There are some things worth noting here, below sample output the different output count.
The laster Number
3
or4
is not the last number in the array, but actually thenumberOfElements
, linkThe range here is diff from Python language.
Second:
Back to you case, when you deploy
containerRegistry1
with for loop, it will form an array whose length is thenumberOfElements
of range.In output you should treat
containerRegistry1
as an array, index it from0
, and specifythe numberOfElements
in range. (to avoid confusion, you d better use startIndex 0 in both resource and output.)