cloudposse/terraform-aws-named-subnets

Add an optional variable to specify a netnum offset

robgonnella opened this issue · 1 comments

Describe the Feature

Allow users to specify an offset for the netnum value when calculating subnet cidr blocks.
e.g.

subnet_names = ["one", "two"]
cidr_block = 10.10.0.0/16
netnum_offset = 1

Which would result in 2 subnets with cidr blocks, 10.10.16.0/20 and 10.10.32.0/20

Expected Behavior

Specifying a netnum offset would offset the call to cidrsubnet by the given value
e.g.

cidrsubnet(var.cidr_block, ceil(log(var.max_subnets, 2)), count.index + var.netnum_offset)

Use Case

This valuable in cases where you want to use available addresses in a cidr block before needing to add an additional cidr block to the vpc.

Describe Ideal Solution

An optional variable (type = number) is added to the module that allows specification of a netnum offset. This offset is then used to offset the netnum value when calculating the subnet cidr block via the call to the cidrsubnet function.

Alternatives Considered

I could just add a new cidr block to the vpc, forego any netnum offset, and calling it a day. This is a totally valid and easy solution, but the addition of an offset doesn't seem too difficult either, and maybe someone else would also find value in this option.

After working with this approach a bit more, I realized that in order for the netnum_offset to work as described in the above example, max_subnets will need to be more than what is actually needed. The example above works because the default value for max_subnets is 16, which translates to 4 newbits log(16, 2) = 4.
e.g.

subnet_names = ["one", "two"]
cidr_block = "10.10.0.0/18"
max_subnets = 3 # will cause a newbits value of 2 - ceil(log(3, 2)) = 2
netnum_offset = 1

Should result in 10.10.16.0/20 and 10.10.32.0/20
or...

subnet_names = ["one", "two", "three"]
cidr_block = "10.10.0.0/17"
max_subnets = 5 # will cause a newbits value of 3 - ceil(log(5, 2)) = 3

Should result in 10.10.16.0/20, 10.10.32.0/20, and 10.10.64.0/20

I realize this is a bit ugly so maybe there is a better solution? Maybe whenever netnum_offset is specified the cidr calculation becomes cidrsubnet(var.cidr_block, local.<public_count | private_count>, count.index + var.netnum_offset) instead of using ceil(log(var.max_subnets, 2)) ?