Skip to main content

Working with Bicep CIDR Functions - Part 2

· 3 min read
Hasan Gural

In the previous blog post, we left out an example where we have parameters for vNetAddress, vSubnetCount, and vSubnetRange. I would like to show how the deployment looks like when we use what-if and how the output looks like.

Our requirement was to create a virtual network with a given address space and a given number of subnets. We also wanted to specify the range of subnets. We used the cidrSubnet function to create the subnets.

⚡ Example Deployment for cidrSubnet Function

Here is the output of the what-if operation: We can observe that the subnets are created as expected, following the calculated range and names.

image

Scope: /subscriptions/e8055386-2b08-4238-9749-edd80932c74e/resourceGroups/rg-vnnet

+ Microsoft.Network/virtualNetworks/myVNet [2023-04-01]

apiVersion: "2023-04-01"
id: "/subscriptions/e8055386-2b08-4238-9749-edd80932c74e/resourceGroups/rg-vnet/providers/Microsoft.Network/virtualNetworks/myVNet"
location: "westus"
name: "myVNet"
properties.addressSpace.addressPrefixes: [
0: "192.168.8.0/24"
]
properties.subnets: [
0:

name: "subnet-0"
properties.addressPrefix: "192.168.8.0/26"

1:

name: "subnet-1"
properties.addressPrefix: "192.168.8.64/26"

2:

name: "subnet-2"
properties.addressPrefix: "192.168.8.128/26"

3:

name: "subnet-3"
properties.addressPrefix: "192.168.8.192/26"

]
type: "Microsoft.Network/virtualNetworks"

Resource changes: 1 to create, 1 to ignore.

Output looks good for the one of real example. We've covered two of the most commonly used Bicep CIDR functions: parseCidr and cidrSubnet. Last function that I want to talk about is the cidrHost function.

🏷️ cidrHost Function

This function gives us the host address for a given subnet. The function takes two parameters:

  • network (string): String containing an IP address range to convert in CIDR notation.
  • hostIndex (int) : The index determines the host IP address to be returned.

The function returns a string representing the host address.

Simple representation of the cidrHost function is as follows:


var lookupHosts = [
{
vSubnetRange: 27
hostIndex: 6
}
{
vSubnetRange: 27
hostIndex: 7
}
{
vSubnetRange: 27
hostIndex: 8
}
]

var addressRange = '192.168.25.128/25'


output hostAddress array = [for (item, idx) in lookupHosts: {

vSubnetRange : cidrSubnet(addressRange, item.vSubnetRange, idx)
hostAddress : cidrHost((cidrSubnet(addressRange, item.vSubnetRange, idx)),item.hostIndex)

}]

This time, I have extended the example using the cidrSubnet and cidrHost functions. If we look closely at the variable that I have introduced, lookupHosts, which is just an array of objects. Each object has two properties: vSubnetRange and hostIndex. That means we can use the cidrHost function to calculate the host addresses for the given subnet range and host index.

When we run the above code, we will get output as an array of objects to represent the host addresses:


"hostAddress": {
"type": "Array",
"value": [
{
"hostAddress": "192.168.25.135",
"vSubnetRange": "192.168.25.128/27"
},
{
"hostAddress": "192.168.25.168",
"vSubnetRange": "192.168.25.160/27"
},
{
"hostAddress": "192.168.25.201",
"vSubnetRange": "192.168.25.192/27"
}
]
}

I hope the blog post series have given you a good understanding of the CIDR functions in Bicep. I have tried to cover the most commonly used functions and provided real-world examples. I hope you find this information useful. If you have any questions or feedback, please feel free to reach out to me. See you in the next post.