liveBook · Manning

Cidrsubnet

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

cidrsubnet is a Terraform function that derives a numbered subnet CIDR from an input network CIDR and a requested prefix extension.

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

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Explains CIDR prefixes, bit extension, subnet numbering, and nested allocation through console and module examples.
Examples
Uses a /16 divided into four /18 networks and module locals assigning public, private, and isolated subnet ranges.
What it assumes
Assumes basic Terraform module and networking familiarity while defining CIDR notation and subnet-bit allocation.
What you can do after
Build reusable Terraform network modules with deterministic public, private, and isolated CIDR blocks.
How it connects
Connects CIDR notation and subnetting with Terraform modules, VPCs, public and private subnets, NAT gateways, and route tables.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.