Azure / Azure/bicep

There should be an easy and standardized way to get the ID of a subnet defined as a property of network

Open
#3,886 4 comments 1 reaction 0 assignees View on GitHub
Dominant language
Bicep
Stars
3.6k
Forks
830
Avg merge
1d 21m
Merged PRs (30d)
79

Description

From https://github.com/Azure/bicep/issues/3499

Example: https://github.com/Azure/azure-quickstart-templates/blob/cb02bd7943dd618c120705a3a5a36788b5b267f3/demos/nested-vms-in-virtual-network/main.bicep

```bicep
resource vnet 'Microsoft.Network/virtualNetworks@2021-02-01' = {
name: virtualNetworkName
location: location
properties: {
addressSpace: {
addressPrefixes: [
virtualNetworkAddressPrefix
]
}
subnets: [
{
name: NATSubnetName
properties: {
addressPrefix: NATSubnetPrefix
networkSecurityGroup: {
id: natNsg.id
}
}
}
resource vnet 'Microsoft.Network/virtualNetworks@2021-02-01' = {
name: virtualNetworkName
location: location
properties: {
addressSpace: {
addressPrefixes: [
virtualNetworkAddressPrefix
]
}
subnets: [
{
name: NATSubnetName
properties: {
addressPrefix: NATSubnetPrefix
networkSecurityGroup: {
id: natNsg.id
}
}
}
{
name: hyperVSubnetName
...
```
To access the ID of the subnet is then somewhat awkward. Here's one way:
```bicep
module createNic1 './nic.bicep' = {
name: 'createNic1'
params: {
location: location
nicName: HostNetworkInterface1Name
subnetId: '${vnet.id}/subnets/${NATSubnetName}'
```
This requires knowledge of the structure of a subnet's ID (and for me would requiring looking it up in docs).
Here's an arguably better way which uses resourceId:
```bicep
var subnet_id = resourceId(virtualNetworkResourceGroupName, 'Microsoft.Network/virtualNetworks/subnets', existingVnetName, existingSubnetName)
```
These both have problems:
1) Not entirely obvious, possible to get wrong
2) no automatic dependency analysis on the subnet
3) no strong typing for the subnet for other properties (can't say subnet.name or subnet.id)
4) not standardized and easy to use like network.id

NOTE: One solution is to break out the subnets into child or top-level resources, instead of as properties of the virtual network. But this has issues with deployment and is probably not the right approach:
1) subnets get re-created unnecessarily
2) must use dependsOn to force only one subnet to be created at a time (contention on modifying the network)

POSSIBILITIES:

1) make a way to access the subnet directly

We discussed virtualNetwork::subnet1 or virtualNetwork.properties.subnet1 as possibilities (the latter will be confusing because it looks like a runtime construct).

or 2) make it easy through intellisense to form resourceId() calls, similar to how the ARM extension currently does it (using resources in the current template):

https://user-images.githubusercontent.com/6913354/128101966-26218ac1-bf1a-4d12-a632-abefec605b2e.mov

Contributor guide

Open the contributing guide

Research direction

Start with this issue, the linked Azure Bicep issue #3499, and the referenced demos/nested-vms-in-virtual-network/main.bicep example. Compare the proposed direct subnet access and IntelliSense-assisted resourceId approaches; done requires an agreed, standardized way to retrieve a property-defined subnet ID with dependency analysis and strong typing.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure
Domain
cloud
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.