Terraform provides functions to manage and manipulate IP addresses and CIDR blocks. These functions are crucial for network automation and orchestration in cloud environments, enabling users to create, manage, and allocate network resources efficiently. This article shows how to use the Terraform IP functions, along with some practical examples to help you get the most out of these powerful functions.
Terraform cidrhost
Function
The cidrhost
function in Terraform is used to calculate the specific IP address for a given host number within a specified CIDR block. This function is particularly useful when you need to assign static IP addresses within a subnet, ensuring that each resource gets a unique and correctly allocated IP address.
cidrhost(prefix, hostnum)
The cidrhost
function accepts the following arguments:
prefix
: The CIDR block (e.g., “192.168.0.0/24”).hostnum
: The host number within the CIDR block.
Here’s a simple example of using this function:
output "host_ip" {
value = cidrhost("192.168.0.0/24", 5)
}
This example calculates the 5th IP address within the 192.168.0.0/24
subnet, resulting in 192.168.0.5
.
The cidrhost
function in Terraform is a versatile tool for calculating specific IP addresses within a given CIDR block. By understanding its syntax and usage, you can efficiently assign static IP addresses to your resources, ensuring unique and correctly allocated addresses within your subnets.
Terraform cidrnetmask
Function
The cidrnetmask
function in Terraform is used to calculate the netmask for a given CIDR block. The netmask is often required in networking configurations where you need the dot-decimal notation of the subnet mask rather than the CIDR notation. This function is especially useful when configuring network devices or services that do not accept CIDR notation directly.
cidrnetmask(prefix)
The cidrnetmask
accepts the following argument:
prefix
: The CIDR block for which you want to calculate the netmask (e.g.,192.168.0.0/24
).
Here’s a simple example of using this function:
output "netmask" {
value = cidrnetmask("192.168.0.0/24")
}
This example returns the netmask for the 192.168.0.0/24
subnet, which is 255.255.255.0
.
The cidrnetmask
function in Terraform is a valuable tool for converting CIDR blocks into dot-decimal netmask notation, which is often required for configuring network devices and services. By understanding its syntax and usage, you can ensure accurate and consistent network configurations in your Terraform-managed infrastructure.

Terraform cidrsubnet
Function
The cidrsubnet
function is designed to facilitate the creation and management of subnets within a larger CIDR block. This function is particularly useful for defining hierarchical subnet structures, ensuring efficient IP address utilization, and simplifying network configurations.
cidrsubnet(prefix, newbits, netsum)
The cidrsubnet
function accepts the following arguments:
prefix
: The base CIDR block from which the subnet will be created.newbits
: The number of additional bits to add to the prefix length. This determines the size of the new subnet.netnum
: The network number (or subnet number) to be assigned within the new range.
Let’s consider a scenario where we have a virtual network with the CIDR block 10.0.0.0/16
and we want to create two smaller subnets within this network.
local {
vnet_cidr = "10.0.0.0/16"
}
output "subnet1" {
value = cidrsubnet(local.vnet_cidr, 8, 0)
}
output "subnet2" {
value = cidrsubnet(local.vnet_cidr, 8, 1)
}
This example will split the base CIDR block into the two following subnets:
cidrsubnet(loca.vnet_cidr, 8, 0)
: Outputs the subnet IP address range of10.0.0.0/24
. Thenewbits
of8
specifies a/24
subnet. Thenetnum
of0
specifies the first subnet block.cidrsubnet(loca.vnet_cidr, 8, 1)
: Outputs the subnet IP address range of10.0.1.0/24
. Thenewbits
of8
specifies a/24
subnet. Thenetnum
of1
specifies the second subnet block.
The cidrsubnet
function is a powerful tool in Terraform for dynamically calculating subnet addresses within a larger CIDR block. By understanding its syntax, you can efficiently manage and organize your cloud network resources. Whether you’re creating subnets for different environments or segmenting a network for security purposes, the cidrsubnet
function simplifies the process and ensures efficient IP address utilization.
Terraform cidrsubnets
Function
The cidrsubnets
function in Terraform is a powerful utility for generating multiple subnet addresses from a single CIDR block. This function simplifies the creation of hierarchical and segmented network structures by allowing you to define several subnets at once. It is especially useful when you need to divide a larger network into smaller subnets for various environments, such as production, development, and testing.
cidrsubnets(prefix, newbits1, newbits2, ...)
The cidrsubnets
function accepts the following arguments:
prefix
: The CIDR block (e.g., “192.168.0.0/16”).newbits
: The number of additional bits for each subnet.
Here’s a simple example of using this function:
output "subnets" {
value = cidrsubnets("192.168.0.0/16", 8, 8)
}
This example splits the 192.168.0.0/16
block into two /24
subnets, resulting in ["192.168.0.0/24", "192.168.1.0/24"]
.
The cidrsubnets
function is an efficient way to generate multiple subnet addresses from a single CIDR block. By understanding its syntax and usage, you can create well-structured, hierarchical network designs that suit various environments and requirements. Adhering to best practices, such as planning your subnetting strategy, using descriptive names, and validating CIDR blocks, ensures a robust and manageable network infrastructure.
IPv4 and IPv6 Support: The Terraform IP Address
cidrhost
,cidrsubnet
, andcidrsubnets
functions accept both IPv4 and IPv6 prefixes. The results of the functions always uses the same addressing scheme as the prefix given.
Usage Examples
Creating subnets within a virtual network is a common task when managing cloud infrastructure. Subnets allow you to segment your network for better organization, security, and performance. In this section, we’ll look at some examples around setting up subnets and assigning IP Addresses, leveraging the Terraform cidrhost
and cidrsubnet
functions to dynamically calculate subnet addresses within a given CIDR block.
Example: Creating Subnets
For a network architecture requiring multiple subnets, you can use the cidrsubnets
function:
variable "vnet_cidr" {
default = "10.0.0.0/16"
}
resource "azurerm_virtual_network" "example" {
name = "example-vnet"
address_space = [var.vnet_cidr]
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}
resource "azurerm_subnet" "subnet1" {
name = "subnet1"
resource_group_name = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.example.name
address_prefixes = [cidrsubnet(var.vnet_cidr, 8, 0)]
}
resource "azurerm_subnet" "subnet2" {
name = "subnet2"
resource_group_name = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.example.name
address_prefixes = [cidrsubnet(var.vnet_cidr, 8, 1)]
}
This example creates two subnets as follows:
cidrsubnet(var.vnet_cidr, 8, 0)
: This function call takes the base CIDR block (10.0.0.0/16
), adds 8 bits to the prefix length (resulting in/24
subnets), and calculates the first subnet (10.0.0.0/24
).cidrsubnet(var.vnet_cidr, 8, 1)
: This call similarly calculates the second subnet (10.0.1.0/24
).
Example Assign IP Addresses to Resources
When creating network interfaces and assigning specific IP addresses within each subnet, the cidrhost
function can be used.
resource "azurerm_network_interface" "nic1" {
name = "example-nic1"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.subnet1.id
private_ip_address_allocation = "Static"
private_ip_address = cidrhost(azurerm_subnet.subnet1.address_prefixes[0], 10)
}
}
resource "azurerm_network_interface" "nic2" {
name = "example-nic2"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.subnet2.id
private_ip_address_allocation = "Static"
private_ip_address = cidrhost(azurerm_subnet.subnet2.address_prefixes[0], 10)
}
}
This example assigns the IP Addresses as follows:
cidrhost(azurerm_subnet.subnet1.address_prefixes[0], 10)
: This function calculates the 10th host IP address in the first subnet (10.0.0.10
).cidrhost(azurerm_subnet.subnet2.address_prefixes[0], 10)
: Similarly, this calculates the 10th host IP address in the second subnet (10.0.1.10
).
Summary
Terraform’s IP functions are powerful tools for managing and manipulating IP addresses and CIDR blocks within your infrastructure as code configurations. The cidrhost
, cidrnetmask
, cidrsubnet
, and cidrsubnets
functions allow you to define, calculate, and utilize IP addresses and network ranges efficiently. By leveraging these functions, you can create flexible, scalable, and well-structured network configurations.
By following the best practices and examples provided in this article, you can enhance your Terraform configurations to better manage your cloud network resources. Whether you are creating subnets, assigning IP addresses, or calculating network masks, these functions will help you achieve your infrastructure goals with ease.
Original Article Source: Terraform IP Functions for Managing IP Addresses, CIDR Blocks, and Subnets written by Chris Pietschmann (If you’re reading this somewhere other than Build5Nines.com, it was republished without permission.)