Azure / Azure/bicep

Problem with accessing module array members: function 'copyIndex' is not expected at this location

Open
#5,101 0 comments 0 reactions 1 assignee Claimed by @majastrz View on GitHub
bug top 10 committed
Dominant language
Bicep
Stars
3.6k
Forks
830
Avg merge
1d 21m
Merged PRs (30d)
79

Description

**Bicep version**
Bicep CLI version 0.4.1008 (223b8d227a)

**Describe the bug**
I expect to be able to access module array member by index and use their values in a module's property, but get an error.

**To Reproduce**
```bicep
targetScope = 'subscription'

var demoName = 'demo1'

resource lzRG 'Microsoft.Resources/resourceGroups@2021-04-01' = [for i in range(0,3): {
name: '${demoName}-lz${i}-rg'
location: 'westeurope'
}]

module vnet './../modules/vnet.bicep' = [for i in range(0,3): {
scope: lzRG[i]
name: 'vnet-deployment-${i}'
params: {
vnetName: '${demoName}-lz${i}-vnet'
}
}]

module vm '../modules/vm-linux.bicep' = {
scope: lzRG[1]
name: 'vm-deployment'
params: {
vmName: demoName
adminUsername: 'admin'
adminPasswordOrKey: 'pwd' //tbd
virtualNetworkName: vnet[1].outputs.name // <-- InvalidTemplate - Deployment template validation failed: 'The template resource 'vm-deployment' at line '1' and column '6040' is not valid: The template function 'copyIndex' is not expected at this location. The function can only be used in a resource with copy specified
}
}
```

Deploying with 'az deployment sub create -l westeurope -f .\try.bicep -c'
The error is
`InvalidTemplate - Deployment template validation failed: 'The template resource 'vm-deployment' at line '1' and column '6040' is not valid: The template function 'copyIndex' is not expected at this location. The function can only be used in a resource with copy specified. Please see https://aka.ms/arm-copy for usage details.. Please see https://aka.ms/arm-template-expressions for usage details.'.`

**Additional context**
The workaround is to fake a loop.
```bicep
targetScope = 'subscription'

var demoName = 'demo1'

resource lzRG 'Microsoft.Resources/resourceGroups@2021-04-01' = [for i in range(0,3): {
name: '${demoName}-lz${i}-rg'
location: 'westeurope'
}]

module vnet './../modules/vnet.bicep' = [for i in range(0,3): {
scope: lzRG[i]
name: 'vnet-deployment-${i}'
params: {
vnetName: '${demoName}-lz${i}-vnet'
}
}]

module vm '../modules/vm-linux.bicep' = [ for i in range(0,3): if (i == 1) {
scope: lzRG[i]
name: 'vm-deployment'
params: {
vmName: demoName
adminUsername: 'admin'
adminPasswordOrKey: 'pwd'
virtualNetworkName: vnet[i].outputs.name
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.