cidrsubnet derives a numbered subnet CIDR from a larger network. It accepts an input CIDR, the number of host bits to convert into subnet bits, and the subnet number to return. section 10.2.1
CIDR notation combines a network address with a prefix length. Extending the prefix by two bits divides 192.168.0.0/16 into four /18 networks; each additional bit doubles the number of available subnet numbers. section 10.2.1
Numbering four /18 subnets
cidrsubnet("192.168.0.0/16", 2, 0) returns "192.168.0.0/18"; subnet numbers 1, 2, and 3 return "192.168.64.0/18", "192.168.128.0/18", and "192.168.192.0/18". section 10.2.1
The function becomes useful when Terraform locals turn one module input into predictable network segments. A two-segment module can assign subnet number 0 to a private subnet and 1 to a public subnet. A three-segment design can first create an intermediary block, then subdivide it into public and isolated networks. section 10.2.3
When using cidrsubnet in network modules:
- Choose the extension-bit count according to the number of subnet ranges required. section 10.2.1
- Assign stable subnet numbers so public, private, and isolated roles receive predictable CIDR blocks. section 10.2.3
- Treat CIDR allocation separately from provider-specific routing, gateways, and subnet resources. section 10.2.1 section 10.2.3
Table 10.2 CIDR main network (view table figure)
| CIDR |
192.168.0.0/16 |
| CIDR subnet mask |
255.255.0.0 |
| CIDR wildcard mask |
0.0.255.255 |
| Binary network mask |
11111111.11111111.00000000.00000000 |
| Binary wildcard mask |
00000000.00000000.11111111.11111111 |
| Network range |
192.168.0.0 - 192.168.255.255 |
| Maximum addresses |
65,534 |
Use the CIDR table to inspect the original /16 network, its masks, address range, and available addresses. section 10.2.1
