-
I have a bicep file that creates a virtual machine. I would like to attach an existing subnet already in Azure. Would it be added in this section?
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 10 replies
-
Hi @derekgrus,
Hope this samples will guide you to a correct solution. |
Beta Was this translation helpful? Give feedback.
-
Hi, my guess would be you are referencing the subnet wrong, try this, i commented directly below in your code: look at the error, something is not adding up it should be /vnet1/subnet1 or vnet1/subnet not two times like it is in your error: {"code":"InvalidResourceReference","message":"Resource /subscriptions/xxxxx-xxxx-xxxx/resourceGroups/rg-vnet/providers/Microsoft.Network/virtualNetworks/vnet1/subnets/subnet1 I commented out the not needed VNET part, //I am using the existing virtualNetwork here
//You do not actually need to reference vnet, you can only just reference a subnet like below:
/*
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2022-05-01' existing = {
name: virtualNetworkName
scope: resourceGroup('rg-vnet')
//'rg-vnet' is the resource group where the virtual network is built
}
*/
resource networksubnet 'Microsoft.Network/virtualNetworks/subnets@2023-04-01' existing = {
name: virtualNetworkName/subnetName //example: 'vnet1/subnet1'
scope: resourceGroup('rg-vnet') //this is ok
}
resource nic 'Microsoft.Network/networkInterfaces@2022-05-01' = {
name: nicName
location: location
properties: {
ipConfigurations: [
{
name: 'ipconfig1'
properties: {
privateIPAllocationMethod: 'Dynamic'
subnet: {
id: networksubnet.id //this should work - no need to state both vnet and subnet
}
}
}
]
}
dependsOn: [ //you should also probably remove this, you are referencing and existing vnet, so they already exisist, and if they do not deployment will fail either way
virtualNetwork
networksubnet
]
} Try it out. |
Beta Was this translation helpful? Give feedback.
-
Well, you have to reference public IP in your NIC resource creation and you are not doing that, are you attaching NSG to Subnet or NIC? |
Beta Was this translation helpful? Give feedback.
Hi @derekgrus,