liveBook · Manning

Classless inter-domain routing

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

A CIDR block is a compact notation for an IP network and the range of addresses it contains.

CIDR divides an IP network into smaller networks and represents each block with a base address and prefix, such as 192.168.0.0/16. The prefix states how many of an IPv4 address’s 32 bits identify the network; remaining bits identify hosts or support further subnetting. section 10.2.1

Increasing the prefix by taking bits from the host portion creates more, smaller networks. Taking two bits from 192.168.0.0/16 produces four /18 networks, each with a distinct address range. section 10.2.1

Generating subnets with Terraform

Terraform’s cidrsubnet function accepts an original CIDR block, the number of additional subnet bits, and a subnet number. For example, cidrsubnet("192.168.0.0/16", 2, 1) returns 192.168.64.0/18; cidrnetmask returns 255.255.192.0. section 10.2.1

A practical network module can derive public and private subnet blocks from one input CIDR. A two-segment design uses one additional bit, assigns separate subnet numbers, enables public IP mapping on the public subnet, and routes private traffic through a NAT gateway. section 10.2.3

Operational considerations:

  • Research provider-specific networking resources before designing a module; Terraform’s IP-network functions remain provider-independent. section 10.2.1
  • Treat NAT gateways as simple but potentially expensive; NAT instances can reduce cost while requiring more involved setup. section 10.2.3

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

How deep it goes
Explains IPv4 prefixes, bit-based subdivision, masks, ranges, and Terraform calls, then stops short of provider-wide networking design.
Examples
Demonstrates 192.168.0.0/16, four /18 ranges, cidrsubnet console calls, and Terraform public-private subnet modules.
What it assumes
Assumes basic IPv4 networking and Terraform module context while defining CIDR notation and subnet-bit allocation from first principles.
What you can do after
Configure Terraform subnet layouts by deriving nonoverlapping CIDR blocks and routing private traffic through NAT.
How it connects
Connects CIDR with subnetting, Terraform, VPCs, NAT gateways, route tables, public subnets, private subnets, and isolated subnets.

Also explained in

Compare how 5 books explain classless inter-domain routing →

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.