Skip to content

Instantly share code, notes, and snippets.

@chancez
Last active March 16, 2023 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chancez/c8ae20540d94a685a2208e5351b12b04 to your computer and use it in GitHub Desktop.
Save chancez/c8ae20540d94a685a2208e5351b12b04 to your computer and use it in GitHub Desktop.
locals {
// Extracts the cidr block from a cidr, eg: the "16" from a CIDR such as "10.0.0.0/16"
cidr_block_size = split("/", var.cidr_block)[1]
// Get the total number of subnets (private subnet size + public subnet size)
// Take the logarithm base 2 to get the number of bits to accommodate all the
// subnets.
// We cannot use fractional values for cidrsubnet's newbits value, so use
// ceil() to round up. We can't round down because a smaller number bits
// would be insufficient to represent the required subnets.
default_subnet_size = ceil(log(var.private_subnet_count + var.public_subnet_size, 2))
// Use the default subnet size if the user doesn't specify a subnet size,
// otherwise use their value.
// Because the user expresses their subnet size in terms of CIDR block size,
// not newbits, we subtract the CIDR block size from their value to get the
// newbits for cidrsubnet().
private_subnet_size = var.private_subnet_size == 0 ? local.default_subnet_size : var.private_subnet_size - local.cidr_block_size
public_subnet_size = var.public_subnet_size == 0 ? local.default_subnet_size : var.public_subnet_size - local.cidr_block_size
// Create var.private_subnet_count subnets of size local.private_subnet_size
// (determined above), starting at private_subnet_offset.
private_cidr_blocks = [
for i in range(var.private_subnet_count) :
cidrsubnet(var.cidr_block, local.private_subnet_size, i + var.private_subnet_offset
)]
// Same as private, but the public subnet offset is additionally offset by
// the number of private subnets to avoid overlapping the public subnets with
// the private.
public_cidr_blocks = [
for i in range(var.public_subnet_count) :
cidrsubnet(var.cidr_block, local.public_subnet_size, i + var.private_subnet_count + var.public_subnet_offset
)]
}
output "private_cidr_blocks" {
value = local.private_cidr_blocks
description = "A list of CIDR blocks for private subnets."
}
output "public_cidr_blocks" {
value = local.public_cidr_blocks
description = "A list of CIDR blocks for public subnets."
}
variable "cidr_block" {
type = string
description = "Base CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`)"
}
variable "private_subnet_count" {
description = "Sets the maximum amount of private subnets to deploy."
}
variable "public_subnet_count" {
description = "Sets the maximum amount of public subnets to deploy."
}
variable "private_subnet_size" {
default = 0
description = "Block size of private subnets. For example, for a /24 specify 24."
}
variable "public_subnet_size" {
default = 0
description = "Block size of public subnets. For example, for a /24 specify 24."
}
variable "private_subnet_offset" {
default = 0
description = "Netnum offset for the private subnets. A value of 0 (default) means the private subnets will start at the beginning of the cidr_block."
}
variable "public_subnet_offset" {
default = 0
description = "Netnum offset for the public subnets. A value of 0 (default) means the public subnets will begin immediately following the private subnets."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment