-
-
Notifications
You must be signed in to change notification settings - Fork 166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Overhaul for IPv6 and flexiblity #159
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
40dc06e
Update testing framework
Nuru 00adbb7
Overhaul for IPv6 and flexibility
Nuru 370c550
Minor cleanups
cloudpossebot 6d96a8f
fix test input
Nuru 2f04b18
enable/disable route tables rather than routes
Nuru dde85f4
Add public and private label names
Nuru f3ce75d
Add v2 documentation
Nuru 0c54dcc
Fix #120, implement #154
Nuru e584396
Address reviewer comments, fix nat_instance_enabled=true
Nuru 898448e
Rather than fix them, allow certain configurations to produce errors
Nuru 0b6adef
Minor documentation format fix
Nuru 0e06d92
Address reviewer notes
Nuru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,39 @@ | ||
## Subnet calculation logic | ||
|
||
`terraform-aws-dynamic-subnets` creates a set of subnets based on `${var.cidr_block}` input and number of Availability Zones in the region. | ||
|
||
For subnet set calculation, the module uses Terraform interpolation | ||
|
||
[cidrsubnet](https://www.terraform.io/docs/configuration/interpolation.html#cidrsubnet-iprange-newbits-netnum-). | ||
`terraform-aws-dynamic-subnets` creates a set of subnets based on various CIDR inputs and | ||
the maximum possible number of subnets, which is `max_subnet_count` when specified or | ||
the number of Availability Zones in the region when `max_subnet_count` is left at | ||
its default value of zero. | ||
|
||
You can explicitly provide CIDRs for subnets via `ipv4_cidrs` and `ipv6_cidrs` inputs if you want, | ||
but the usual use case is to provide a single CIDR which this module will subdivide into a set | ||
of CIDRs as follows: | ||
|
||
1. Get number of available AZ in the region: | ||
``` | ||
${ | ||
cidrsubnet( | ||
signum(length(var.cidr_block)) == 1 ? | ||
var.cidr_block : data.aws_vpc.default.cidr_block, | ||
ceil(log(length(data.aws_availability_zones.available.names) * 2, 2)), | ||
count.index) | ||
} | ||
existing_az_count = length(data.aws_availability_zones.available.names) | ||
``` | ||
2. Determine how many sets of subnets are being created. (Usually it is `2`: `public` and `private`): `subnet_type_count`. | ||
3. Multiply the results of (1) and (2) to determine how many CIDRs to reserve: | ||
``` | ||
cidr_count = existing_az_count * subnet_type_count | ||
``` | ||
|
||
4. Calculate the number of bits needed to enumerate all the CIDRs: | ||
``` | ||
subnet_bits = ceil(log(cidr_count, 2)) | ||
``` | ||
5. Reserve CIDRs for private subnets using [`cidrsubnet`](https://www.terraform.io/language/functions/cidrsubnet): | ||
``` | ||
private_subnet_cidrs = [ for netnumber in range(0, existing_az_count): cidrsubnet(cidr_block, subnet_bits, netnumber) ] | ||
``` | ||
6. Reserve CIDRs for public subnets in the second half of the CIDR block: | ||
``` | ||
public_subnet_cidrs = [ for netnumber in range(existing_az_count, existing_az_count * 2): cidrsubnet(cidr_block, subnet_bits, netnumber) ] | ||
``` | ||
|
||
1. Use `${var.cidr_block}` input (if specified) or | ||
use a VPC CIDR block `data.aws_vpc.default.cidr_block` (e.g. `10.0.0.0/16`) | ||
2. Get number of available AZ in the region (e.g. `length(data.aws_availability_zones.available.names)`) | ||
3. Calculate `newbits`. `newbits` number specifies how many subnets | ||
be the CIDR block (input or VPC) will be divided into. `newbits` is the number of `binary digits`. | ||
|
||
Example: | ||
|
||
`newbits = 1` - 2 subnets are available (`1 binary digit` allows to count up to `2`) | ||
|
||
`newbits = 2` - 4 subnets are available (`2 binary digits` allows to count up to `4`) | ||
|
||
`newbits = 3` - 8 subnets are available (`3 binary digits` allows to count up to `8`) | ||
|
||
etc. | ||
|
||
1. We know, that we have `6` AZs in a `us-east-1` region (see step 2). | ||
2. We need to create `1 public` subnet and `1 private` subnet in each AZ, | ||
thus we need to create `12 subnets` in total (`6` AZs * (`1 public` + `1 private`)). | ||
3. We need `4 binary digits` for that ( 2<sup>4</sup> = 16 ). | ||
In order to calculate the number of `binary digits` we should use `logarithm` | ||
function. We should use `base 2` logarithm because decimal numbers | ||
can be calculated as `powers` of binary number. | ||
See [Wiki](https://en.wikipedia.org/wiki/Binary_number#Decimal) | ||
for more details | ||
|
||
Example: | ||
|
||
For `12 subnets` we need `3.58` `binary digits` (log<sub>2</sub>12) | ||
|
||
For `16 subnets` we need `4` `binary digits` (log<sub>2</sub>16) | ||
|
||
For `7 subnets` we need `2.81` `binary digits` (log<sub>2</sub>7) | ||
|
||
etc. | ||
4. We can't use fractional values to calculate the number of `binary digits`. | ||
We can't round it down because smaller number of `binary digits` is | ||
insufficient to represent the required subnets. | ||
We round it up. See [ceil](https://www.terraform.io/docs/configuration/interpolation.html#ceil-float-). | ||
|
||
Example: | ||
|
||
For `12 subnets` we need `4` `binary digits` (ceil(log<sub>2</sub>12)) | ||
|
||
For `16 subnets` we need `4` `binary digits` (ceil(log<sub>2</sub>16)) | ||
|
||
For `7 subnets` we need `3` `binary digits` (ceil(log<sub>2</sub>7)) | ||
|
||
etc. | ||
|
||
5. Assign private subnets according to AZ number (we're using `count.index` for that). | ||
6. Assign public subnets according to AZ number but with a shift according to the number of AZs in the region (see step 2) | ||
Note that this means that, for example, in a region with 4 availability zones, if you specify only 3 availability zones | ||
in `var.availability_zones`, this module will still reserve CIDRs for the 4th zone. This is so that if you later | ||
want to expand into that zone, the existing subnet CIDR assignments will not be disturbed. If you do not want | ||
to reserve these CIDRs, set `max_subnet_count` to the number of zones you are actually using. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: consider eliminating the second
that
:Note that this means, for example, ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eliminating optional/implied words in the fashion you suggest works for me, but it seems to be a problem for readers for whom English is not their native language.