Skip to content

A module used to calculate subnets (specifically used in Azurem, but can work for other providers)

License

Notifications You must be signed in to change notification settings

cyber-scot/terraform-null-subnet-calculator

Repository files navigation

locals {
  base_cidr_mask_size = tonumber(split("/", var.base_cidr)[1])

  # Determine if custom subnet details are provided
  use_custom_details = length(var.subnets) > 0

  # Use provided subnet details or generate subnet names and use default netnum and mask size
  final_subnet_details = local.use_custom_details ? var.subnets : {
    for i, size in var.subnet_sizes : format("subnet%s", i + 1) => {
      mask_size = size
      netnum    = i # Default netnum, can be changed to another logic
    }
  }

  calculated_subnets = {
    for subnet_name, details in local.final_subnet_details :
    subnet_name => cidrsubnet(var.base_cidr, details.mask_size - local.base_cidr_mask_size, details.netnum)
  }

  subnet_names = sort(keys(local.calculated_subnets))
}

Requirements

No requirements.

Providers

No providers.

Modules

No modules.

Resources

No resources.

Inputs

Name Description Type Default Required
base_cidr The base CIDR block string n/a yes
subnet_sizes List of subnet sizes if names are to be generated list(number) [] no
subnets Map of subnet names to their desired mask sizes and netnum
map(object({
mask_size = number
netnum = number
}))
{} no

Outputs

Name Description
base_cidr The base CIDR given to the module for calculation
subnet_first_ip A map of subnet names to their first usable IP addresses.
subnet_first_last_ip A map of subnet names to their last usable IP addresses.
subnet_last_ip A map of subnet names to their first and last usable IP addresses.
subnet_mask_sizes A map of subnet names to their mask sizes.
subnet_names A sorted list of subnet names calculated based on the provided details or sizes.
subnet_ranges A list of subnet CIDR ranges corresponding to the sorted subnet names, ensuring consistent ordering.
subnet_sizes A map of subnet names to the count of usable IPs in each subnet.
subnets_map A map of subnet names to their corresponding generated subnet CIDR ranges.

About

A module used to calculate subnets (specifically used in Azurem, but can work for other providers)

Resources

License

Stars

Watchers

Forks

Packages

No packages published