Azure / Azure/bicep

Feature Request: cidrHostCount Function for Bicep

Open
#18,148 3 comments 7 reactions 1 assignee Claimed by @majastrz View on GitHub
enhancement intermediate language
Dominant language
Bicep
Stars
3.6k
Forks
830
Avg merge
1d 21m
Merged PRs (30d)
79

Description

# Feature Request: `cidrHostCount` Function for Bicep

**Hi Bicep team,**

I have been working with Azure Verified Modules (AVM) and the new IPAM pool functionality for Virtual Networks. However, I discovered a gap when trying to calculate the number of IP addresses needed for IPAM pool allocation from a CIDR prefix length.

When using the AVM Virtual Network resource module with IPAM pools, the `numberOfIpAddresses` parameter is required, but there's currently no built-in function to calculate this value from a CIDR prefix. This forces developers to manually calculate or use complex workarounds.

In the AVM Virtual Network module, I need to configure IPAM pool allocation like this:

```bicep
@allowed([
'dev'
'prod'
])
param parEnvironment string

// Currently requires manual calculation or complex workarounds
param ipamPoolNumberOfIpAddresses string = parEnvironment == 'prod' ? '65534' : '4094'

module virtualNetwork 'br/public:avm/res/network/virtual-network:0.8.0' = {
name: 'vnetDeployment'
params: {
name: 'vnet-example'
addressPrefixes: ['/subscriptions/.../resourceGroups/.../providers/Microsoft.Network/networkManagers/.../ipamPools/pool-example']
ipamPoolNumberOfIpAddresses: ipamPoolNumberOfIpAddresses
// ... other parameters
}
}
```

What would work better is having a built-in function to calculate this dynamically:

```bicep
@allowed([
'dev'
'prod'
])
param parEnvironment string

// Dynamic calculation based on environment
var cidrPrefix = parEnvironment == 'prod' ? 16 : 20

module virtualNetwork 'br/public:avm/res/network/virtual-network:0.8.0' = {
name: 'vnetDeployment'
params: {
name: 'vnet-example'
addressPrefixes: ['/subscriptions/.../resourceGroups/.../providers/Microsoft.Network/networkManagers/.../ipamPools/pool-example']
ipamPoolNumberOfIpAddresses: string(cidrHostCount(cidrPrefix))
// ... other parameters
}
}
```

**Describe the solution you'd like:**

The desired solution is a new Bicep function called `cidrHostCount` that calculates the number of usable host IP addresses from a CIDR prefix length. This function would:

1. Accept either integer (e.g., `16`) or string format (e.g., `'/16'`)
2. Return the number of usable host addresses (excluding network and broadcast addresses) **and** total number of hostAdresses
3. Handle special cases for `/31` and `/32` subnets
4. Integrate seamlessly with AVM modules requiring IP address counts

**Function Specification:**

```bicep
param parCidrPrefix int

// Proposed function usage
var hostCount = cidrHostCount(parCidrPrefix) // Returns usable host count + total number of ip addresses
```

**Expected Behavior:**

```bicep
cidrHostCount(16) // usableNumberOfIps: 65534 (2^16 - 2) and totalNumberOfIps: 65536 (Needed for ipam pool allocation)
```

**Use Cases:**

1. **IPAM Pool Sizing**: Calculate IP addresses needed for Azure IPAM pool allocations
2. **Subnet Planning**: Determine capacity for subnet configurations
3. **Network Design**: Validate that CIDR blocks meet capacity requirements
4. **AVM Integration**: Seamless integration with Azure Verified Modules

**Benefits:**

- **Simplifies AVM Usage**: Eliminates manual calculations for IPAM configurations
- **Reduces Errors**: Prevents miscalculation of IP address requirements
- **Enhances Readability**: Makes network sizing intentions clear in templates
- **Follows Bicep Patterns**: Aligns with existing CIDR functions (`cidrSubnet`, `cidrHost`, `parseCidr`)

This function would perfectly complement the existing `cidrSubnet`, `cidrHost`, and `parseCidr` functions for complete CIDR manipulation capabilities in Bicep, especially when working with Azure IPAM and AVM modules.

**Thanks for considering! 😊**

**Additional Context:**

This feature request directly addresses a gap in the current AVM Virtual Network module where developers need to manually calculate `numberOfIpAddresses` for IPAM pool allocations. The `cidrHostCount` function would make Azure IPAM integration much more developer-friendly and reduce configuration errors in production deployments.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.