Referencing items from other resources in loops (copy index) #2034
-
Does anyone know if it's possible to reference another resource that's looped over an array, which then allows a parameter field to be dynamically generated? Looking at my reference code below; essentially I'm dynamically building out both resources via loops (copy indexing), and I would like to reference one field from the other, param p_vnetObject array
param p_routeTables array
resource myRoutes 'Microsoft.Network/routeTables@2020-06-01' = [for item in p_routeTables: {
name : item.name
location : resourceGroup().location
properties : {
routes : [for subitem in item.routes: {
name : subitem.name
properties: subitem.properties
}]
}
}]
resource vnet 'Microsoft.Network/virtualNetworks@2020-08-01' = [for (item, i) in p_vnetObject: if (item.deploy == true) {
name : '${item.name}${i}'
location : resourceGroup().location
properties : {
addressSpace : {
addressPrefixes : item.cidr
}
subnets : [for subitem in item.subnets: {
name : subitem.name
properties : {
addressPrefix: subitem.addressPrefix
routeTable: {
id: myRoutes[].name == 'asdf'
}
}
}]
}
}] |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
You can use the local loop's index I feel like something is missing from the expression at: routeTable: {
id: myRoutes[].name == 'asdf'
}
|
Beta Was this translation helpful? Give feedback.
You can use the local loop's index
i
in themyRoutes[i].name
expression. Ifi
exceeds the range of themyRoutes
array, you will get a failure at deployment time, though.I feel like something is missing from the expression at:
id
properties are usually strings and that expression above returns abool
type. Are you trying to conditionally set different IDs? Or something else?