Skip to main content

Using Date, Time, and Network Functions

1. Using timestamp Function

Function Description Example
timestamp() Returns the current date and time in RFC3339 format 2026-01-25T14:30:00Z

Example: Using timestamp in resource naming

resource "aws_instance" "web" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
  tags = {
    Name = "web-server-${timestamp()}"
  }
}

2. Using formatdate Function

Argument Description Example
format Format string specifying the date/time layout "YYYY-MM-DD"
timestamp Input timestamp to format (optional, defaults to current time) "2024-01-01T00:00:00Z"

Example: Formatting Current Date

output "current_date" {
  value = formatdate("YYYY-MM-DD", timestamp())
}

Example: Formatting Specific Timestamp

output "formatted_date" {
  value = formatdate("DD/MM/YYYY", "2024-07-15T10:30:00Z")
}
Note: The formatdate function uses the Luxon parsing tokens for format strings.

3. Using timeadd Function

Argument Description Example
time The input time string in RFC3339 format "2024-01-01T00:00:00Z"
duration Duration to add, in Terraform duration format "1h"
result Output time string in RFC3339 format "2024-01-01T01:00:00Z"

Example: Adding one hour to a timestamp

timeadd("2024-01-01T00:00:00Z", "1h")

Example: Adding 30 minutes to a timestamp

timeadd("2024-01-01T00:00:00Z", "30m")

4. Using timecmp Function

Function Signature Description Return Type
timecmp timecmp(time1, time2) Compares two time values and returns an integer indicating their relative order. number

Example: Comparing Time Values

locals {
  time1 = "2025-04-01T10:00:00Z"
  time2 = "2025-04-01T12:00:00Z"
  comparison = timecmp(time1, time2)
}
Note: The timecmp function returns 0 if the times are equal, a negative number if time1 is earlier than time2, and a positive number if time1 is later than time2.

5. Using plantimestamp Function

Property Description Example
plantimestamp Generates a timestamp in RFC3339 format based on the current date and time plantimestamp()

Example: Using plantimestamp in a resource

resource "aws_instance" "example" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"

  tags = {
    CreatedAt = plantimestamp()
  }
}
Note: The plantimestamp() function is useful for tagging resources with creation timestamps. It returns a string in RFC3339 format (e.g., 2025-04-05T14:30:00Z).

6. Using cidrhost Function

Argument Description Example
cidrhost() Generates an IP address within a CIDR block cidrhost("10.0.0.0/16", 5)

Example: Generate IP Address from CIDR Block

resource "aws_instance" "web" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"

  # Assigning an IP address from a subnet
  private_ip = cidrhost("10.0.1.0/24", 10)
}
Note: The cidrhost function takes a subnet and a host number, returning the IP address at that position in the subnet.

7. Using cidrsubnet Function

Argument Description Example
cidrsubnet() Generates a subnet within a given CIDR block cidrsubnet("10.0.0.0/16", 4, 5)

Example: Basic Usage

resource "aws_subnet" "example" {
  vpc_id     = aws_vpc.example.id
  cidr_block = cidrsubnet(aws_vpc.example.cidr_block, 4, 5)
}

Example: Subnetting with Different Prefix Lengths

output "subnet_cidr" {
  value = cidrsubnet("192.168.0.0/16", 8, 10)
}
Note: The function takes a base CIDR block, a prefix length increment, and a subnet number to calculate the new subnet.

8. Using cidrnetmask Function

Function Signature Description Example
cidrnetmask cidrnetmask(string) Returns the netmask for a CIDR block. cidrnetmask("192.168.1.0/24") => "255.255.255.0"

Example: Using cidrnetmask in a Network Configuration

resource "aws_subnet" "example" {
  vpc_id = aws_vpc.example.id
  cidr_block = "10.0.1.0/24"
  availability_zone = "us-west-2a"
  map_public_ip_on_launch = false
}

output "subnet_netmask" {
  value = cidrnetmask(aws_subnet.example.cidr_block)
}

9. Using cidrsubnetloop Function

The cidrsubnetloop function in Terraform is used to generate a list of CIDR subnets by looping through a specified range and applying subnetting logic. It is particularly useful when you need to create multiple subnets with a consistent pattern, such as in a VPC or network configuration.

Argument Type Description
base_cidr_block string The base CIDR block from which subnets will be derived.
new_bits number The number of additional bits to add to the subnet mask for each generated subnet.
count number The total number of subnets to generate.

Example: Using cidrsubnetloop to Generate Subnets

output "subnet_cidrs" {
  value = cidrsubnetloop("10.0.0.0/16", 4, 3)
}

This example generates three subnets from the base CIDR block 10.0.0.0/16, each with an additional 4 bits in the subnet mask, resulting in subnets like 10.0.0.0/20, 10.0.16.0/20, and 10.0.32.0/20.

Note: The cidrsubnetloop function is available in Terraform v1.6 and later.

10. Using cidrcontains Function

The cidrcontains function in Terraform checks whether a given IP address or subnet is contained within another subnet.

Argument Type Description
cidr string The parent CIDR block to check against (e.g., "10.0.0.0/8")
ip string The IP address or subnet to check for containment (e.g., "10.0.1.0/24")

Example: Checking if an IP is within a subnet

output "ip_in_subnet" {
  value = cidrcontains("10.0.0.0/8", "10.0.1.0/24")
}

Example: Checking if a subnet is contained within another

output "subnet_in_subnet" {
  value = cidrcontains("192.168.0.0/16", "192.168.1.0/24")
}
Note: The function returns true if the second argument (IP or subnet) is fully contained within the first argument (CIDR block). It supports both IPv4 and IPv6 subnets.

Was this article helpful?