Skip to navigation

IP network functions

CIDR functions normalize IPv4 or IPv6 network prefixes. Host/subnet indices are signed 64-bit integer arguments.

FunctionResult and rules
cidrhost(prefix, host)Address at host offset; negative offsets count backward from the last address.
cidrnetmask(prefix)IPv4 netmask; IPv6 is rejected.
cidrsubnet(prefix, newbits, number)One subnet obtained by extending the prefix by newbits and selecting a nonnegative subnet number.
cidrsubnets(prefix, newbits...)Sequential, naturally aligned subnets for each extension; each newbits is positive, at most 32, and must fit the prefix.

Out-of-range indices, invalid prefixes, impossible extensions, and exhausted address space fail. No host discovery, DNS resolution, or network request occurs.

Example

locals {
network = {
host = cidrhost("192.0.2.0/24", 10)
mask = cidrnetmask("192.0.2.0/24")
subnet = cidrsubnet("192.0.2.0/24", 2, 1)
subnets = cidrsubnets("192.0.2.0/24", 2, 2)
}
}
output "network" {
type = string
value = jsonencode(local.network)
}

All calls obey the function limits and annotation rules.