How to reference resource created with loop in another looped resource? #4817
-
Hi, I know the name of topic is weird, but let me explain: Let says I am creating two NIC's with looping: resource nic 'Microsoft.Network/networkInterfaces@2021-02-01' = [for i in range(1,2): {
name: 'test${i}-NIC'
location: location
properties: {
ipConfigurations: [
{
name: 'ipconfig1'
properties: {
privateIPAllocationMethod: 'Dynamic'
subnet: {
id: subnet1
}
}
}
]
}
}] and then I am creating two VMS with loop also and would like to reference previously created NIC for each created VM for network like so: resource` vm 'Microsoft.Compute/virtualMachines@2021-03-01' = [for i in range(1,2): {
name: 'test-vm${i}'
location: location
properties: {...
...
networkProfile: {
networkInterfaces: [
{
id: 'test${i}-NIC.id'
}
]
}
}
}] Which is the proper way to do this? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 5 replies
-
Can you try the following? resource vm 'Microsoft.Compute/virtualMachines@2021-03-01' = [for i in range(1,2): {
name: 'iCSYS20-APA0${i}'
location: location
properties: {
...
networkProfile: {
networkInterfaces: [
{
id: 'test${i}-${nic[i].id}'
}
]
}
}
}] |
Beta Was this translation helpful? Give feedback.
-
Hi Alex, it is not working:
|
Beta Was this translation helpful? Give feedback.
-
Hm but does not changing |
Beta Was this translation helpful? Give feedback.
-
This is what it gets created if I change to Resource and property changes are indicated with these symbols:
+ Create
* Ignore
The deployment will update the following scope:
Scope: /subscriptions/41121bdf-5d73-4658-9612-9900f083ed7a/resourceGroups/BicepRG8
+ Microsoft.Compute/virtualMachines/test-vm0 [2021-03-01]
apiVersion: "2021-03-01"
id: "/subscriptions/41121bdf-5d73-4658-9612-9900f083ed7a/resourceGroups/BicepRG8/providers/Microsoft.Compute/virtualMachines/test-vm0"
location: "eastus"
name: "test-vm0"
properties.hardwareProfile.vmSize: "Standard_D2_v3"
properties.networkProfile.networkInterfaces: [
0:
id: "test0-/subscriptions/41121bdf-5d73-4658-9612-9900f083ed7a/resourceGroups/BicepRG8/providers/Microsoft.Network/networkInterfaces/test0-NIC"
]
properties.osProfile.adminPassword: "*******"
properties.osProfile.adminUsername: "azureadmin"
properties.osProfile.computerName: "test-vm0"
properties.storageProfile.dataDisks: [
0:
createOption: "Empty"
lun: 0
]
properties.storageProfile.imageReference.offer: "WindowsServer"
properties.storageProfile.imageReference.publisher: "MicrosoftWindowsServer"
properties.storageProfile.imageReference.sku: "2019-Datacenter"
properties.storageProfile.imageReference.version: "latest"
properties.storageProfile.osDisk.createOption: "FromImage"
type: "Microsoft.Compute/virtualMachines"
+ Microsoft.Network/networkInterfaces/test0-NIC [2021-02-01]
apiVersion: "2021-02-01"
id: "/subscriptions/41121bdf-5d73-4658-9612-9900f083ed7a/resourceGroups/BicepRG8/providers/Microsoft.Network/networkInterfaces/test0-NIC"
location: "eastus"
name: "test0-NIC"
properties.ipConfigurations: [
0:
name: "ipconfig1"
properties.privateIPAllocationMethod: "Dynamic"
]
type: "Microsoft.Network/networkInterfaces" |
Beta Was this translation helpful? Give feedback.
-
You probably meant change |
Beta Was this translation helpful? Give feedback.
-
Ok so this is what I found out: It is not the NICs loop that is causing the issue, but the VM loop, I can choose whatever range for NICs (like
Why is this happening? Can you please try to replicate the issue, here is the code, just replace the subnet "id" with yours. It seems so trivial but I guess it is not :) param adminUsername string
@minLength(12)
@secure()
param adminPassword string
var subnet1 = //replace with subnet ID
resource nic 'Microsoft.Network/networkInterfaces@2021-02-01' = [for i in range(1,2): {
name: 'test${i}-NIC'
location: resourceGroup().location
properties: {
ipConfigurations: [
{
name: 'ipconfig1'
properties: {
privateIPAllocationMethod: 'Dynamic'
subnet: {
id: subnet1
}
}
}
]
}
}]
resource vm 'Microsoft.Compute/virtualMachines@2021-03-01' = [for i in range(1,2): { // range(0,2) works fine
name: 'test-vm${i}'
location: resourceGroup().location
properties: {
hardwareProfile: {
vmSize: 'Standard_D2_v3'
}
osProfile: {
computerName: 'test-vm${i}'
adminUsername: adminUsername
adminPassword: adminPassword
}
storageProfile: {
imageReference: {
publisher: 'MicrosoftWindowsServer'
offer: 'WindowsServer'
sku: '2019-Datacenter'
version: 'latest'
}
osDisk: {
createOption: 'FromImage'
managedDisk: {
storageAccountType: 'StandardSSD_LRS'
}
}
dataDisks: [
{
diskSizeGB: 40
managedDisk:{
storageAccountType: 'StandardSSD_LRS'
}
lun: 0
createOption: 'Empty'
}
]
}
networkProfile: {
networkInterfaces: [
{
id: nic[i].id
}
]
}
}
}]
|
Beta Was this translation helpful? Give feedback.
-
Alex thanks, this code now works perfectly. I also liked you used "existing" as I need to reference more already created resources for my further development of the whole deployment! You can mark this as solved! |
Beta Was this translation helpful? Give feedback.
-
Adding above solution as a new comment so I can mark this as the answer. Ok, I just had to re-learn how the And if we look at the return value of That results in trying to access If the goal is to make sure the NIC names and the VM names are the following: Then this code should work (I tested it this time!). Notice I am taking advantage of the param adminUsername string
@minLength(12)
@secure()
param adminPassword string
resource subnet1 'Microsoft.Network/virtualNetworks/subnets@2021-02-01' existing = {
name: '<<VNETNAME>>/<<SUBNETNAME>>' // UPDATE ME
scope: resourceGroup('<<RG-OF-VNET-NAME>>') // UPDATE ME
}
resource nic 'Microsoft.Network/networkInterfaces@2021-02-01' = [for i in range(0, 2): {
name: 'test${i+1}-NIC'
location: resourceGroup().location
properties: {
ipConfigurations: [
{
name: 'ipconfig1'
properties: {
privateIPAllocationMethod: 'Dynamic'
subnet: {
id: subnet1.id
}
}
}
]
}
}]
resource vm 'Microsoft.Compute/virtualMachines@2021-03-01' = [for i in range(0, 2): {
// range(0,2) works fine
name: 'test-vm${i + 1}'
location: resourceGroup().location
properties: {
hardwareProfile: {
vmSize: 'Standard_D2_v3'
}
osProfile: {
computerName: 'test-vm${i + 1}'
adminUsername: adminUsername
adminPassword: adminPassword
}
storageProfile: {
imageReference: {
publisher: 'MicrosoftWindowsServer'
offer: 'WindowsServer'
sku: '2019-Datacenter'
version: 'latest'
}
osDisk: {
createOption: 'FromImage'
managedDisk: {
storageAccountType: 'StandardSSD_LRS'
}
}
dataDisks: [
{
diskSizeGB: 40
managedDisk: {
storageAccountType: 'StandardSSD_LRS'
}
lun: 0
createOption: 'Empty'
}
]
}
networkProfile: {
networkInterfaces: [
{
id: nic[i].id
}
]
}
}
}] |
Beta Was this translation helpful? Give feedback.
Adding above solution as a new comment so I can mark this as the answer.
Ok, I just had to re-learn how the
range()
function works :) Thanks to the helpful bicep function signatures, I realized that I misunderstood what the second argument of the function is doing. It is responsible for the amount of items in the returned array. I thought it was the upper bound of the array, which is not right.And if we look at the return value of
output stuff array = range(1,2)
:That results in trying to access
nic[2]
in the VM loop, which does not exist and we get this error.If the goal is to make sure the NIC names and the VM names are the following:
test-nic1
test-nic2
test-vm1
test-vm2
Then this…