From 40dc06e9773ba118d4c8e270e0cef9a10fd4a315 Mon Sep 17 00:00:00 2001 From: Nuru Date: Thu, 28 Apr 2022 11:23:34 -0700 Subject: [PATCH 01/12] Update testing framework --- README.md | 2 +- docs/terraform.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 051557ef..098a296d 100644 --- a/README.md +++ b/README.md @@ -232,7 +232,7 @@ Available targets: | Name | Version | |------|---------| -| [aws](#provider\_aws) | >= 3.62.0 | +| [aws](#provider\_aws) | 4.11.0 | ## Modules diff --git a/docs/terraform.md b/docs/terraform.md index 106e982b..1a609f7a 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -10,7 +10,7 @@ | Name | Version | |------|---------| -| [aws](#provider\_aws) | >= 3.62.0 | +| [aws](#provider\_aws) | 4.11.0 | ## Modules From 00adbb741f82b545aa0b1ca7fea7eb5a3cbf6f8e Mon Sep 17 00:00:00 2001 From: Nuru Date: Fri, 6 May 2022 18:56:59 -0700 Subject: [PATCH 02/12] Overhaul for IPv6 and flexibility --- README.md | 167 ++++++++++++--- README.yaml | 57 ++++- docs/terraform.md | 111 +++++++--- examples/complete/main.tf | 17 +- examples/complete/outputs.tf | 22 ++ main.tf | 213 +++++++++++++++++-- moved.tf | 16 ++ nat-gateway.tf | 69 +++--- nat-instance.tf | 82 ++++---- outputs-deprecated.tf | 5 + outputs.tf | 54 +++-- private.tf | 141 +++++++++---- public.tf | 156 +++++++++----- test/src/go.mod | 2 +- test/src/go.sum | 4 +- variables-deprecated.tf | 72 +++++++ variables.tf | 396 +++++++++++++++++++++++++++++------ versions.tf | 4 +- 18 files changed, 1256 insertions(+), 332 deletions(-) create mode 100644 moved.tf create mode 100644 outputs-deprecated.tf create mode 100644 variables-deprecated.tf diff --git a/README.md b/README.md index 098a296d..507846ab 100644 --- a/README.md +++ b/README.md @@ -30,10 +30,64 @@ Terraform module to provision public and private [`subnets`](http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Subnets.html) in an existing [`VPC`](https://aws.amazon.com/vpc) -**IMPORTANT:** This module provisions NAT instance with public IP. + __Note:__ this module is intended for use with an existing VPC and existing Internet Gateway. To create a new VPC, use [terraform-aws-vpc](https://github.com/cloudposse/terraform-aws-vpc) module. +The core feature of this module is dividing up a given CIDR range so that a set of subnets each gets its own +distinct CIDR range within that range, and then creating those subnets in the appropriate availability zones. +The intention is to keep this module relatively simple and easy to use for the most popular use cases. +In its default configuration, this module creates 1 public subnet and 1 private subnet in each +of the specified availability zones. The public subnets are configured for bi-directional traffic to the +public internet, while the private subnets are configured for egress-only traffic to the public internet. +Rather than provide a wealth of configuration options allowing for numerous special cases, this module +provides some common options and further provides the ability to suppress the creation of resources, allowing +you to create and configure them as you like from outside this module. For example, rather than give you the +option to customize the Network ACL, the module gives you the option to create a completely open one (and control +access via Security Groups and other means) or not create one at all, allowing you to create and configure one yourself. + +### Public subnets + +This module defines a public subnet as one that has direct access to an internet gateway and can accept incoming connection requests. +In the simplest configuration, the module creates a single route table with a default route targeted to the +VPC's internet gateway, and associates all the public subnets with that single route table. + +Likewise it creates a single Network ACL with associated rules allowing all ingress and all egress, +and associates that ACL with all the public subnets. + +### Private subnets + +A private subnet may be able to initiate traffic to the public internet through a NAT gateway, +a NAT instance, or an egress-only internet gateway, or it might only have direct access to other +private subnets. In the simple configuration, for IPv4 and/or IPv6 with NAT64 enabled via `public_dns64_enabled` +or `private_dns64_enabled`, the module creates 1 NAT Gateway or NAT Instance for each +private subnet (in the public subnet in the same availability zone), creates 1 route table for each private subnet, +and adds to that route table a default route from the subnet to its NAT Gateway or Instance. For IPv6, +the module adds a route to the Egress-Only Internet Gateway configured via input. + +As with the Public subnets, the module creates a single Network ACL with associated rules allowing all ingress and +all egress, and associates that ACL with all the private subnets. + +### Customization for special use cases + +Various features are controlled by `bool` arguments with names ending in `_enabled`. By changing the default +values, you can enable or disable creation of public subnets, private subnets, route tables, +NAT gateways, NAT instances, or Network ACLs. So for example, you could use this module to create only +private subnets and the open Network ACL, and then add your own route table associations to the subnets +and route all non-local traffic to a Transit Gateway or VPN. + +### CIDR allocation + +For IPv4, you provide a CIDR and the module divides the address space into the largest CIDRs possible that are still +small enough to accommodate `max_subnet_count` subnets of each enabled type (public or private). When `max_subnet_count` +is left at the default `0`, it is set to the total number of availability zones in the region. Private subnets +are allocated out of the first half of the reserved range, and public subnets are allocated out of the second half. + +For IPv6, you provide a `/56` CIDR and the module assigns `/64` subnets of that CIDR in consecutive order starting +at zero. (You have the option of specifying a list of CIDRs instead.) As with IPv4, enough CIDRs are allocated to +cover `max_subnet_count` private and public subnets (when both are enabled, which is the default), with the private +subnets being allocated out of the lower half of the reservation and the public subnets allocated out of the upper half. + --- This project is part of our comprehensive ["SweetOps"](https://cpco.io/sweetops) approach towards DevOps. @@ -225,14 +279,14 @@ Available targets: | Name | Version | |------|---------| -| [terraform](#requirement\_terraform) | >= 0.13.0 | -| [aws](#requirement\_aws) | >= 3.62.0 | +| [terraform](#requirement\_terraform) | >= 1.1.0 | +| [aws](#requirement\_aws) | >= 3.71.0 | ## Providers | Name | Version | |------|---------| -| [aws](#provider\_aws) | 4.11.0 | +| [aws](#provider\_aws) | 4.13.0 | ## Modules @@ -250,28 +304,38 @@ Available targets: | Name | Type | |------|------| | [aws_eip.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eip) | resource | -| [aws_eip.nat_instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eip) | resource | | [aws_eip_association.nat_instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eip_association) | resource | | [aws_instance.nat_instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance) | resource | | [aws_nat_gateway.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/nat_gateway) | resource | | [aws_network_acl.private](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl) | resource | | [aws_network_acl.public](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl) | resource | -| [aws_route.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource | +| [aws_network_acl_rule.private4_egress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl_rule) | resource | +| [aws_network_acl_rule.private4_ingress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl_rule) | resource | +| [aws_network_acl_rule.private6_egress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl_rule) | resource | +| [aws_network_acl_rule.private6_ingress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl_rule) | resource | +| [aws_network_acl_rule.public4_egress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl_rule) | resource | +| [aws_network_acl_rule.public4_ingress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl_rule) | resource | +| [aws_network_acl_rule.public6_egress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl_rule) | resource | +| [aws_network_acl_rule.public6_ingress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl_rule) | resource | +| [aws_route.nat4](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource | | [aws_route.nat_instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource | +| [aws_route.private6](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource | +| [aws_route.private_nat64](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource | | [aws_route.public](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource | +| [aws_route.public6](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource | +| [aws_route.public_nat64](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource | | [aws_route_table.private](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table) | resource | | [aws_route_table.public](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table) | resource | | [aws_route_table_association.private](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table_association) | resource | | [aws_route_table_association.public](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table_association) | resource | -| [aws_route_table_association.public_default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table_association) | resource | | [aws_security_group.nat_instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource | | [aws_security_group_rule.nat_instance_egress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource | | [aws_security_group_rule.nat_instance_ingress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource | | [aws_subnet.private](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet) | resource | | [aws_subnet.public](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet) | resource | | [aws_ami.nat_instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source | -| [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source | -| [aws_eip.nat_ips](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eip) | data source | +| [aws_availability_zones.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source | +| [aws_eip.nat](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eip) | data source | | [aws_vpc.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source | ## Inputs @@ -280,63 +344,102 @@ Available targets: |------|-------------|------|---------|:--------:| | [additional\_tag\_map](#input\_additional\_tag\_map) | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.
This is for some rare cases where resources want additional configuration of tags
and therefore take a list of maps with tag key, value, and additional configuration. | `map(string)` | `{}` | no | | [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the `delimiter`
and treated as a single ID element. | `list(string)` | `[]` | no | -| [availability\_zone\_attribute\_style](#input\_availability\_zone\_attribute\_style) | The style of Availability Zone code to use in tags and names. One of `full`, `short`, or `fixed`. | `string` | `"short"` | no | -| [availability\_zones](#input\_availability\_zones) | List of Availability Zones where subnets will be created | `list(string)` | n/a | yes | -| [aws\_route\_create\_timeout](#input\_aws\_route\_create\_timeout) | Time to wait for AWS route creation specifed as a Go Duration, e.g. `2m` | `string` | `"2m"` | no | -| [aws\_route\_delete\_timeout](#input\_aws\_route\_delete\_timeout) | Time to wait for AWS route deletion specifed as a Go Duration, e.g. `5m` | `string` | `"5m"` | no | -| [cidr\_block](#input\_cidr\_block) | Base CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`) | `string` | n/a | yes | +| [availability\_zone\_attribute\_style](#input\_availability\_zone\_attribute\_style) | The style of Availability Zone code to use in tags and names. One of `full`, `short`, or `fixed`.
When using `availability_zone_ids`, IDs will first be translated into AZ names. | `string` | `"short"` | no | +| [availability\_zone\_ids](#input\_availability\_zone\_ids) | List of Availability Zones IDs where subnets will be created. Conflicts with `availability_zones`.
Useful in some regions when using only some AZs and you want to use the same ones across multiple accounts. | `list(string)` | `[]` | no | +| [availability\_zones](#input\_availability\_zones) | List of Availability Zones (AZs) where subnets will be created. Conflicts with `availability_zone_ids`.
The order of zones in the list ***must be stable*** or else Terraform will continually make changes.
If no AZs are specified, then `max_subnet_count` AZs will be selected in alphabetical order.
If `max_subnet_count > 0` and `length(var.availability_zones) > max_subnet_count`, the list
will be truncated. | `list(string)` | `[]` | no | +| [aws\_route\_create\_timeout](#input\_aws\_route\_create\_timeout) | DEPRECTATED: Use `route_create_timeout` instead.
Time to wait for AWS route creation, specified as a Go Duration, e.g. `2m` | `string` | `null` | no | +| [aws\_route\_delete\_timeout](#input\_aws\_route\_delete\_timeout) | DEPRECTATED: Use `route_delete_timeout` instead.
Time to wait for AWS route deletion, specified as a Go Duration, e.g. `2m` | `string` | `null` | no | +| [cidr\_block](#input\_cidr\_block) | DEPRECATED: Use `ipv4_cidr_block` instead.
Base IPv4 CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`) | `string` | `null` | no | | [context](#input\_context) | Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as `null` to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional\_tag\_map, which are merged. | `any` |
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"unset"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
| no | | [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no | | [descriptor\_formats](#input\_descriptor\_formats) | Describe additional descriptors to be output in the `descriptors` output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
`{
format = string
labels = list(string)
}`
(Type is `any` so the map values can later be enhanced to provide additional options.)
`format` is a Terraform format string to be passed to the `format()` function.
`labels` is a list of labels, in order, to pass to `format()` function.
Label values will be normalized before being passed to `format()` so they will be
identical to how they appear in `id`.
Default is `{}` (`descriptors` output will be empty). | `any` | `{}` | no | | [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no | | [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no | | [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).
Set to `0` for unlimited length.
Set to `null` for keep the existing setting, which defaults to `0`.
Does not affect `id_full`. | `number` | `null` | no | -| [igw\_id](#input\_igw\_id) | Internet Gateway ID the public route table will point to (e.g. `igw-9c26a123`) | `string` | n/a | yes | +| [igw\_id](#input\_igw\_id) | The Internet Gateway ID that the public subnets will route traffic to.
Used if `public_network_route_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | +| [ipv4\_cidr\_block](#input\_ipv4\_cidr\_block) | Base IPv4 CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`). Ignored if `ipv4_cidrs` is set.
If no CIDR block is provided, the VPC's default IPv4 CIDR block will be used. | `list(string)` | `[]` | no | +| [ipv4\_cidrs](#input\_ipv4\_cidrs) | Lists of CIDRs to assign to subnets. Order of CIDRs in the lists must not change over time.
Lists may contain more CIDRs than needed. |
list(object({
private = list(string)
public = list(string)
}))
| `[]` | no | +| [ipv4\_enabled](#input\_ipv4\_enabled) | Set true to enable IPv4 addresses in the subnets | `bool` | `true` | no | +| [ipv4\_private\_instance\_hostname\_type](#input\_ipv4\_private\_instance\_hostname\_type) | How to generate the DNS name for the instances in the private subnets.
Either `ip-name` to generate it from the IPv4 address, or
`resource-name` to generate it from the instance ID. | `string` | `"ip-name"` | no | +| [ipv4\_private\_instance\_hostnames\_enabled](#input\_ipv4\_private\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the private subnets will be answered with A (IPv4) records. | `bool` | `false` | no | +| [ipv4\_public\_instance\_hostname\_type](#input\_ipv4\_public\_instance\_hostname\_type) | How to generate the DNS name for the instances in the public subnets.
Either `ip-name` to generate it from the IPv4 address, or
`resource-name` to generate it from the instance ID. | `string` | `"ip-name"` | no | +| [ipv4\_public\_instance\_hostnames\_enabled](#input\_ipv4\_public\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the public subnets will be answered with A (IPv4) records. | `bool` | `false` | no | +| [ipv6\_cidr\_block](#input\_ipv6\_cidr\_block) | Base IPv6 CIDR block from which `/64` subnet CIDRs will be assigned. Must be `/56`. (e.g. `2600:1f16:c52:ab00::/56`).
Ignored if `ipv6_cidrs` is set. If no CIDR block is provided, the VPC's default IPv6 CIDR block will be used. | `list(string)` | `[]` | no | +| [ipv6\_cidrs](#input\_ipv6\_cidrs) | Lists of CIDRs to assign to subnets. Order of CIDRs in the lists must not change over time.
Lists may contain more CIDRs than needed. |
list(object({
private = list(string)
public = list(string)
}))
| `[]` | no | +| [ipv6\_egress\_only\_igw\_id](#input\_ipv6\_egress\_only\_igw\_id) | The Egress Only Internet Gateway ID the private IPv6 subnets will route traffic to.
Used if `private_network_route_enabled` is `true` and `ipv6_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | +| [ipv6\_enabled](#input\_ipv6\_enabled) | Set true to enable IPv6 addresses in the subnets | `bool` | `false` | no | +| [ipv6\_private\_instance\_hostnames\_enabled](#input\_ipv6\_private\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the private subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | +| [ipv6\_public\_instance\_hostnames\_enabled](#input\_ipv6\_public\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the public subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | | [label\_key\_case](#input\_label\_key\_case) | Controls the letter case of the `tags` keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper`.
Default value: `title`. | `string` | `null` | no | | [label\_order](#input\_label\_order) | The order in which the labels (ID elements) appear in the `id`.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. | `list(string)` | `null` | no | | [label\_value\_case](#input\_label\_value\_case) | Controls the letter case of ID elements (labels) as included in `id`,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper` and `none` (no transformation).
Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.
Default value: `lower`. | `string` | `null` | no | | [labels\_as\_tags](#input\_labels\_as\_tags) | Set of labels (ID elements) to include as tags in the `tags` output.
Default is to include all labels.
Tags with empty values will not be included in the `tags` output.
Set to `[]` to suppress all generated tags.
**Notes:**
The value of the `name` tag, if included, will be the `id`, not the `name`.
Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be
changed in later chained modules. Attempts to change it will be silently ignored. | `set(string)` |
[
"default"
]
| no | -| [map\_public\_ip\_on\_launch](#input\_map\_public\_ip\_on\_launch) | Instances launched into a public subnet should be assigned a public IP address | `bool` | `true` | no | -| [max\_subnet\_count](#input\_max\_subnet\_count) | Sets the maximum amount of subnets to deploy. 0 will deploy a subnet for every provided availablility zone (in `availability_zones` variable) within the region | `number` | `0` | no | -| [metadata\_http\_endpoint\_enabled](#input\_metadata\_http\_endpoint\_enabled) | Whether the metadata service is available | `bool` | `true` | no | -| [metadata\_http\_put\_response\_hop\_limit](#input\_metadata\_http\_put\_response\_hop\_limit) | The desired HTTP PUT response hop limit (between 1 and 64) for instance metadata requests. | `number` | `1` | no | -| [metadata\_http\_tokens\_required](#input\_metadata\_http\_tokens\_required) | Whether or not the metadata service requires session tokens, also referred to as Instance Metadata Service Version 2. | `bool` | `true` | no | +| [map\_public\_ip\_on\_launch](#input\_map\_public\_ip\_on\_launch) | If `true`, instances launched into a public subnet will be assigned a public IPv4 address | `bool` | `true` | no | +| [max\_subnet\_count](#input\_max\_subnet\_count) | Sets the maximum number of each type (public or private) of subnet to deploy.
0 will reserve a CIDR for every Availability Zone (excluding Local Zones) in the region, and
deploy a subnet in each availability zone specified in `availability_zones` or `availability_zone_ids`,
or every zone if none are specified. Recommended to set equal to the maximum number of AZs you anticipate using,
to avoid causing subnets to be destroyed and recreated with smaller IPv4 CIDRs when AWS adds an availability zone. | `number` | `0` | no | +| [metadata\_http\_endpoint\_enabled](#input\_metadata\_http\_endpoint\_enabled) | Whether the metadata service is available on the created NAT instances | `bool` | `true` | no | +| [metadata\_http\_put\_response\_hop\_limit](#input\_metadata\_http\_put\_response\_hop\_limit) | The desired HTTP PUT response hop limit (between 1 and 64) for instance metadata requests on the created NAT instances | `number` | `1` | no | +| [metadata\_http\_tokens\_required](#input\_metadata\_http\_tokens\_required) | Whether or not the metadata service requires session tokens, also referred to as Instance Metadata Service Version 2, on the created NAT instances | `bool` | `true` | no | | [name](#input\_name) | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a `tag`.
The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. | `string` | `null` | no | | [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no | -| [nat\_elastic\_ips](#input\_nat\_elastic\_ips) | Existing Elastic IPs to attach to the NAT Gateway(s) or Instance(s) instead of creating new ones. | `list(string)` | `[]` | no | +| [nat\_elastic\_ips](#input\_nat\_elastic\_ips) | Existing Elastic IPs (not EIP IDs) to attach to the NAT Gateway(s) or Instance(s) instead of creating new ones. | `list(string)` | `[]` | no | | [nat\_gateway\_enabled](#input\_nat\_gateway\_enabled) | Flag to enable/disable NAT Gateways to allow servers in the private subnets to access the Internet | `bool` | `true` | no | -| [nat\_instance\_cpu\_credits\_override](#input\_nat\_instance\_cpu\_credits\_override) | NAT Instance credit option for CPU usage. Valid values include standard or unlimited. T3 instances are launched as unlimited by default. T2 instances are launched as standard by default. | `string` | `""` | no | +| [nat\_instance\_ami\_id](#input\_nat\_instance\_ami\_id) | A list optionally containing the ID of the AMI to use for the NAT instance.
If the list is empty (the default), the latest official AWS NAT instance AMI
will be used. NOTE: The Official NAT instance AMI is being phased out and
does not support NAT64. Use of a NAT gateway is recommended instead. | `list(string)` | `[]` | no | +| [nat\_instance\_cpu\_credits\_override](#input\_nat\_instance\_cpu\_credits\_override) | NAT Instance credit option for CPU usage. Valid values are "standard" or "unlimited".
T3 and later instances are launched as unlimited by default. T2 instances are launched as standard by default. | `string` | `""` | no | | [nat\_instance\_enabled](#input\_nat\_instance\_enabled) | Flag to enable/disable NAT Instances to allow servers in the private subnets to access the Internet | `bool` | `false` | no | +| [nat\_instance\_root\_block\_device\_encrypted](#input\_nat\_instance\_root\_block\_device\_encrypted) | Whether to encrypt the root block device on the created NAT instances | `bool` | `true` | no | | [nat\_instance\_type](#input\_nat\_instance\_type) | NAT Instance type | `string` | `"t3.micro"` | no | -| [private\_network\_acl\_id](#input\_private\_network\_acl\_id) | Network ACL ID that will be added to private subnets. If empty, a new ACL will be created | `string` | `""` | no | +| [open\_network\_acl\_ipv4\_rule\_number](#input\_open\_network\_acl\_ipv4\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv4 traffic generated by this module | `number` | `100` | no | +| [open\_network\_acl\_ipv6\_rule\_number](#input\_open\_network\_acl\_ipv6\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv6 traffic generated by this module | `number` | `111` | no | +| [private\_assign\_ipv6\_address\_on\_creation](#input\_private\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a private subnet will be assigned an IPv6 address | `bool` | `true` | no | +| [private\_dns64\_enabled](#input\_private\_dns64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic IPv6 addresses for IPv4-only destinations.
Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_network_route_enabled` to be `true` to be fully operational. | `bool` | `true` | no | +| [private\_network\_acl\_id](#input\_private\_network\_acl\_id) | DEPRECATED: Use `private_open_network_acl_enabled` instead.
If set, no Network ACL will be created or assigned to the public subnets. | `string` | `""` | no | +| [private\_network\_route\_enabled](#input\_private\_network\_route\_enabled) | If true, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway
will be created for each private subnet (1:1). If false, you will need to create your own route table(s) and route(s). | `bool` | `true` | no | +| [private\_open\_network\_acl\_enabled](#input\_private\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every private subnet, and a rule (number 100)
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | | [private\_subnets\_additional\_tags](#input\_private\_subnets\_additional\_tags) | Additional tags to be added to private subnets | `map(string)` | `{}` | no | -| [public\_network\_acl\_id](#input\_public\_network\_acl\_id) | Network ACL ID that will be added to public subnets. If empty, a new ACL will be created | `string` | `""` | no | +| [private\_subnets\_enabled](#input\_private\_subnets\_enabled) | If false, do not create private subnets (or NAT gateways or instances) | `bool` | `true` | no | +| [public\_assign\_ipv6\_address\_on\_creation](#input\_public\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a public subnet will be assigned an IPv6 address | `bool` | `true` | no | +| [public\_dns64\_enabled](#input\_public\_dns64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in public subnets will return synthetic IPv6 addresses for IPv4-only destinations.
Requires `nat_gateway_enabled` and `public_network_route_enabled` to be `true` to be fully operational. | `bool` | `false` | no | +| [public\_network\_acl\_id](#input\_public\_network\_acl\_id) | DEPRECATED: Use `public_open_network_acl_enabled` instead.
If set, no Network ACL will be created or assigned to the public subnets. | `string` | `""` | no | +| [public\_network\_route\_enabled](#input\_public\_network\_route\_enabled) | If `true`, a default route to the internet gateway will be added route table(s) associated with the public subnets.
If `public_dns64_enabled` and `nat_gateway_enabled` are also `true`, a route for NAT64 addresses to the NAT gateway will also be created.
If `false`, you will need to create your own route(s). | `bool` | `true` | no | +| [public\_open\_network\_acl\_enabled](#input\_public\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every public subnet, and a rule
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | +| [public\_route\_table\_id](#input\_public\_route\_table\_id) | List optionally containing the ID of a single route table shared by all public subnets.
If provided, and `public_network_route_enabled` is `true`,
a route will be added to it, directing traffic to the VPC's Internet Gateway.
Ignored (treated as omitted) if `public_dns64_enabled` is `true`.
If omitted (or ignored), and `public_network_route_enabled` is `true`,
a network route table will be created for each public subnet (1:1) and routes added to it. | `list(string)` | `[]` | no | | [public\_subnets\_additional\_tags](#input\_public\_subnets\_additional\_tags) | Additional tags to be added to public subnets | `map(string)` | `{}` | no | +| [public\_subnets\_enabled](#input\_public\_subnets\_enabled) | If false, do not create public subnets.
Since NAT gateways and instances must be created in public subnets, these will also not be created when `false`.
Since the only default route for a private subnet is to a NAT gateway or instance,
route tables and routes will also not be created when `false`. | `bool` | `true` | no | | [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no | -| [root\_block\_device\_encrypted](#input\_root\_block\_device\_encrypted) | Whether to encrypt the root block device | `bool` | `true` | no | +| [root\_block\_device\_encrypted](#input\_root\_block\_device\_encrypted) | DEPRECATED: use `nat_instance_root_block_device_encrypted` instead.
Whether to encrypt the root block device on the created NAT instances | `bool` | `null` | no | +| [route\_create\_timeout](#input\_route\_create\_timeout) | Time to wait for a network routing table entry to be created, specified as a Go Duration, e.g. `2m` | `string` | `"5m"` | no | +| [route\_delete\_timeout](#input\_route\_delete\_timeout) | Time to wait for a network routing table entry to be deleted, specified as a Go Duration, e.g. `2m` | `string` | `"10m"` | no | | [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no | -| [subnet\_type\_tag\_key](#input\_subnet\_type\_tag\_key) | Key for subnet type tag to provide information about the type of subnets, e.g. `cpco.io/subnet/type=private` or `cpco.io/subnet/type=public` | `string` | `"cpco.io/subnet/type"` | no | -| [subnet\_type\_tag\_value\_format](#input\_subnet\_type\_tag\_value\_format) | This is using the format interpolation symbols to allow the value of the subnet\_type\_tag\_key to be modified. | `string` | `"%s"` | no | +| [subnet\_create\_timeout](#input\_subnet\_create\_timeout) | Time to wait for a subnet to be created, specified as a Go Duration, e.g. `2m` | `string` | `"10m"` | no | +| [subnet\_delete\_timeout](#input\_subnet\_delete\_timeout) | Time to wait for a subnet to be deleted, specified as a Go Duration, e.g. `5m` | `string` | `"20m"` | no | +| [subnet\_type\_tag\_key](#input\_subnet\_type\_tag\_key) | DEPRECATED: Use `public_subnets_additional_tags` and `private_subnets_additional_tags` instead
Key for subnet type tag to provide information about the type of subnets, e.g. `cpco.io/subnet/type: private` or `cpco.io/subnet/type: public` | `string` | `null` | no | +| [subnet\_type\_tag\_value\_format](#input\_subnet\_type\_tag\_value\_format) | DEPRECATED: Use `public_subnets_additional_tags` and `private_subnets_additional_tags` instead.
The value of the `subnet_type_tag_key` will be set to `format(var.subnet_type_tag_value_format, )`
where `` is either `public` or `private`. | `string` | `"%s"` | no | | [tags](#input\_tags) | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).
Neither the tag keys nor the tag values will be modified by this module. | `map(string)` | `{}` | no | | [tenant](#input\_tenant) | ID element \_(Rarely used, not included by default)\_. A customer identifier, indicating who this instance of a resource is for | `string` | `null` | no | -| [vpc\_default\_route\_table\_id](#input\_vpc\_default\_route\_table\_id) | Default route table for public subnets. If not set, will be created. (e.g. `rtb-f4f0ce12`) | `string` | `""` | no | | [vpc\_id](#input\_vpc\_id) | VPC ID where subnets will be created (e.g. `vpc-aceb2723`) | `string` | n/a | yes | ## Outputs | Name | Description | |------|-------------| +| [availability\_zone\_ids](#output\_availability\_zone\_ids) | List of Availability Zones IDs where subnets were created, when available | | [availability\_zones](#output\_availability\_zones) | List of Availability Zones where subnets were created | +| [nat\_eip\_allocation\_ids](#output\_nat\_eip\_allocation\_ids) | Elastic IP allocations in use by NAT | | [nat\_gateway\_ids](#output\_nat\_gateway\_ids) | IDs of the NAT Gateways created | -| [nat\_gateway\_public\_ips](#output\_nat\_gateway\_public\_ips) | EIP of the NAT Gateway | +| [nat\_gateway\_public\_ips](#output\_nat\_gateway\_public\_ips) | DEPRECATED: use `nat_ips` instead. Public IPv4 IP addresses in use by NAT. | +| [nat\_instance\_ami\_id](#output\_nat\_instance\_ami\_id) | ID of AMI used by NAT instance | | [nat\_instance\_ids](#output\_nat\_instance\_ids) | IDs of the NAT Instances created | -| [nat\_ips](#output\_nat\_ips) | IP Addresses in use for NAT | +| [nat\_ips](#output\_nat\_ips) | IP Addresses in use by NAT | +| [private\_network\_acl](#output\_private\_network\_acl) | n/a | | [private\_route\_table\_ids](#output\_private\_route\_table\_ids) | IDs of the created private route tables | -| [private\_subnet\_cidrs](#output\_private\_subnet\_cidrs) | CIDR blocks of the created private subnets | +| [private\_subnet\_cidrs](#output\_private\_subnet\_cidrs) | IPv4 CIDR blocks of the created private subnets | | [private\_subnet\_ids](#output\_private\_subnet\_ids) | IDs of the created private subnets | +| [private\_subnet\_ipv6\_cidrs](#output\_private\_subnet\_ipv6\_cidrs) | IPv6 CIDR blocks of the created private subnets | +| [public\_network\_acl](#output\_public\_network\_acl) | n/a | | [public\_route\_table\_ids](#output\_public\_route\_table\_ids) | IDs of the created public route tables | -| [public\_subnet\_cidrs](#output\_public\_subnet\_cidrs) | CIDR blocks of the created public subnets | +| [public\_subnet\_cidrs](#output\_public\_subnet\_cidrs) | IPv4 CIDR blocks of the created public subnets | | [public\_subnet\_ids](#output\_public\_subnet\_ids) | IDs of the created public subnets | +| [public\_subnet\_ipv6\_cidrs](#output\_public\_subnet\_ipv6\_cidrs) | IPv6 CIDR blocks of the created public subnets | diff --git a/README.yaml b/README.yaml index 0ebe2b2a..5c572458 100644 --- a/README.yaml +++ b/README.yaml @@ -49,9 +49,64 @@ related: description: |- Terraform module to provision public and private [`subnets`](http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Subnets.html) in an existing [`VPC`](https://aws.amazon.com/vpc) - **IMPORTANT:** This module provisions NAT instance with public IP. + __Note:__ this module is intended for use with an existing VPC and existing Internet Gateway. To create a new VPC, use [terraform-aws-vpc](https://github.com/cloudposse/terraform-aws-vpc) module. + + The core feature of this module is dividing up a given CIDR range so that a set of subnets each gets its own + distinct CIDR range within that range, and then creating those subnets in the appropriate availability zones. + The intention is to keep this module relatively simple and easy to use for the most popular use cases. + In its default configuration, this module creates 1 public subnet and 1 private subnet in each + of the specified availability zones. The public subnets are configured for bi-directional traffic to the + public internet, while the private subnets are configured for egress-only traffic to the public internet. + Rather than provide a wealth of configuration options allowing for numerous special cases, this module + provides some common options and further provides the ability to suppress the creation of resources, allowing + you to create and configure them as you like from outside this module. For example, rather than give you the + option to customize the Network ACL, the module gives you the option to create a completely open one (and control + access via Security Groups and other means) or not create one at all, allowing you to create and configure one yourself. + + ### Public subnets + + This module defines a public subnet as one that has direct access to an internet gateway and can accept incoming connection requests. + In the simplest configuration, the module creates a single route table with a default route targeted to the + VPC's internet gateway, and associates all the public subnets with that single route table. + + Likewise it creates a single Network ACL with associated rules allowing all ingress and all egress, + and associates that ACL with all the public subnets. + + ### Private subnets + + A private subnet may be able to initiate traffic to the public internet through a NAT gateway, + a NAT instance, or an egress-only internet gateway, or it might only have direct access to other + private subnets. In the simple configuration, for IPv4 and/or IPv6 with NAT64 enabled via `public_dns64_enabled` + or `private_dns64_enabled`, the module creates 1 NAT Gateway or NAT Instance for each + private subnet (in the public subnet in the same availability zone), creates 1 route table for each private subnet, + and adds to that route table a default route from the subnet to its NAT Gateway or Instance. For IPv6, + the module adds a route to the Egress-Only Internet Gateway configured via input. + + As with the Public subnets, the module creates a single Network ACL with associated rules allowing all ingress and + all egress, and associates that ACL with all the private subnets. + + ### Customization for special use cases + + Various features are controlled by `bool` arguments with names ending in `_enabled`. By changing the default + values, you can enable or disable creation of public subnets, private subnets, route tables, + NAT gateways, NAT instances, or Network ACLs. So for example, you could use this module to create only + private subnets and the open Network ACL, and then add your own route table associations to the subnets + and route all non-local traffic to a Transit Gateway or VPN. + + ### CIDR allocation + + For IPv4, you provide a CIDR and the module divides the address space into the largest CIDRs possible that are still + small enough to accommodate `max_subnet_count` subnets of each enabled type (public or private). When `max_subnet_count` + is left at the default `0`, it is set to the total number of availability zones in the region. Private subnets + are allocated out of the first half of the reserved range, and public subnets are allocated out of the second half. + + For IPv6, you provide a `/56` CIDR and the module assigns `/64` subnets of that CIDR in consecutive order starting + at zero. (You have the option of specifying a list of CIDRs instead.) As with IPv4, enough CIDRs are allocated to + cover `max_subnet_count` private and public subnets (when both are enabled, which is the default), with the private + subnets being allocated out of the lower half of the reservation and the public subnets allocated out of the upper half. + # How to use this project usage: |- ```hcl diff --git a/docs/terraform.md b/docs/terraform.md index 1a609f7a..aee92426 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -3,14 +3,14 @@ | Name | Version | |------|---------| -| [terraform](#requirement\_terraform) | >= 0.13.0 | -| [aws](#requirement\_aws) | >= 3.62.0 | +| [terraform](#requirement\_terraform) | >= 1.1.0 | +| [aws](#requirement\_aws) | >= 3.71.0 | ## Providers | Name | Version | |------|---------| -| [aws](#provider\_aws) | 4.11.0 | +| [aws](#provider\_aws) | 4.13.0 | ## Modules @@ -28,28 +28,38 @@ | Name | Type | |------|------| | [aws_eip.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eip) | resource | -| [aws_eip.nat_instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eip) | resource | | [aws_eip_association.nat_instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eip_association) | resource | | [aws_instance.nat_instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance) | resource | | [aws_nat_gateway.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/nat_gateway) | resource | | [aws_network_acl.private](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl) | resource | | [aws_network_acl.public](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl) | resource | -| [aws_route.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource | +| [aws_network_acl_rule.private4_egress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl_rule) | resource | +| [aws_network_acl_rule.private4_ingress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl_rule) | resource | +| [aws_network_acl_rule.private6_egress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl_rule) | resource | +| [aws_network_acl_rule.private6_ingress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl_rule) | resource | +| [aws_network_acl_rule.public4_egress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl_rule) | resource | +| [aws_network_acl_rule.public4_ingress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl_rule) | resource | +| [aws_network_acl_rule.public6_egress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl_rule) | resource | +| [aws_network_acl_rule.public6_ingress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl_rule) | resource | +| [aws_route.nat4](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource | | [aws_route.nat_instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource | +| [aws_route.private6](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource | +| [aws_route.private_nat64](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource | | [aws_route.public](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource | +| [aws_route.public6](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource | +| [aws_route.public_nat64](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource | | [aws_route_table.private](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table) | resource | | [aws_route_table.public](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table) | resource | | [aws_route_table_association.private](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table_association) | resource | | [aws_route_table_association.public](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table_association) | resource | -| [aws_route_table_association.public_default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table_association) | resource | | [aws_security_group.nat_instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource | | [aws_security_group_rule.nat_instance_egress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource | | [aws_security_group_rule.nat_instance_ingress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource | | [aws_subnet.private](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet) | resource | | [aws_subnet.public](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet) | resource | | [aws_ami.nat_instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source | -| [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source | -| [aws_eip.nat_ips](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eip) | data source | +| [aws_availability_zones.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source | +| [aws_eip.nat](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eip) | data source | | [aws_vpc.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source | ## Inputs @@ -58,61 +68,100 @@ |------|-------------|------|---------|:--------:| | [additional\_tag\_map](#input\_additional\_tag\_map) | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.
This is for some rare cases where resources want additional configuration of tags
and therefore take a list of maps with tag key, value, and additional configuration. | `map(string)` | `{}` | no | | [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the `delimiter`
and treated as a single ID element. | `list(string)` | `[]` | no | -| [availability\_zone\_attribute\_style](#input\_availability\_zone\_attribute\_style) | The style of Availability Zone code to use in tags and names. One of `full`, `short`, or `fixed`. | `string` | `"short"` | no | -| [availability\_zones](#input\_availability\_zones) | List of Availability Zones where subnets will be created | `list(string)` | n/a | yes | -| [aws\_route\_create\_timeout](#input\_aws\_route\_create\_timeout) | Time to wait for AWS route creation specifed as a Go Duration, e.g. `2m` | `string` | `"2m"` | no | -| [aws\_route\_delete\_timeout](#input\_aws\_route\_delete\_timeout) | Time to wait for AWS route deletion specifed as a Go Duration, e.g. `5m` | `string` | `"5m"` | no | -| [cidr\_block](#input\_cidr\_block) | Base CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`) | `string` | n/a | yes | +| [availability\_zone\_attribute\_style](#input\_availability\_zone\_attribute\_style) | The style of Availability Zone code to use in tags and names. One of `full`, `short`, or `fixed`.
When using `availability_zone_ids`, IDs will first be translated into AZ names. | `string` | `"short"` | no | +| [availability\_zone\_ids](#input\_availability\_zone\_ids) | List of Availability Zones IDs where subnets will be created. Conflicts with `availability_zones`.
Useful in some regions when using only some AZs and you want to use the same ones across multiple accounts. | `list(string)` | `[]` | no | +| [availability\_zones](#input\_availability\_zones) | List of Availability Zones (AZs) where subnets will be created. Conflicts with `availability_zone_ids`.
The order of zones in the list ***must be stable*** or else Terraform will continually make changes.
If no AZs are specified, then `max_subnet_count` AZs will be selected in alphabetical order.
If `max_subnet_count > 0` and `length(var.availability_zones) > max_subnet_count`, the list
will be truncated. | `list(string)` | `[]` | no | +| [aws\_route\_create\_timeout](#input\_aws\_route\_create\_timeout) | DEPRECTATED: Use `route_create_timeout` instead.
Time to wait for AWS route creation, specified as a Go Duration, e.g. `2m` | `string` | `null` | no | +| [aws\_route\_delete\_timeout](#input\_aws\_route\_delete\_timeout) | DEPRECTATED: Use `route_delete_timeout` instead.
Time to wait for AWS route deletion, specified as a Go Duration, e.g. `2m` | `string` | `null` | no | +| [cidr\_block](#input\_cidr\_block) | DEPRECATED: Use `ipv4_cidr_block` instead.
Base IPv4 CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`) | `string` | `null` | no | | [context](#input\_context) | Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as `null` to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional\_tag\_map, which are merged. | `any` |
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"unset"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
| no | | [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no | | [descriptor\_formats](#input\_descriptor\_formats) | Describe additional descriptors to be output in the `descriptors` output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
`{
format = string
labels = list(string)
}`
(Type is `any` so the map values can later be enhanced to provide additional options.)
`format` is a Terraform format string to be passed to the `format()` function.
`labels` is a list of labels, in order, to pass to `format()` function.
Label values will be normalized before being passed to `format()` so they will be
identical to how they appear in `id`.
Default is `{}` (`descriptors` output will be empty). | `any` | `{}` | no | | [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no | | [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no | | [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).
Set to `0` for unlimited length.
Set to `null` for keep the existing setting, which defaults to `0`.
Does not affect `id_full`. | `number` | `null` | no | -| [igw\_id](#input\_igw\_id) | Internet Gateway ID the public route table will point to (e.g. `igw-9c26a123`) | `string` | n/a | yes | +| [igw\_id](#input\_igw\_id) | The Internet Gateway ID that the public subnets will route traffic to.
Used if `public_network_route_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | +| [ipv4\_cidr\_block](#input\_ipv4\_cidr\_block) | Base IPv4 CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`). Ignored if `ipv4_cidrs` is set.
If no CIDR block is provided, the VPC's default IPv4 CIDR block will be used. | `list(string)` | `[]` | no | +| [ipv4\_cidrs](#input\_ipv4\_cidrs) | Lists of CIDRs to assign to subnets. Order of CIDRs in the lists must not change over time.
Lists may contain more CIDRs than needed. |
list(object({
private = list(string)
public = list(string)
}))
| `[]` | no | +| [ipv4\_enabled](#input\_ipv4\_enabled) | Set true to enable IPv4 addresses in the subnets | `bool` | `true` | no | +| [ipv4\_private\_instance\_hostname\_type](#input\_ipv4\_private\_instance\_hostname\_type) | How to generate the DNS name for the instances in the private subnets.
Either `ip-name` to generate it from the IPv4 address, or
`resource-name` to generate it from the instance ID. | `string` | `"ip-name"` | no | +| [ipv4\_private\_instance\_hostnames\_enabled](#input\_ipv4\_private\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the private subnets will be answered with A (IPv4) records. | `bool` | `false` | no | +| [ipv4\_public\_instance\_hostname\_type](#input\_ipv4\_public\_instance\_hostname\_type) | How to generate the DNS name for the instances in the public subnets.
Either `ip-name` to generate it from the IPv4 address, or
`resource-name` to generate it from the instance ID. | `string` | `"ip-name"` | no | +| [ipv4\_public\_instance\_hostnames\_enabled](#input\_ipv4\_public\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the public subnets will be answered with A (IPv4) records. | `bool` | `false` | no | +| [ipv6\_cidr\_block](#input\_ipv6\_cidr\_block) | Base IPv6 CIDR block from which `/64` subnet CIDRs will be assigned. Must be `/56`. (e.g. `2600:1f16:c52:ab00::/56`).
Ignored if `ipv6_cidrs` is set. If no CIDR block is provided, the VPC's default IPv6 CIDR block will be used. | `list(string)` | `[]` | no | +| [ipv6\_cidrs](#input\_ipv6\_cidrs) | Lists of CIDRs to assign to subnets. Order of CIDRs in the lists must not change over time.
Lists may contain more CIDRs than needed. |
list(object({
private = list(string)
public = list(string)
}))
| `[]` | no | +| [ipv6\_egress\_only\_igw\_id](#input\_ipv6\_egress\_only\_igw\_id) | The Egress Only Internet Gateway ID the private IPv6 subnets will route traffic to.
Used if `private_network_route_enabled` is `true` and `ipv6_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | +| [ipv6\_enabled](#input\_ipv6\_enabled) | Set true to enable IPv6 addresses in the subnets | `bool` | `false` | no | +| [ipv6\_private\_instance\_hostnames\_enabled](#input\_ipv6\_private\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the private subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | +| [ipv6\_public\_instance\_hostnames\_enabled](#input\_ipv6\_public\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the public subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | | [label\_key\_case](#input\_label\_key\_case) | Controls the letter case of the `tags` keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper`.
Default value: `title`. | `string` | `null` | no | | [label\_order](#input\_label\_order) | The order in which the labels (ID elements) appear in the `id`.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. | `list(string)` | `null` | no | | [label\_value\_case](#input\_label\_value\_case) | Controls the letter case of ID elements (labels) as included in `id`,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper` and `none` (no transformation).
Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.
Default value: `lower`. | `string` | `null` | no | | [labels\_as\_tags](#input\_labels\_as\_tags) | Set of labels (ID elements) to include as tags in the `tags` output.
Default is to include all labels.
Tags with empty values will not be included in the `tags` output.
Set to `[]` to suppress all generated tags.
**Notes:**
The value of the `name` tag, if included, will be the `id`, not the `name`.
Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be
changed in later chained modules. Attempts to change it will be silently ignored. | `set(string)` |
[
"default"
]
| no | -| [map\_public\_ip\_on\_launch](#input\_map\_public\_ip\_on\_launch) | Instances launched into a public subnet should be assigned a public IP address | `bool` | `true` | no | -| [max\_subnet\_count](#input\_max\_subnet\_count) | Sets the maximum amount of subnets to deploy. 0 will deploy a subnet for every provided availablility zone (in `availability_zones` variable) within the region | `number` | `0` | no | -| [metadata\_http\_endpoint\_enabled](#input\_metadata\_http\_endpoint\_enabled) | Whether the metadata service is available | `bool` | `true` | no | -| [metadata\_http\_put\_response\_hop\_limit](#input\_metadata\_http\_put\_response\_hop\_limit) | The desired HTTP PUT response hop limit (between 1 and 64) for instance metadata requests. | `number` | `1` | no | -| [metadata\_http\_tokens\_required](#input\_metadata\_http\_tokens\_required) | Whether or not the metadata service requires session tokens, also referred to as Instance Metadata Service Version 2. | `bool` | `true` | no | +| [map\_public\_ip\_on\_launch](#input\_map\_public\_ip\_on\_launch) | If `true`, instances launched into a public subnet will be assigned a public IPv4 address | `bool` | `true` | no | +| [max\_subnet\_count](#input\_max\_subnet\_count) | Sets the maximum number of each type (public or private) of subnet to deploy.
0 will reserve a CIDR for every Availability Zone (excluding Local Zones) in the region, and
deploy a subnet in each availability zone specified in `availability_zones` or `availability_zone_ids`,
or every zone if none are specified. Recommended to set equal to the maximum number of AZs you anticipate using,
to avoid causing subnets to be destroyed and recreated with smaller IPv4 CIDRs when AWS adds an availability zone. | `number` | `0` | no | +| [metadata\_http\_endpoint\_enabled](#input\_metadata\_http\_endpoint\_enabled) | Whether the metadata service is available on the created NAT instances | `bool` | `true` | no | +| [metadata\_http\_put\_response\_hop\_limit](#input\_metadata\_http\_put\_response\_hop\_limit) | The desired HTTP PUT response hop limit (between 1 and 64) for instance metadata requests on the created NAT instances | `number` | `1` | no | +| [metadata\_http\_tokens\_required](#input\_metadata\_http\_tokens\_required) | Whether or not the metadata service requires session tokens, also referred to as Instance Metadata Service Version 2, on the created NAT instances | `bool` | `true` | no | | [name](#input\_name) | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a `tag`.
The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. | `string` | `null` | no | | [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no | -| [nat\_elastic\_ips](#input\_nat\_elastic\_ips) | Existing Elastic IPs to attach to the NAT Gateway(s) or Instance(s) instead of creating new ones. | `list(string)` | `[]` | no | +| [nat\_elastic\_ips](#input\_nat\_elastic\_ips) | Existing Elastic IPs (not EIP IDs) to attach to the NAT Gateway(s) or Instance(s) instead of creating new ones. | `list(string)` | `[]` | no | | [nat\_gateway\_enabled](#input\_nat\_gateway\_enabled) | Flag to enable/disable NAT Gateways to allow servers in the private subnets to access the Internet | `bool` | `true` | no | -| [nat\_instance\_cpu\_credits\_override](#input\_nat\_instance\_cpu\_credits\_override) | NAT Instance credit option for CPU usage. Valid values include standard or unlimited. T3 instances are launched as unlimited by default. T2 instances are launched as standard by default. | `string` | `""` | no | +| [nat\_instance\_ami\_id](#input\_nat\_instance\_ami\_id) | A list optionally containing the ID of the AMI to use for the NAT instance.
If the list is empty (the default), the latest official AWS NAT instance AMI
will be used. NOTE: The Official NAT instance AMI is being phased out and
does not support NAT64. Use of a NAT gateway is recommended instead. | `list(string)` | `[]` | no | +| [nat\_instance\_cpu\_credits\_override](#input\_nat\_instance\_cpu\_credits\_override) | NAT Instance credit option for CPU usage. Valid values are "standard" or "unlimited".
T3 and later instances are launched as unlimited by default. T2 instances are launched as standard by default. | `string` | `""` | no | | [nat\_instance\_enabled](#input\_nat\_instance\_enabled) | Flag to enable/disable NAT Instances to allow servers in the private subnets to access the Internet | `bool` | `false` | no | +| [nat\_instance\_root\_block\_device\_encrypted](#input\_nat\_instance\_root\_block\_device\_encrypted) | Whether to encrypt the root block device on the created NAT instances | `bool` | `true` | no | | [nat\_instance\_type](#input\_nat\_instance\_type) | NAT Instance type | `string` | `"t3.micro"` | no | -| [private\_network\_acl\_id](#input\_private\_network\_acl\_id) | Network ACL ID that will be added to private subnets. If empty, a new ACL will be created | `string` | `""` | no | +| [open\_network\_acl\_ipv4\_rule\_number](#input\_open\_network\_acl\_ipv4\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv4 traffic generated by this module | `number` | `100` | no | +| [open\_network\_acl\_ipv6\_rule\_number](#input\_open\_network\_acl\_ipv6\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv6 traffic generated by this module | `number` | `111` | no | +| [private\_assign\_ipv6\_address\_on\_creation](#input\_private\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a private subnet will be assigned an IPv6 address | `bool` | `true` | no | +| [private\_dns64\_enabled](#input\_private\_dns64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic IPv6 addresses for IPv4-only destinations.
Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_network_route_enabled` to be `true` to be fully operational. | `bool` | `true` | no | +| [private\_network\_acl\_id](#input\_private\_network\_acl\_id) | DEPRECATED: Use `private_open_network_acl_enabled` instead.
If set, no Network ACL will be created or assigned to the public subnets. | `string` | `""` | no | +| [private\_network\_route\_enabled](#input\_private\_network\_route\_enabled) | If true, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway
will be created for each private subnet (1:1). If false, you will need to create your own route table(s) and route(s). | `bool` | `true` | no | +| [private\_open\_network\_acl\_enabled](#input\_private\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every private subnet, and a rule (number 100)
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | | [private\_subnets\_additional\_tags](#input\_private\_subnets\_additional\_tags) | Additional tags to be added to private subnets | `map(string)` | `{}` | no | -| [public\_network\_acl\_id](#input\_public\_network\_acl\_id) | Network ACL ID that will be added to public subnets. If empty, a new ACL will be created | `string` | `""` | no | +| [private\_subnets\_enabled](#input\_private\_subnets\_enabled) | If false, do not create private subnets (or NAT gateways or instances) | `bool` | `true` | no | +| [public\_assign\_ipv6\_address\_on\_creation](#input\_public\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a public subnet will be assigned an IPv6 address | `bool` | `true` | no | +| [public\_dns64\_enabled](#input\_public\_dns64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in public subnets will return synthetic IPv6 addresses for IPv4-only destinations.
Requires `nat_gateway_enabled` and `public_network_route_enabled` to be `true` to be fully operational. | `bool` | `false` | no | +| [public\_network\_acl\_id](#input\_public\_network\_acl\_id) | DEPRECATED: Use `public_open_network_acl_enabled` instead.
If set, no Network ACL will be created or assigned to the public subnets. | `string` | `""` | no | +| [public\_network\_route\_enabled](#input\_public\_network\_route\_enabled) | If `true`, a default route to the internet gateway will be added route table(s) associated with the public subnets.
If `public_dns64_enabled` and `nat_gateway_enabled` are also `true`, a route for NAT64 addresses to the NAT gateway will also be created.
If `false`, you will need to create your own route(s). | `bool` | `true` | no | +| [public\_open\_network\_acl\_enabled](#input\_public\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every public subnet, and a rule
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | +| [public\_route\_table\_id](#input\_public\_route\_table\_id) | List optionally containing the ID of a single route table shared by all public subnets.
If provided, and `public_network_route_enabled` is `true`,
a route will be added to it, directing traffic to the VPC's Internet Gateway.
Ignored (treated as omitted) if `public_dns64_enabled` is `true`.
If omitted (or ignored), and `public_network_route_enabled` is `true`,
a network route table will be created for each public subnet (1:1) and routes added to it. | `list(string)` | `[]` | no | | [public\_subnets\_additional\_tags](#input\_public\_subnets\_additional\_tags) | Additional tags to be added to public subnets | `map(string)` | `{}` | no | +| [public\_subnets\_enabled](#input\_public\_subnets\_enabled) | If false, do not create public subnets.
Since NAT gateways and instances must be created in public subnets, these will also not be created when `false`.
Since the only default route for a private subnet is to a NAT gateway or instance,
route tables and routes will also not be created when `false`. | `bool` | `true` | no | | [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no | -| [root\_block\_device\_encrypted](#input\_root\_block\_device\_encrypted) | Whether to encrypt the root block device | `bool` | `true` | no | +| [root\_block\_device\_encrypted](#input\_root\_block\_device\_encrypted) | DEPRECATED: use `nat_instance_root_block_device_encrypted` instead.
Whether to encrypt the root block device on the created NAT instances | `bool` | `null` | no | +| [route\_create\_timeout](#input\_route\_create\_timeout) | Time to wait for a network routing table entry to be created, specified as a Go Duration, e.g. `2m` | `string` | `"5m"` | no | +| [route\_delete\_timeout](#input\_route\_delete\_timeout) | Time to wait for a network routing table entry to be deleted, specified as a Go Duration, e.g. `2m` | `string` | `"10m"` | no | | [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no | -| [subnet\_type\_tag\_key](#input\_subnet\_type\_tag\_key) | Key for subnet type tag to provide information about the type of subnets, e.g. `cpco.io/subnet/type=private` or `cpco.io/subnet/type=public` | `string` | `"cpco.io/subnet/type"` | no | -| [subnet\_type\_tag\_value\_format](#input\_subnet\_type\_tag\_value\_format) | This is using the format interpolation symbols to allow the value of the subnet\_type\_tag\_key to be modified. | `string` | `"%s"` | no | +| [subnet\_create\_timeout](#input\_subnet\_create\_timeout) | Time to wait for a subnet to be created, specified as a Go Duration, e.g. `2m` | `string` | `"10m"` | no | +| [subnet\_delete\_timeout](#input\_subnet\_delete\_timeout) | Time to wait for a subnet to be deleted, specified as a Go Duration, e.g. `5m` | `string` | `"20m"` | no | +| [subnet\_type\_tag\_key](#input\_subnet\_type\_tag\_key) | DEPRECATED: Use `public_subnets_additional_tags` and `private_subnets_additional_tags` instead
Key for subnet type tag to provide information about the type of subnets, e.g. `cpco.io/subnet/type: private` or `cpco.io/subnet/type: public` | `string` | `null` | no | +| [subnet\_type\_tag\_value\_format](#input\_subnet\_type\_tag\_value\_format) | DEPRECATED: Use `public_subnets_additional_tags` and `private_subnets_additional_tags` instead.
The value of the `subnet_type_tag_key` will be set to `format(var.subnet_type_tag_value_format, )`
where `` is either `public` or `private`. | `string` | `"%s"` | no | | [tags](#input\_tags) | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).
Neither the tag keys nor the tag values will be modified by this module. | `map(string)` | `{}` | no | | [tenant](#input\_tenant) | ID element \_(Rarely used, not included by default)\_. A customer identifier, indicating who this instance of a resource is for | `string` | `null` | no | -| [vpc\_default\_route\_table\_id](#input\_vpc\_default\_route\_table\_id) | Default route table for public subnets. If not set, will be created. (e.g. `rtb-f4f0ce12`) | `string` | `""` | no | | [vpc\_id](#input\_vpc\_id) | VPC ID where subnets will be created (e.g. `vpc-aceb2723`) | `string` | n/a | yes | ## Outputs | Name | Description | |------|-------------| +| [availability\_zone\_ids](#output\_availability\_zone\_ids) | List of Availability Zones IDs where subnets were created, when available | | [availability\_zones](#output\_availability\_zones) | List of Availability Zones where subnets were created | +| [nat\_eip\_allocation\_ids](#output\_nat\_eip\_allocation\_ids) | Elastic IP allocations in use by NAT | | [nat\_gateway\_ids](#output\_nat\_gateway\_ids) | IDs of the NAT Gateways created | -| [nat\_gateway\_public\_ips](#output\_nat\_gateway\_public\_ips) | EIP of the NAT Gateway | +| [nat\_gateway\_public\_ips](#output\_nat\_gateway\_public\_ips) | DEPRECATED: use `nat_ips` instead. Public IPv4 IP addresses in use by NAT. | +| [nat\_instance\_ami\_id](#output\_nat\_instance\_ami\_id) | ID of AMI used by NAT instance | | [nat\_instance\_ids](#output\_nat\_instance\_ids) | IDs of the NAT Instances created | -| [nat\_ips](#output\_nat\_ips) | IP Addresses in use for NAT | +| [nat\_ips](#output\_nat\_ips) | IP Addresses in use by NAT | +| [private\_network\_acl](#output\_private\_network\_acl) | n/a | | [private\_route\_table\_ids](#output\_private\_route\_table\_ids) | IDs of the created private route tables | -| [private\_subnet\_cidrs](#output\_private\_subnet\_cidrs) | CIDR blocks of the created private subnets | +| [private\_subnet\_cidrs](#output\_private\_subnet\_cidrs) | IPv4 CIDR blocks of the created private subnets | | [private\_subnet\_ids](#output\_private\_subnet\_ids) | IDs of the created private subnets | +| [private\_subnet\_ipv6\_cidrs](#output\_private\_subnet\_ipv6\_cidrs) | IPv6 CIDR blocks of the created private subnets | +| [public\_network\_acl](#output\_public\_network\_acl) | n/a | | [public\_route\_table\_ids](#output\_public\_route\_table\_ids) | IDs of the created public route tables | -| [public\_subnet\_cidrs](#output\_public\_subnet\_cidrs) | CIDR blocks of the created public subnets | +| [public\_subnet\_cidrs](#output\_public\_subnet\_cidrs) | IPv4 CIDR blocks of the created public subnets | | [public\_subnet\_ids](#output\_public\_subnet\_ids) | IDs of the created public subnets | +| [public\_subnet\_ipv6\_cidrs](#output\_public\_subnet\_ipv6\_cidrs) | IPv6 CIDR blocks of the created public subnets | diff --git a/examples/complete/main.tf b/examples/complete/main.tf index 5668e332..ab22429d 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -4,24 +4,35 @@ provider "aws" { module "vpc" { source = "cloudposse/vpc/aws" - version = "0.18.1" + version = "1.1.0" cidr_block = "172.16.0.0/16" + assign_generated_ipv6_cidr_block = true + ipv6_egress_only_internet_gateway_enabled = true + context = module.this.context } module "subnets" { source = "../../" + #source = "cloudposse/dynamic-subnets/aws" + #version = "1.0.0" availability_zones = var.availability_zones vpc_id = module.vpc.vpc_id - igw_id = module.vpc.igw_id - cidr_block = module.vpc.vpc_cidr_block + igw_id = [module.vpc.igw_id] + ipv4_enabled = true + ipv6_enabled = true + ipv6_egress_only_igw_id = [module.vpc.ipv6_egress_only_igw_id] + ipv4_cidr_block = [module.vpc.vpc_cidr_block] + ipv6_cidr_block = [module.vpc.vpc_ipv6_cidr_block] nat_gateway_enabled = false nat_instance_enabled = false aws_route_create_timeout = "5m" aws_route_delete_timeout = "10m" + subnet_type_tag_key = "cpco.io/subnet/type" + context = module.this.context } diff --git a/examples/complete/outputs.tf b/examples/complete/outputs.tf index 575e42e4..2496e57f 100644 --- a/examples/complete/outputs.tf +++ b/examples/complete/outputs.tf @@ -5,3 +5,25 @@ output "public_subnet_cidrs" { output "private_subnet_cidrs" { value = module.subnets.private_subnet_cidrs } + +output "public_subnet_ipv6_cidrs" { + value = module.subnets.public_subnet_ipv6_cidrs +} + +output "private_subnet_ipv6_cidrs" { + value = module.subnets.private_subnet_ipv6_cidrs +} + +output "vpc_ipv6_cidr" { + value = module.vpc.vpc_ipv6_cidr_block +} + +output "public_route_table_ids" { + description = "IDs of the created public route tables" + value = module.subnets.public_route_table_ids +} + +output "private_route_table_ids" { + description = "IDs of the created private route tables" + value = module.subnets.private_route_table_ids +} diff --git a/main.tf b/main.tf index 2c92654d..507c99cb 100644 --- a/main.tf +++ b/main.tf @@ -1,33 +1,206 @@ -# Get object aws_vpc by vpc_id -data "aws_vpc" "default" { - count = local.enabled ? 1 : 0 - id = var.vpc_id +# Process all the optional inputs into a fully fleshed out configuration +# the rest of the code can consume. + +locals { + enabled = module.this.enabled && (var.public_subnets_enabled || var.private_subnets_enabled) && (var.ipv4_enabled || var.ipv6_enabled) + + # We are going to reference `enabled` a *lot*, so abbreviate it + e = local.enabled + + # Use delimiter shortcut for creating AZ-based Names/IDs + delimiter = module.this.delimiter + + # The RFC 6052 "well known" NAT64 CIDR + nat64_cidr = "64:ff9b::/96" + + # In case we later decide to compute it + vpc_id = var.vpc_id + + ##################################################################### + ## Determine the set of availability zones in which to deploy subnets + # Priority is + # - availability_zone_ids + # - availability_zones + # - data.aws_availability_zones.default + + use_az_ids = local.e && length(var.availability_zone_ids) > 0 + use_az_var = local.e && length(var.availability_zones) > 0 + use_default_azs = local.e && !(local.use_az_ids || local.use_az_var) + + # Create a map of AZ IDs to AZ names (and the reverse), + # but fail safely, because AZ IDs are not always available. + az_id_map = try(zipmap(data.aws_availability_zones.default[0].zone_ids, data.aws_availability_zones.default[0].names), {}) + az_name_map = try(zipmap(data.aws_availability_zones.default[0].names, data.aws_availability_zones.default[0].zone_ids), {}) + + # Create a map of options, not necessarily all filled in, to separate creating the option + # from selecting the option, making the code easier to understand. + az_option_map = { + from_az_ids = local.e ? [for id in var.availability_zone_ids : local.az_id_map[id]] : [] + from_az_var = local.e ? var.availability_zones : [] + all_azs = local.e ? sort(data.aws_availability_zones.default[0].names) : [] + } + + subnet_availability_zone_option = local.use_az_ids ? "from_az_ids" : ( + local.use_az_var ? "from_az_var" : "all_azs" + ) + + subnet_possible_availability_zones = local.az_option_map[local.subnet_availability_zone_option] + + # Adjust list according to `max_subnet_count` + subnet_availability_zones = slice(local.subnet_possible_availability_zones, 0, var.max_subnet_count > 0 ? var.max_subnet_count : length(local.subnet_possible_availability_zones)) + + subnet_az_count = local.e ? length(local.subnet_availability_zones) : 0 + subnet_count = ((local.public_enabled ? 1 : 0) + (local.private_enabled ? 1 : 0)) * local.subnet_az_count + + # Lookup the abbreviations for the availability zones we are using + az_abbreviation_map_map = { + short = "to_short" + fixed = "to_fixed" + full = "identity" + } + az_abbreviation_map = module.utils.region_az_alt_code_maps[local.az_abbreviation_map_map[var.availability_zone_attribute_style]] + + subnet_az_abbreviations = [for az in local.subnet_availability_zones : local.az_abbreviation_map[az]] + + ################### End of Availability Zone Normalization ####################### + + ######################################### + # Configure subnet CIDRs + + # Figure out how many CIDRs to reserve. By default, we often reserve more CIDRs than we need so that + # with future growth, we can add subnets without affecting existing subnets. + existing_az_count = local.e ? length(data.aws_availability_zones.default[0].names) : 0 + base_cidr_reservations = var.max_subnet_count == 0 ? local.existing_az_count : var.max_subnet_count + private_cidr_reservations = (local.private_enabled ? 1 : 0) * local.base_cidr_reservations + public_cidr_reservations = (local.public_enabled ? 1 : 0) * local.base_cidr_reservations + cidr_reservations = local.private_cidr_reservations + local.public_cidr_reservations + + + # Calculate how many bits are required to designate a subnet, + # but also prevent errors like log(0) when things are disabled. + required_ipv4_subnet_bits = local.e ? ceil(log(local.cidr_reservations, 2)) : 1 + required_ipv6_subnet_bits = 8 # Currently the only value allowed by AWS + + supplied_ipv4_private_subnet_cidrs = try(var.ipv4_cidrs[0].private, []) + supplied_ipv4_public_subnet_cidrs = try(var.ipv4_cidrs[0].public, []) + + supplied_ipv6_private_subnet_cidrs = try(var.ipv6_cidrs[0].private, []) + supplied_ipv6_public_subnet_cidrs = try(var.ipv6_cidrs[0].public, []) + + compute_ipv4_cidrs = local.ipv4_enabled && (length(local.supplied_ipv4_private_subnet_cidrs) + length(local.supplied_ipv4_public_subnet_cidrs)) == 0 + compute_ipv6_cidrs = local.ipv6_enabled && (length(local.supplied_ipv6_private_subnet_cidrs) + length(local.supplied_ipv6_public_subnet_cidrs)) == 0 + need_vpc_data = (local.compute_ipv4_cidrs && length(var.ipv4_cidr_block) == 0) || (local.compute_ipv6_cidrs && length(var.ipv6_cidr_block) == 0) + + base_ipv4_cidr_block = length(var.ipv4_cidr_block) > 0 ? var.ipv4_cidr_block[0] : (local.need_vpc_data ? data.aws_vpc.default[0].cidr_block : "") + base_ipv6_cidr_block = length(var.ipv6_cidr_block) > 0 ? var.ipv6_cidr_block[0] : (local.need_vpc_data ? data.aws_vpc.default[0].ipv6_cidr_block : "") + + # For backward compatibility, private subnets get the lower CIDR range + ipv4_private_subnet_cidrs = local.compute_ipv4_cidrs ? [ + for net in range(0, local.private_cidr_reservations) : cidrsubnet(local.base_ipv4_cidr_block, local.required_ipv4_subnet_bits, net) + ] : local.supplied_ipv4_private_subnet_cidrs + ipv4_public_subnet_cidrs = local.compute_ipv4_cidrs ? [ + for net in range(local.private_cidr_reservations, local.cidr_reservations) : cidrsubnet(local.base_ipv4_cidr_block, local.required_ipv4_subnet_bits, net) + ] : local.supplied_ipv4_public_subnet_cidrs + + ipv6_private_subnet_cidrs = local.compute_ipv6_cidrs ? [ + for net in range(0, local.private_cidr_reservations) : cidrsubnet(local.base_ipv6_cidr_block, local.required_ipv6_subnet_bits, net) + ] : local.supplied_ipv6_private_subnet_cidrs + ipv6_public_subnet_cidrs = local.compute_ipv6_cidrs ? [ + for net in range(local.private_cidr_reservations, local.cidr_reservations) : cidrsubnet(local.base_ipv6_cidr_block, local.required_ipv6_subnet_bits, net) + ] : local.supplied_ipv6_public_subnet_cidrs + + ################### End of CIDR configuration ####################### + + ########################################## + # Tick off the list of things to create + + public_enabled = local.e && var.public_subnets_enabled + private_enabled = local.e && var.private_subnets_enabled + ipv4_enabled = local.e && var.ipv4_enabled + ipv6_enabled = local.e && var.ipv6_enabled + + igw_configured = length(var.igw_id) > 0 + # ipv6_egress_only_configured indicates if the configuration *supports* the use of + # an IPv6 Egress-only Internet Gateway, not if it *requires* its use. + ipv6_egress_only_configured = local.ipv6_enabled && length(var.ipv6_egress_only_igw_id) > 0 + + public4_enabled = local.public_enabled && local.ipv4_enabled + public6_enabled = local.public_enabled && local.ipv6_enabled + private4_enabled = local.private_enabled && local.ipv4_enabled + private6_enabled = local.private_enabled && local.ipv6_enabled + + public_dns64_enabled = local.public6_enabled && var.public_dns64_enabled + private_dns64_enabled = local.private6_enabled && var.private_dns64_enabled + + public_network_route_enabled = local.public_enabled && var.public_network_route_enabled + public_network_table_enabled = local.public_network_route_enabled && (length(var.public_route_table_id) == 0 || local.public_dns64_enabled) + public_network_table_count = local.public_network_table_enabled ? (local.public_dns64_enabled ? local.subnet_az_count : 1) : 0 + public_network_table_ids = local.public_network_table_enabled ? aws_route_table.public.*.id : var.public_route_table_id + + private_network_route_enabled = local.private_enabled && (local.nat_enabled || local.ipv6_egress_only_configured) && var.private_network_route_enabled + private_network_table_enabled = local.private_network_route_enabled + private_network_table_count = local.private_network_table_enabled ? local.subnet_az_count : 0 + private_network_table_ids = local.private_network_table_enabled ? aws_route_table.private.*.id : [] + + # public and private network ACLs + # Support deprecated var.public_network_acl_id + public_open_network_acl_enabled = local.public_enabled && (length(var.public_network_acl_id) > 0 ? false : var.public_open_network_acl_enabled) + # Support deprecated var.private_network_acl_id + private_open_network_acl_enabled = local.private_enabled && (length(var.private_network_acl_id) > 0 ? false : var.private_open_network_acl_enabled) + + # A NAT device is needed to NAT from private IPv4 to public IPv4 or to perform NAT64 for IPv6, + # but since it must be placed in a public subnet, we consider it not required if we are not creating public subnets. + nat_required = local.public_enabled && (local.private4_enabled || local.public_dns64_enabled) + nat_gateway_enabled = local.nat_required && var.nat_gateway_enabled + # An AWS NAT instance does not perform NAT64, but we allow for one anyway because + # the user can supply their own NAT instance AMI that does support it. + nat_instance_enabled = local.nat_required && var.nat_instance_enabled + nat_enabled = local.nat_gateway_enabled || local.nat_instance_enabled + need_nat_eips = local.nat_enabled && length(var.nat_elastic_ips) == 0 + need_nat_eip_data = local.nat_enabled && length(var.nat_elastic_ips) > 0 + nat_eip_allocations = local.nat_enabled ? (local.need_nat_eips ? aws_eip.default.*.id : data.aws_eip.nat.*.id) : [] + + need_nat_ami_id = local.nat_instance_enabled && length(var.nat_instance_ami_id) == 0 + nat_instance_ami_id = local.need_nat_ami_id ? data.aws_ami.nat_instance[0].id : try(var.nat_instance_ami_id[0], "") + } -data "aws_availability_zones" "available" { - count = local.enabled_count +data "aws_availability_zones" "default" { + count = local.enabled ? 1 : 0 + + # Filter out Local Zones. See https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones#by-filter + filter { + name = "opt-in-status" + values = ["opt-in-not-required"] + } } -locals { - availability_zones_count = local.enabled ? length(var.availability_zones) : 0 - enabled = module.this.enabled - enabled_count = local.enabled ? 1 : 0 - delimiter = module.this.delimiter +data "aws_vpc" "default" { + count = local.need_vpc_data ? 1 : 0 + + id = local.vpc_id } -data "aws_eip" "nat_ips" { - count = local.enabled ? length(var.nat_elastic_ips) : 0 +data "aws_eip" "nat" { + count = local.need_nat_eip_data ? length(var.nat_elastic_ips) : 0 public_ip = element(var.nat_elastic_ips, count.index) } -locals { - use_existing_eips = length(var.nat_elastic_ips) > 0 - map_map = { - short = "to_short" - fixed = "to_fixed" - full = "identity" + +resource "aws_eip" "default" { + count = local.need_nat_eips ? local.subnet_az_count : 0 + vpc = true + + tags = merge( + module.nat_label.tags, + { + "Name" = format("%s%s%s", module.nat_label.id, local.delimiter, local.subnet_az_abbreviations[count.index]) + } + ) + + lifecycle { + create_before_destroy = true } - az_map = module.utils.region_az_alt_code_maps[local.map_map[var.availability_zone_attribute_style]] } module "utils" { diff --git a/moved.tf b/moved.tf new file mode 100644 index 00000000..b235b64c --- /dev/null +++ b/moved.tf @@ -0,0 +1,16 @@ +# This file is just for "moved" declarations for backward compatibility + +moved { + from = aws_eip.nat_instance + to = aws_eip.default +} + +moved { + from = aws_route.default + to = aws_route.nat4 +} + +moved { + from = aws_route_table_association.public_default + to = aws_route_table_association.public +} \ No newline at end of file diff --git a/nat-gateway.tf b/nat-gateway.tf index 9c7abc9d..e92da423 100644 --- a/nat-gateway.tf +++ b/nat-gateway.tf @@ -7,21 +7,16 @@ module "nat_label" { context = module.this.context } -locals { - nat_gateway_eip_count = local.use_existing_eips ? 0 : local.nat_gateways_count - gateway_eip_allocations = local.use_existing_eips ? data.aws_eip.nat_ips.*.id : aws_eip.default.*.id - eips_allocations = local.use_existing_eips ? data.aws_eip.nat_ips.*.id : aws_eip.default.*.id - nat_gateways_count = var.nat_gateway_enabled ? length(var.availability_zones) : 0 -} +resource "aws_nat_gateway" "default" { + count = local.nat_gateway_enabled ? local.subnet_az_count : 0 -resource "aws_eip" "default" { - count = local.enabled ? local.nat_gateway_eip_count : 0 - vpc = true + allocation_id = local.nat_eip_allocations[count.index] + subnet_id = aws_subnet.public[count.index].id tags = merge( - module.private_label.tags, + module.nat_label.tags, { - "Name" = format("%s%s%s", module.private_label.id, local.delimiter, local.az_map[element(var.availability_zones, count.index)]) + "Name" = format("%s%s%s", module.nat_label.id, local.delimiter, local.subnet_az_abbreviations[count.index]) } ) @@ -30,32 +25,44 @@ resource "aws_eip" "default" { } } -resource "aws_nat_gateway" "default" { - count = local.enabled ? local.nat_gateways_count : 0 - allocation_id = element(local.gateway_eip_allocations, count.index) - subnet_id = element(aws_subnet.public.*.id, count.index) +resource "aws_route" "nat4" { + count = local.nat_gateway_enabled && local.private_network_route_enabled && local.ipv4_enabled ? local.private_network_table_count : 0 - tags = merge( - module.nat_label.tags, - { - "Name" = format("%s%s%s", module.nat_label.id, local.delimiter, local.az_map[element(var.availability_zones, count.index)]) - } - ) + route_table_id = aws_route_table.private[count.index].id + nat_gateway_id = aws_nat_gateway.default[count.index].id + destination_cidr_block = "0.0.0.0/0" + depends_on = [aws_route_table.private] - lifecycle { - create_before_destroy = true + timeouts { + create = local.route_create_timeout + delete = local.route_delete_timeout } } -resource "aws_route" "default" { - count = local.enabled ? local.nat_gateways_count : 0 - route_table_id = element(aws_route_table.private.*.id, count.index) - nat_gateway_id = element(aws_nat_gateway.default.*.id, count.index) - destination_cidr_block = "0.0.0.0/0" - depends_on = [aws_route_table.private] +resource "aws_route" "private_nat64" { + count = local.nat_gateway_enabled && local.private_network_route_enabled && local.private_dns64_enabled ? local.private_network_table_count : 0 + + route_table_id = aws_route_table.private[count.index].id + nat_gateway_id = aws_nat_gateway.default[count.index].id + destination_ipv6_cidr_block = local.nat64_cidr + depends_on = [aws_route_table.private] + + timeouts { + create = local.route_create_timeout + delete = local.route_delete_timeout + } +} + +resource "aws_route" "public_nat64" { + count = local.nat_gateway_enabled && local.public_network_route_enabled && local.public_dns64_enabled ? local.public_network_table_count : 0 + + route_table_id = aws_route_table.public[count.index].id + nat_gateway_id = aws_nat_gateway.default[count.index].id + destination_ipv6_cidr_block = local.nat64_cidr + depends_on = [aws_route_table.public] timeouts { - create = var.aws_route_create_timeout - delete = var.aws_route_delete_timeout + create = local.route_create_timeout + delete = local.route_delete_timeout } } diff --git a/nat-instance.tf b/nat-instance.tf index f7d569c0..1f5e2f95 100644 --- a/nat-instance.tf +++ b/nat-instance.tf @@ -1,3 +1,8 @@ + +# AWS NAT Instances are being phased out, and do not support IPv6 traffic, +# such as NAT64, so this module does not support IPv6 traffic to NAT instances. +# NAT Gateways are recommended instead. + module "nat_instance_label" { source = "cloudposse/label/null" version = "0.25.0" @@ -7,24 +12,23 @@ module "nat_instance_label" { context = module.this.context } -locals { - cidr_block = var.cidr_block != "" ? var.cidr_block : join("", data.aws_vpc.default.*.cidr_block) - nat_instance_enabled = var.nat_instance_enabled ? 1 : 0 - nat_instance_count = var.nat_instance_enabled ? length(var.availability_zones) : 0 - nat_instance_eip_count = local.use_existing_eips ? 0 : local.nat_instance_count - instance_eip_allocations = local.use_existing_eips ? data.aws_eip.nat_ips.*.id : aws_eip.nat_instance.*.id -} - +## Note: May 2022, although Cloud Posse is generally converting its modules +## to use its `security-group` module rather than provision security group +## resources directly, that is a breaking change, and given that this +## security group is minimal, we will defer making that breaking change +## to preserve compatibility while we add IPv6 functionality. resource "aws_security_group" "nat_instance" { - count = local.enabled ? local.nat_instance_enabled : 0 + count = local.nat_instance_enabled ? 1 : 0 + name = module.nat_instance_label.id description = "Security Group for NAT Instance" - vpc_id = var.vpc_id + vpc_id = local.vpc_id tags = module.nat_instance_label.tags } resource "aws_security_group_rule" "nat_instance_egress" { - count = local.enabled ? local.nat_instance_enabled : 0 + count = local.nat_instance_enabled ? 1 : 0 + description = "Allow all egress traffic" from_port = 0 to_port = 0 @@ -35,19 +39,21 @@ resource "aws_security_group_rule" "nat_instance_egress" { } resource "aws_security_group_rule" "nat_instance_ingress" { - count = local.enabled ? local.nat_instance_enabled : 0 + count = local.nat_instance_enabled ? 1 : 0 + description = "Allow ingress traffic from the VPC CIDR block" from_port = 0 to_port = 0 protocol = "-1" - cidr_blocks = [local.cidr_block] + cidr_blocks = [local.base_ipv4_cidr_block] security_group_id = join("", aws_security_group.nat_instance.*.id) type = "ingress" } # aws --region us-west-2 ec2 describe-images --owners amazon --filters Name="name",Values="amzn-ami-vpc-nat*" Name="virtualization-type",Values="hvm" data "aws_ami" "nat_instance" { - count = local.enabled ? local.nat_instance_enabled : 0 + count = local.need_nat_ami_id ? 1 : 0 + most_recent = true filter { @@ -67,16 +73,17 @@ data "aws_ami" "nat_instance" { # https://docs.aws.amazon.com/vpc/latest/userguide/VPC_NAT_Instance.html # https://dzone.com/articles/nat-instance-vs-nat-gateway resource "aws_instance" "nat_instance" { - count = local.enabled ? local.nat_instance_count : 0 - ami = join("", data.aws_ami.nat_instance.*.id) + count = local.nat_instance_enabled ? local.subnet_az_count : 0 + + ami = local.nat_instance_ami_id instance_type = var.nat_instance_type - subnet_id = element(aws_subnet.public.*.id, count.index) + subnet_id = aws_subnet.public[count.index].id vpc_security_group_ids = [aws_security_group.nat_instance[0].id] tags = merge( module.nat_instance_label.tags, { - "Name" = format("%s%s%s", module.nat_instance_label.id, local.delimiter, local.az_map[element(var.availability_zones, count.index)]) + "Name" = format("%s%s%s", module.nat_instance_label.id, local.delimiter, local.subnet_az_abbreviations[count.index]) } ) @@ -93,13 +100,13 @@ resource "aws_instance" "nat_instance" { } metadata_options { - http_endpoint = (var.metadata_http_endpoint_enabled) ? "enabled" : "disabled" + http_endpoint = var.metadata_http_endpoint_enabled ? "enabled" : "disabled" http_put_response_hop_limit = var.metadata_http_put_response_hop_limit - http_tokens = (var.metadata_http_tokens_required) ? "required" : "optional" + http_tokens = var.metadata_http_tokens_required ? "required" : "optional" } root_block_device { - encrypted = var.root_block_device_encrypted + encrypted = local.nat_instance_root_block_device_encrypted } dynamic "credit_specification" { @@ -111,36 +118,23 @@ resource "aws_instance" "nat_instance" { } } -resource "aws_eip" "nat_instance" { - count = local.enabled ? local.nat_instance_eip_count : 0 - vpc = true - tags = merge( - module.nat_instance_label.tags, - { - "Name" = format("%s%s%s", module.nat_instance_label.id, local.delimiter, local.az_map[element(var.availability_zones, count.index)]) - } - ) - - lifecycle { - create_before_destroy = true - } -} - resource "aws_eip_association" "nat_instance" { - count = local.enabled ? local.nat_instance_count : 0 - instance_id = element(aws_instance.nat_instance.*.id, count.index) - allocation_id = element(local.instance_eip_allocations, count.index) + count = local.nat_instance_enabled ? local.subnet_az_count : 0 + + instance_id = aws_instance.nat_instance[count.index].id + allocation_id = local.nat_eip_allocations[count.index] } resource "aws_route" "nat_instance" { - count = local.enabled ? local.nat_instance_count : 0 - route_table_id = element(aws_route_table.private.*.id, count.index) - network_interface_id = element(aws_instance.nat_instance.*.primary_network_interface_id, count.index) + count = local.nat_instance_enabled && local.private_network_route_enabled ? local.private_network_table_count : 0 + + route_table_id = aws_route_table.private[count.index].id + network_interface_id = aws_instance.nat_instance[count.index].primary_network_interface_id destination_cidr_block = "0.0.0.0/0" depends_on = [aws_route_table.private] timeouts { - create = var.aws_route_create_timeout - delete = var.aws_route_delete_timeout + create = local.route_create_timeout + delete = local.route_delete_timeout } } diff --git a/outputs-deprecated.tf b/outputs-deprecated.tf new file mode 100644 index 00000000..8f332556 --- /dev/null +++ b/outputs-deprecated.tf @@ -0,0 +1,5 @@ +output "nat_gateway_public_ips" { + description = "DEPRECATED: use `nat_ips` instead. Public IPv4 IP addresses in use by NAT." + value = local.need_nat_eip_data ? var.nat_elastic_ips : aws_eip.default.*.public_ip +} + diff --git a/outputs.tf b/outputs.tf index 14eeea36..adf3f482 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,3 +1,15 @@ +output "availability_zones" { + description = "List of Availability Zones where subnets were created" + value = local.subnet_availability_zones +} + +output "availability_zone_ids" { + description = "List of Availability Zones IDs where subnets were created, when available" + value = local.use_az_ids ? var.availability_zone_ids : [ + for az in local.subnet_availability_zones : local.az_name_map[az] + ] +} + output "public_subnet_ids" { description = "IDs of the created public subnets" value = aws_subnet.public.*.id @@ -9,15 +21,25 @@ output "private_subnet_ids" { } output "public_subnet_cidrs" { - description = "CIDR blocks of the created public subnets" + description = "IPv4 CIDR blocks of the created public subnets" value = aws_subnet.public.*.cidr_block } +output "public_subnet_ipv6_cidrs" { + description = "IPv6 CIDR blocks of the created public subnets" + value = aws_subnet.public.*.ipv6_cidr_block +} + output "private_subnet_cidrs" { - description = "CIDR blocks of the created private subnets" + description = "IPv4 CIDR blocks of the created private subnets" value = aws_subnet.private.*.cidr_block } +output "private_subnet_ipv6_cidrs" { + description = "IPv6 CIDR blocks of the created private subnets" + value = aws_subnet.private.*.ipv6_cidr_block +} + output "public_route_table_ids" { description = "IDs of the created public route tables" value = aws_route_table.public.*.id @@ -28,27 +50,35 @@ output "private_route_table_ids" { value = aws_route_table.private.*.id } +output "public_network_acl" { + value = local.public_open_network_acl_enabled ? aws_network_acl.public[0].id : null +} + +output "private_network_acl" { + value = local.private_open_network_acl_enabled ? aws_network_acl.private[0].id : null +} + output "nat_gateway_ids" { description = "IDs of the NAT Gateways created" value = aws_nat_gateway.default.*.id } -output "nat_gateway_public_ips" { - description = "EIP of the NAT Gateway" - value = aws_eip.default.*.public_ip -} - output "nat_instance_ids" { description = "IDs of the NAT Instances created" value = aws_instance.nat_instance.*.id } -output "availability_zones" { - description = "List of Availability Zones where subnets were created" - value = var.availability_zones +output "nat_instance_ami_id" { + description = "ID of AMI used by NAT instance" + value = local.nat_instance_enabled ? local.nat_instance_ami_id : null } output "nat_ips" { - description = "IP Addresses in use for NAT" - value = coalescelist(aws_eip.default.*.public_ip, aws_eip.nat_instance.*.public_ip, data.aws_eip.nat_ips.*.public_ip, tolist([""])) + description = "IP Addresses in use by NAT" + value = local.need_nat_eip_data ? var.nat_elastic_ips : aws_eip.default.*.public_ip } + +output "nat_eip_allocation_ids" { + description = "Elastic IP allocations in use by NAT" + value = local.nat_eip_allocations +} \ No newline at end of file diff --git a/private.tf b/private.tf index f922e50b..7031b998 100644 --- a/private.tf +++ b/private.tf @@ -5,35 +5,37 @@ module "private_label" { attributes = ["private"] tags = merge( var.private_subnets_additional_tags, - { (var.subnet_type_tag_key) = format(var.subnet_type_tag_value_format, "private") } + var.subnet_type_tag_key != null && var.subnet_type_tag_value_format != null ? { (var.subnet_type_tag_key) = format(var.subnet_type_tag_value_format, "private") } : {} ) context = module.this.context } -locals { - private_subnet_count = var.max_subnet_count == 0 ? length(flatten(data.aws_availability_zones.available.*.names)) : var.max_subnet_count - private_network_acl_enabled = signum(length(var.private_network_acl_id)) == 0 ? 1 : 0 -} - resource "aws_subnet" "private" { - count = local.enabled ? local.availability_zones_count : 0 - vpc_id = join("", data.aws_vpc.default.*.id) - availability_zone = element(var.availability_zones, count.index) - - cidr_block = cidrsubnet( - signum(length(var.cidr_block)) == 1 ? var.cidr_block : join("", data.aws_vpc.default.*.cidr_block), - ceil(log(local.private_subnet_count * 2, 2)), - count.index - ) + count = local.private_enabled ? local.subnet_az_count : 0 + + vpc_id = local.vpc_id + availability_zone = local.subnet_availability_zones[count.index] + + cidr_block = local.ipv4_enabled ? local.ipv4_private_subnet_cidrs[count.index] : null + ipv6_cidr_block = local.ipv6_enabled ? local.ipv6_private_subnet_cidrs[count.index] : null + ipv6_native = local.ipv6_enabled && !local.ipv4_enabled tags = merge( module.private_label.tags, { - "Name" = format("%s%s%s", module.private_label.id, local.delimiter, local.az_map[element(var.availability_zones, count.index)]) + "Name" = format("%s%s%s", module.private_label.id, local.delimiter, local.subnet_az_abbreviations[count.index]) } ) + assign_ipv6_address_on_creation = local.ipv6_enabled ? var.private_assign_ipv6_address_on_creation : null + enable_dns64 = local.ipv6_enabled ? var.private_dns64_enabled : null + + enable_resource_name_dns_a_record_on_launch = local.ipv4_enabled ? var.ipv4_private_instance_hostnames_enabled : null + enable_resource_name_dns_aaaa_record_on_launch = local.ipv6_enabled ? var.ipv6_private_instance_hostnames_enabled : null + + private_dns_hostname_type_on_launch = local.ipv4_enabled ? var.ipv4_private_instance_hostname_type : null + lifecycle { # Ignore tags added by kops or kubernetes ignore_changes = [tags.kubernetes, tags.SubnetType] @@ -41,45 +43,102 @@ resource "aws_subnet" "private" { } resource "aws_route_table" "private" { - count = local.enabled ? local.availability_zones_count : 0 - vpc_id = join("", data.aws_vpc.default.*.id) + # Currently private_network_table_count == subnet_az_count, + # but keep parallel to public route table configuration + count = local.private_network_table_count + + vpc_id = local.vpc_id tags = merge( module.private_label.tags, { - "Name" = format("%s%s%s", module.private_label.id, local.delimiter, local.az_map[element(var.availability_zones, count.index)]) + "Name" = format("%s%s%s", module.private_label.id, local.delimiter, local.subnet_az_abbreviations[count.index]) } ) } +resource "aws_route" "private6" { + count = local.ipv6_egress_only_configured ? local.private_network_table_count : 0 + + route_table_id = aws_route_table.private[count.index].id + destination_ipv6_cidr_block = "::/0" + egress_only_gateway_id = var.ipv6_egress_only_igw_id[0] + + timeouts { + create = local.route_create_timeout + delete = local.route_delete_timeout + } +} + resource "aws_route_table_association" "private" { - count = local.enabled ? local.availability_zones_count : 0 - subnet_id = element(aws_subnet.private.*.id, count.index) - route_table_id = element(aws_route_table.private.*.id, count.index) + count = local.private_network_route_enabled ? local.subnet_az_count : 0 + + subnet_id = aws_subnet.private[count.index].id + # Use element() to "wrap around" and allow for a single table to be associated with all subnets + route_table_id = element(local.private_network_table_ids, count.index) } resource "aws_network_acl" "private" { - count = local.enabled ? local.private_network_acl_enabled : 0 - vpc_id = var.vpc_id + count = local.private_open_network_acl_enabled ? 1 : 0 + + vpc_id = local.vpc_id subnet_ids = aws_subnet.private.*.id - egress { - rule_no = 100 - action = "allow" - cidr_block = "0.0.0.0/0" - from_port = 0 - to_port = 0 - protocol = "-1" - } + tags = module.private_label.tags +} - ingress { - rule_no = 100 - action = "allow" - cidr_block = "0.0.0.0/0" - from_port = 0 - to_port = 0 - protocol = "-1" - } +resource "aws_network_acl_rule" "private4_ingress" { + count = local.private_open_network_acl_enabled && local.ipv4_enabled ? 1 : 0 - tags = module.private_label.tags + network_acl_id = aws_network_acl.private[0].id + rule_action = "allow" + rule_number = var.open_network_acl_ipv4_rule_number + + egress = false + cidr_block = "0.0.0.0/0" + from_port = 0 + to_port = 0 + protocol = "-1" +} + +resource "aws_network_acl_rule" "private4_egress" { + count = local.private_open_network_acl_enabled && local.ipv4_enabled ? 1 : 0 + + network_acl_id = aws_network_acl.private[0].id + rule_action = "allow" + rule_number = var.open_network_acl_ipv4_rule_number + + egress = true + cidr_block = "0.0.0.0/0" + from_port = 0 + to_port = 0 + protocol = "-1" +} + +resource "aws_network_acl_rule" "private6_ingress" { + count = local.private_open_network_acl_enabled && local.ipv6_enabled ? 1 : 0 + + network_acl_id = aws_network_acl.private[0].id + rule_action = "allow" + rule_number = var.open_network_acl_ipv6_rule_number + + egress = false + ipv6_cidr_block = "::/0" + from_port = 0 + to_port = 0 + protocol = "-1" } + +resource "aws_network_acl_rule" "private6_egress" { + count = local.private_open_network_acl_enabled && local.ipv6_enabled ? 1 : 0 + + network_acl_id = aws_network_acl.private[0].id + rule_action = "allow" + rule_number = var.open_network_acl_ipv6_rule_number + + egress = true + ipv6_cidr_block = "::/0" + from_port = 0 + to_port = 0 + protocol = "-1" +} \ No newline at end of file diff --git a/public.tf b/public.tf index 0b91841c..9dd88d9a 100644 --- a/public.tf +++ b/public.tf @@ -5,36 +5,40 @@ module "public_label" { attributes = ["public"] tags = merge( var.public_subnets_additional_tags, - { (var.subnet_type_tag_key) = format(var.subnet_type_tag_value_format, "public") } + var.subnet_type_tag_key != null && var.subnet_type_tag_value_format != null ? { (var.subnet_type_tag_key) = format(var.subnet_type_tag_value_format, "public") } : {} ) context = module.this.context } -locals { - public_subnet_count = local.enabled && var.max_subnet_count == 0 ? length(flatten(data.aws_availability_zones.available.*.names)) : var.max_subnet_count - public_route_expr_enabled = local.enabled && signum(length(var.vpc_default_route_table_id)) == 1 - public_network_acl_enabled = local.enabled && signum(length(var.public_network_acl_id)) == 0 ? 1 : 0 - vpc_default_route_table_id = local.enabled ? signum(length(var.vpc_default_route_table_id)) : 0 -} - resource "aws_subnet" "public" { - count = local.availability_zones_count - vpc_id = join("", data.aws_vpc.default.*.id) - availability_zone = element(var.availability_zones, count.index) - - cidr_block = cidrsubnet( - signum(length(var.cidr_block)) == 1 ? var.cidr_block : join("", data.aws_vpc.default.*.cidr_block), - ceil(log(local.public_subnet_count * 2, 2)), - local.public_subnet_count + count.index - ) + count = local.subnet_az_count + + vpc_id = local.vpc_id + availability_zone = local.subnet_availability_zones[count.index] + + # When provisioning both public and private subnets, the public subnets get the second set of CIDRs. + # Use element()'s wrap-around behavior to handle the case where we are only provisioning public subnets. + cidr_block = local.ipv4_enabled ? element(local.ipv4_public_subnet_cidrs, count.index) : null + ipv6_cidr_block = local.ipv6_enabled ? element(local.ipv6_public_subnet_cidrs, count.index) : null + ipv6_native = local.ipv6_enabled && !local.ipv4_enabled + + + map_public_ip_on_launch = local.ipv4_enabled ? var.map_public_ip_on_launch : null + + assign_ipv6_address_on_creation = local.ipv6_enabled ? var.public_assign_ipv6_address_on_creation : null + enable_dns64 = local.ipv6_enabled ? var.public_dns64_enabled : null + + enable_resource_name_dns_a_record_on_launch = local.ipv4_enabled ? var.ipv4_public_instance_hostnames_enabled : null + enable_resource_name_dns_aaaa_record_on_launch = local.ipv6_enabled ? var.ipv6_public_instance_hostnames_enabled : null + + private_dns_hostname_type_on_launch = local.ipv4_enabled ? var.ipv4_public_instance_hostname_type : null - map_public_ip_on_launch = var.map_public_ip_on_launch tags = merge( module.public_label.tags, { - "Name" = format("%s%s%s", module.public_label.id, local.delimiter, local.az_map[element(var.availability_zones, count.index)]) + "Name" = format("%s%s%s", module.public_label.id, local.delimiter, local.subnet_az_abbreviations[count.index]) } ) @@ -44,17 +48,20 @@ resource "aws_subnet" "public" { } resource "aws_route_table" "public" { - count = local.public_route_expr_enabled ? 0 : local.enabled_count - vpc_id = join("", data.aws_vpc.default.*.id) + # May need 1 table or 1 per AZ + count = local.public_network_table_count + + vpc_id = local.vpc_id tags = module.public_label.tags } resource "aws_route" "public" { - count = local.public_route_expr_enabled ? 0 : local.enabled_count - route_table_id = join("", aws_route_table.public.*.id) + count = local.ipv4_enabled && local.igw_configured ? local.public_network_table_count : 0 + + route_table_id = aws_route_table.public[count.index].id destination_cidr_block = "0.0.0.0/0" - gateway_id = var.igw_id + gateway_id = var.igw_id[0] timeouts { create = var.aws_route_create_timeout @@ -62,40 +69,87 @@ resource "aws_route" "public" { } } -resource "aws_route_table_association" "public" { - count = local.public_route_expr_enabled ? 0 : local.availability_zones_count - subnet_id = element(aws_subnet.public.*.id, count.index) - route_table_id = aws_route_table.public[0].id +resource "aws_route" "public6" { + count = local.ipv6_enabled && local.igw_configured ? local.public_network_table_count : 0 + + route_table_id = aws_route_table.public[count.index].id + destination_ipv6_cidr_block = "::/0" + gateway_id = var.igw_id[0] + + timeouts { + create = var.aws_route_create_timeout + delete = var.aws_route_delete_timeout + } } -resource "aws_route_table_association" "public_default" { - count = local.public_route_expr_enabled ? local.availability_zones_count : 0 - subnet_id = element(aws_subnet.public.*.id, count.index) - route_table_id = var.vpc_default_route_table_id +resource "aws_route_table_association" "public" { + count = local.public_network_table_enabled ? local.subnet_az_count : 0 + + subnet_id = aws_subnet.public[count.index].id + route_table_id = element(local.public_network_table_ids, count.index) } resource "aws_network_acl" "public" { - count = local.public_network_acl_enabled - vpc_id = var.vpc_id + count = local.public_open_network_acl_enabled ? 1 : 0 + + vpc_id = local.vpc_id subnet_ids = aws_subnet.public.*.id - egress { - rule_no = 100 - action = "allow" - cidr_block = "0.0.0.0/0" - from_port = 0 - to_port = 0 - protocol = "-1" - } + tags = module.public_label.tags +} - ingress { - rule_no = 100 - action = "allow" - cidr_block = "0.0.0.0/0" - from_port = 0 - to_port = 0 - protocol = "-1" - } +resource "aws_network_acl_rule" "public4_ingress" { + count = local.public_open_network_acl_enabled && local.ipv4_enabled ? 1 : 0 - tags = module.public_label.tags + network_acl_id = aws_network_acl.public[0].id + rule_action = "allow" + rule_number = var.open_network_acl_ipv4_rule_number + + egress = false + cidr_block = "0.0.0.0/0" + from_port = 0 + to_port = 0 + protocol = "-1" } + +resource "aws_network_acl_rule" "public4_egress" { + count = local.public_open_network_acl_enabled && local.ipv4_enabled ? 1 : 0 + + network_acl_id = aws_network_acl.public[0].id + rule_action = "allow" + rule_number = var.open_network_acl_ipv4_rule_number + + egress = true + cidr_block = "0.0.0.0/0" + from_port = 0 + to_port = 0 + protocol = "-1" +} + +resource "aws_network_acl_rule" "public6_ingress" { + count = local.public_open_network_acl_enabled && local.ipv6_enabled ? 1 : 0 + + network_acl_id = aws_network_acl.public[0].id + rule_action = "allow" + rule_number = var.open_network_acl_ipv6_rule_number + + egress = false + ipv6_cidr_block = "::/0" + from_port = 0 + to_port = 0 + protocol = "-1" +} + +resource "aws_network_acl_rule" "public6_egress" { + count = local.public_open_network_acl_enabled && local.ipv6_enabled ? 1 : 0 + + network_acl_id = aws_network_acl.public[0].id + rule_action = "allow" + rule_number = var.open_network_acl_ipv6_rule_number + + egress = true + ipv6_cidr_block = "::/0" + from_port = 0 + to_port = 0 + protocol = "-1" +} \ No newline at end of file diff --git a/test/src/go.mod b/test/src/go.mod index 2a6d2562..44cafbcd 100644 --- a/test/src/go.mod +++ b/test/src/go.mod @@ -32,7 +32,7 @@ require ( github.com/gruntwork-io/go-commons v0.8.0 // indirect github.com/hashicorp/errwrap v1.0.0 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect - github.com/hashicorp/go-getter v1.5.9 // indirect + github.com/hashicorp/go-getter v1.5.11 // indirect github.com/hashicorp/go-multierror v1.1.0 // indirect github.com/hashicorp/go-safetemp v1.0.0 // indirect github.com/hashicorp/go-version v1.3.0 // indirect diff --git a/test/src/go.sum b/test/src/go.sum index d4e2a8ff..417452b3 100644 --- a/test/src/go.sum +++ b/test/src/go.sum @@ -219,8 +219,8 @@ github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/U github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= -github.com/hashicorp/go-getter v1.5.9 h1:b7ahZW50iQiUek/at3CvZhPK1/jiV6CtKcsJiR6E4R0= -github.com/hashicorp/go-getter v1.5.9/go.mod h1:BrrV/1clo8cCYu6mxvboYg+KutTiFnXjMEgDD8+i7ZI= +github.com/hashicorp/go-getter v1.5.11 h1:wioTuNmaBU3IE9vdFtFMcmZWj0QzLc6DYaP6sNe5onY= +github.com/hashicorp/go-getter v1.5.11/go.mod h1:9i48BP6wpWweI/0/+FBjqLrp9S8XtwUGjiu0QkWHEaY= github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI= github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA= github.com/hashicorp/go-safetemp v1.0.0 h1:2HR189eFNrjHQyENnQMMpCiBAsRxzbTMIgBhEyExpmo= diff --git a/variables-deprecated.tf b/variables-deprecated.tf new file mode 100644 index 00000000..7db790f2 --- /dev/null +++ b/variables-deprecated.tf @@ -0,0 +1,72 @@ +variable "cidr_block" { + type = string + description = <<-EOT + DEPRECATED: Use `ipv4_cidr_block` instead. + Base IPv4 CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`) + EOT + default = null +} + +variable "aws_route_create_timeout" { + type = string + description = <<-EOT + DEPRECTATED: Use `route_create_timeout` instead. + Time to wait for AWS route creation, specified as a Go Duration, e.g. `2m` + EOT + default = null +} + +variable "aws_route_delete_timeout" { + type = string + description = <<-EOT + DEPRECTATED: Use `route_delete_timeout` instead. + Time to wait for AWS route deletion, specified as a Go Duration, e.g. `2m` + EOT + default = null +} + +variable "subnet_type_tag_key" { + type = string + description = <<-EOT + DEPRECATED: Use `public_subnets_additional_tags` and `private_subnets_additional_tags` instead + Key for subnet type tag to provide information about the type of subnets, e.g. `cpco.io/subnet/type: private` or `cpco.io/subnet/type: public` + EOT + default = null +} + +variable "subnet_type_tag_value_format" { + description = <<-EOT + DEPRECATED: Use `public_subnets_additional_tags` and `private_subnets_additional_tags` instead. + The value of the `subnet_type_tag_key` will be set to `format(var.subnet_type_tag_value_format, )` + where `` is either `public` or `private`. + EOT + type = string + default = "%s" +} + +variable "public_network_acl_id" { + type = string + default = "" + description = <<-EOT + DEPRECATED: Use `public_open_network_acl_enabled` instead. + If set, no Network ACL will be created or assigned to the public subnets. + EOT +} + +variable "private_network_acl_id" { + type = string + default = "" + description = <<-EOT + DEPRECATED: Use `private_open_network_acl_enabled` instead. + If set, no Network ACL will be created or assigned to the public subnets. + EOT +} + +variable "root_block_device_encrypted" { + type = bool + default = null + description = <<-EOT + DEPRECATED: use `nat_instance_root_block_device_encrypted` instead. + Whether to encrypt the root block device on the created NAT instances + EOT +} diff --git a/variables.tf b/variables.tf index 05419235..6bc7b7fd 100644 --- a/variables.tf +++ b/variables.tf @@ -1,67 +1,163 @@ -variable "subnet_type_tag_key" { +variable "vpc_id" { type = string - default = "cpco.io/subnet/type" - description = "Key for subnet type tag to provide information about the type of subnets, e.g. `cpco.io/subnet/type=private` or `cpco.io/subnet/type=public`" + description = "VPC ID where subnets will be created (e.g. `vpc-aceb2723`)" } -variable "subnet_type_tag_value_format" { - default = "%s" - description = "This is using the format interpolation symbols to allow the value of the subnet_type_tag_key to be modified." - type = string +variable "igw_id" { + type = list(string) + description = <<-EOT + The Internet Gateway ID that the public subnets will route traffic to. + Used if `public_network_route_enabled` is `true`, ignored otherwise. + EOT + default = [] + validation { + condition = length(var.igw_id) < 2 + error_message = "Only 1 igw_id can be provided." + } +} + +variable "ipv6_egress_only_igw_id" { + type = list(string) + description = <<-EOT + The Egress Only Internet Gateway ID the private IPv6 subnets will route traffic to. + Used if `private_network_route_enabled` is `true` and `ipv6_enabled` is `true`, ignored otherwise. + EOT + default = [] + validation { + condition = length(var.ipv6_egress_only_igw_id) < 2 + error_message = "Only 1 ipv6_egress_only_igw_id can be provided." + } } variable "max_subnet_count" { + type = number + description = <<-EOT + Sets the maximum number of each type (public or private) of subnet to deploy. + 0 will reserve a CIDR for every Availability Zone (excluding Local Zones) in the region, and + deploy a subnet in each availability zone specified in `availability_zones` or `availability_zone_ids`, + or every zone if none are specified. Recommended to set equal to the maximum number of AZs you anticipate using, + to avoid causing subnets to be destroyed and recreated with smaller IPv4 CIDRs when AWS adds an availability zone. + EOT default = 0 - description = "Sets the maximum amount of subnets to deploy. 0 will deploy a subnet for every provided availablility zone (in `availability_zones` variable) within the region" } -variable "vpc_id" { - type = string - description = "VPC ID where subnets will be created (e.g. `vpc-aceb2723`)" +variable "private_subnets_enabled" { + type = bool + description = "If false, do not create private subnets (or NAT gateways or instances)" + default = true } -variable "igw_id" { - type = string - description = "Internet Gateway ID the public route table will point to (e.g. `igw-9c26a123`)" +variable "public_subnets_enabled" { + type = bool + description = <<-EOT + If false, do not create public subnets. + Since NAT gateways and instances must be created in public subnets, these will also not be created when `false`. + Since the only default route for a private subnet is to a NAT gateway or instance, + route tables and routes will also not be created when `false`. + EOT + default = true } -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 "ipv4_enabled" { + type = bool + description = "Set true to enable IPv4 addresses in the subnets" + default = true } -variable "availability_zones" { +variable "ipv6_enabled" { + type = bool + description = "Set true to enable IPv6 addresses in the subnets" + default = false +} + +variable "ipv4_cidr_block" { type = list(string) - description = "List of Availability Zones where subnets will be created" + description = <<-EOT + Base IPv4 CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`). Ignored if `ipv4_cidrs` is set. + If no CIDR block is provided, the VPC's default IPv4 CIDR block will be used. + EOT + default = [] + validation { + condition = length(var.ipv4_cidr_block) < 2 + error_message = "Only 1 ipv4_cidr_block can be provided. Use ipv4_cidrs to provide a CIDR per subnet." + } +} +locals { ipv4_cidr_block = var.cidr_block == null ? var.ipv4_cidr_block : [var.cidr_block] } +variable "ipv6_cidr_block" { + type = list(string) + description = <<-EOT + Base IPv6 CIDR block from which `/64` subnet CIDRs will be assigned. Must be `/56`. (e.g. `2600:1f16:c52:ab00::/56`). + Ignored if `ipv6_cidrs` is set. If no CIDR block is provided, the VPC's default IPv6 CIDR block will be used. + EOT + default = [] validation { - condition = length(var.availability_zones) > 0 - error_message = "Availability zones must be greater than zero." + condition = length(var.ipv6_cidr_block) < 2 + error_message = "Only 1 ipv6_cidr_block can be provided. Use ipv6_cidrs to provide a CIDR per subnet." } } -variable "availability_zone_attribute_style" { - type = string - default = "short" - description = "The style of Availability Zone code to use in tags and names. One of `full`, `short`, or `fixed`." +variable "ipv4_cidrs" { + type = list(object({ + private = list(string) + public = list(string) + })) + description = <<-EOT + Lists of CIDRs to assign to subnets. Order of CIDRs in the lists must not change over time. + Lists may contain more CIDRs than needed. + EOT + default = [] + validation { + condition = length(var.ipv4_cidrs) < 2 + error_message = "Only 1 ipv4_cidrs object can be provided. Lists of CIDRs are passed via the `public` and `private` attributes of the single object." + } } -variable "vpc_default_route_table_id" { - type = string - default = "" - description = "Default route table for public subnets. If not set, will be created. (e.g. `rtb-f4f0ce12`)" +variable "ipv6_cidrs" { + type = list(object({ + private = list(string) + public = list(string) + })) + description = <<-EOT + Lists of CIDRs to assign to subnets. Order of CIDRs in the lists must not change over time. + Lists may contain more CIDRs than needed. + EOT + default = [] + validation { + condition = length(var.ipv6_cidrs) < 2 + error_message = "Only 1 ipv6_cidrs object can be provided. Lists of CIDRs are passed via the `public` and `private` attributes of the single object." + } + } -variable "public_network_acl_id" { - type = string - default = "" - description = "Network ACL ID that will be added to public subnets. If empty, a new ACL will be created" +variable "availability_zones" { + type = list(string) + description = <<-EOT + List of Availability Zones (AZs) where subnets will be created. Conflicts with `availability_zone_ids`. + The order of zones in the list ***must be stable*** or else Terraform will continually make changes. + If no AZs are specified, then `max_subnet_count` AZs will be selected in alphabetical order. + If `max_subnet_count > 0` and `length(var.availability_zones) > max_subnet_count`, the list + will be truncated. + EOT + default = [] } -variable "private_network_acl_id" { +variable "availability_zone_ids" { + type = list(string) + description = <<-EOT + List of Availability Zones IDs where subnets will be created. Conflicts with `availability_zones`. + Useful in some regions when using only some AZs and you want to use the same ones across multiple accounts. + EOT + default = [] +} + +variable "availability_zone_attribute_style" { type = string - description = "Network ACL ID that will be added to private subnets. If empty, a new ACL will be created" - default = "" + default = "short" + description = <<-EOT + The style of Availability Zone code to use in tags and names. One of `full`, `short`, or `fixed`. + When using `availability_zone_ids`, IDs will first be translated into AZ names. + EOT } variable "nat_gateway_enabled" { @@ -76,45 +172,183 @@ variable "nat_instance_enabled" { default = false } -variable "nat_instance_type" { +variable "nat_elastic_ips" { + type = list(string) + description = "Existing Elastic IPs (not EIP IDs) to attach to the NAT Gateway(s) or Instance(s) instead of creating new ones." + default = [] +} + +variable "map_public_ip_on_launch" { + type = bool + description = "If `true`, instances launched into a public subnet will be assigned a public IPv4 address" + default = true +} + +variable "private_assign_ipv6_address_on_creation" { + type = bool + description = "If `true`, network interfaces created in a private subnet will be assigned an IPv6 address" + default = true +} + +variable "public_assign_ipv6_address_on_creation" { + type = bool + description = "If `true`, network interfaces created in a public subnet will be assigned an IPv6 address" + default = true +} + +variable "private_dns64_enabled" { + type = bool + description = <<-EOT + If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic IPv6 addresses for IPv4-only destinations. + Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_network_route_enabled` to be `true` to be fully operational. + EOT + default = true +} + +variable "public_dns64_enabled" { + type = bool + description = <<-EOT + If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in public subnets will return synthetic IPv6 addresses for IPv4-only destinations. + Requires `nat_gateway_enabled` and `public_network_route_enabled` to be `true` to be fully operational. + EOT + default = false +} + +variable "ipv4_private_instance_hostname_type" { type = string - description = "NAT Instance type" - default = "t3.micro" + default = "ip-name" + description = <<-EOT + How to generate the DNS name for the instances in the private subnets. + Either `ip-name` to generate it from the IPv4 address, or + `resource-name` to generate it from the instance ID. + EOT } -variable "nat_instance_cpu_credits_override" { +variable "ipv4_private_instance_hostnames_enabled" { + type = bool + default = false + description = "If `true`, DNS queries for instance hostnames in the private subnets will be answered with A (IPv4) records." +} + +variable "ipv6_private_instance_hostnames_enabled" { + type = bool + default = false + description = "If `true`, DNS queries for instance hostnames in the private subnets will be answered with AAAA (IPv6) records." +} + +variable "ipv4_public_instance_hostname_type" { type = string - description = "NAT Instance credit option for CPU usage. Valid values include standard or unlimited. T3 instances are launched as unlimited by default. T2 instances are launched as standard by default." - default = "" + default = "ip-name" + description = <<-EOT + How to generate the DNS name for the instances in the public subnets. + Either `ip-name` to generate it from the IPv4 address, or + `resource-name` to generate it from the instance ID. + EOT +} - validation { - condition = contains(["standard", "unlimited", ""], var.nat_instance_cpu_credits_override) - error_message = "The nat_instance_cpu_credits_override value must be either standard, unlimited, or empty string. Empty string will use default value of the instance type: T3 and T4 are unlimited where T2 is standard." - } +variable "ipv4_public_instance_hostnames_enabled" { + type = bool + default = false + description = "If `true`, DNS queries for instance hostnames in the public subnets will be answered with A (IPv4) records." } -variable "nat_elastic_ips" { +variable "ipv6_public_instance_hostnames_enabled" { + type = bool + default = false + description = "If `true`, DNS queries for instance hostnames in the public subnets will be answered with AAAA (IPv6) records." +} + +variable "private_open_network_acl_enabled" { + type = bool + description = <<-EOT + If `true`, a single network ACL be created and it will be associated with every private subnet, and a rule (number 100) + will be created allowing all ingress and all egress. You can add additional rules to this network ACL + using the `aws_network_acl_rule` resource. + If `false`, you will will need to manage the network ACL external to this module. + EOT + default = true +} + +variable "public_open_network_acl_enabled" { + type = bool + description = <<-EOT + If `true`, a single network ACL be created and it will be associated with every public subnet, and a rule + will be created allowing all ingress and all egress. You can add additional rules to this network ACL + using the `aws_network_acl_rule` resource. + If `false`, you will will need to manage the network ACL external to this module. + EOT + default = true +} + +variable "open_network_acl_ipv4_rule_number" { + type = number + description = "The `rule_no` assigned to the network ACL rules for IPv4 traffic generated by this module" + default = 100 +} + +variable "open_network_acl_ipv6_rule_number" { + type = number + description = "The `rule_no` assigned to the network ACL rules for IPv6 traffic generated by this module" + default = 111 +} + +variable "private_network_route_enabled" { + type = bool + description = <<-EOT + If true, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway + will be created for each private subnet (1:1). If false, you will need to create your own route table(s) and route(s). + EOT + default = true +} + +# TODO Number of public route tables depends on public_dns64_enabled +variable "public_route_table_id" { type = list(string) + description = <<-EOT + List optionally containing the ID of a single route table shared by all public subnets. + If provided, and `public_network_route_enabled` is `true`, + a route will be added to it, directing traffic to the VPC's Internet Gateway. + Ignored (treated as omitted) if `public_dns64_enabled` is `true`. + If omitted (or ignored), and `public_network_route_enabled` is `true`, + a network route table will be created for each public subnet (1:1) and routes added to it. + EOT default = [] - description = "Existing Elastic IPs to attach to the NAT Gateway(s) or Instance(s) instead of creating new ones." } -variable "map_public_ip_on_launch" { +variable "public_network_route_enabled" { type = bool + description = <<-EOT + If `true`, a default route to the internet gateway will be added route table(s) associated with the public subnets. + If `public_dns64_enabled` and `nat_gateway_enabled` are also `true`, a route for NAT64 addresses to the NAT gateway will also be created. + If `false`, you will need to create your own route(s). + EOT default = true - description = "Instances launched into a public subnet should be assigned a public IP address" } -variable "aws_route_create_timeout" { +variable "route_create_timeout" { type = string - default = "2m" - description = "Time to wait for AWS route creation specifed as a Go Duration, e.g. `2m`" + default = "5m" + description = "Time to wait for a network routing table entry to be created, specified as a Go Duration, e.g. `2m`" } +locals { route_create_timeout = var.aws_route_create_timeout == null ? var.route_create_timeout : var.aws_route_create_timeout } -variable "aws_route_delete_timeout" { +variable "route_delete_timeout" { type = string - default = "5m" - description = "Time to wait for AWS route deletion specifed as a Go Duration, e.g. `5m`" + default = "10m" + description = "Time to wait for a network routing table entry to be deleted, specified as a Go Duration, e.g. `2m`" +} +locals { route_delete_timeout = var.aws_route_delete_timeout == null ? var.route_delete_timeout : var.aws_route_delete_timeout } + +variable "subnet_create_timeout" { + type = string + default = "10m" + description = "Time to wait for a subnet to be created, specified as a Go Duration, e.g. `2m`" +} + +variable "subnet_delete_timeout" { + type = string + default = "20m" + description = "Time to wait for a subnet to be deleted, specified as a Go Duration, e.g. `5m`" } variable "private_subnets_additional_tags" { @@ -129,26 +363,66 @@ variable "public_subnets_additional_tags" { description = "Additional tags to be added to public subnets" } +############## NAT instance configuration ################### +variable "nat_instance_type" { + type = string + description = "NAT Instance type" + default = "t3.micro" +} + +variable "nat_instance_ami_id" { + type = list(string) + description = <<-EOT + A list optionally containing the ID of the AMI to use for the NAT instance. + If the list is empty (the default), the latest official AWS NAT instance AMI + will be used. NOTE: The Official NAT instance AMI is being phased out and + does not support NAT64. Use of a NAT gateway is recommended instead. + EOT + default = [] + validation { + condition = length(var.nat_instance_ami_id) < 2 + error_message = "Only 1 NAT Instance AMI ID can be provided." + } + +} + +variable "nat_instance_cpu_credits_override" { + type = string + description = <<-EOT + NAT Instance credit option for CPU usage. Valid values are "standard" or "unlimited". + T3 and later instances are launched as unlimited by default. T2 instances are launched as standard by default. + EOT + default = "" + + validation { + condition = contains(["standard", "unlimited", ""], var.nat_instance_cpu_credits_override) + # Validation error messages must be on a single line, among other restrictions. + # See https://github.com/hashicorp/terraform/issues/24123 + error_message = "The `nat_instance_cpu_credits_override` value must be either \"standard\", \"unlimited\", or empty string." + } +} + variable "metadata_http_endpoint_enabled" { type = bool default = true - description = "Whether the metadata service is available" + description = "Whether the metadata service is available on the created NAT instances" } variable "metadata_http_put_response_hop_limit" { type = number default = 1 - description = "The desired HTTP PUT response hop limit (between 1 and 64) for instance metadata requests." + description = "The desired HTTP PUT response hop limit (between 1 and 64) for instance metadata requests on the created NAT instances" } variable "metadata_http_tokens_required" { type = bool default = true - description = "Whether or not the metadata service requires session tokens, also referred to as Instance Metadata Service Version 2." + description = "Whether or not the metadata service requires session tokens, also referred to as Instance Metadata Service Version 2, on the created NAT instances" } -variable "root_block_device_encrypted" { +variable "nat_instance_root_block_device_encrypted" { type = bool default = true - description = "Whether to encrypt the root block device" + description = "Whether to encrypt the root block device on the created NAT instances" } +locals { nat_instance_root_block_device_encrypted = var.root_block_device_encrypted == null ? var.nat_instance_root_block_device_encrypted : var.root_block_device_encrypted } diff --git a/versions.tf b/versions.tf index 382effb1..9e386405 100644 --- a/versions.tf +++ b/versions.tf @@ -1,10 +1,10 @@ terraform { - required_version = ">= 0.13.0" + required_version = ">= 1.1.0" required_providers { aws = { source = "hashicorp/aws" - version = ">= 3.62.0" + version = ">= 3.71.0" } } } From 370c550549fb9057023f0738510e124a0765902d Mon Sep 17 00:00:00 2001 From: cloudpossebot <11232728+cloudpossebot@users.noreply.github.com> Date: Sat, 7 May 2022 01:58:43 +0000 Subject: [PATCH 03/12] Minor cleanups --- README.md | 6 +++--- docs/terraform.md | 6 +++--- examples/existing-ips/main.tf | 6 +++--- main.tf | 1 + nat-instance.tf | 3 +++ outputs.tf | 6 ++++-- public.tf | 2 +- 7 files changed, 18 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 507846ab..41f2d9d2 100644 --- a/README.md +++ b/README.md @@ -286,7 +286,7 @@ Available targets: | Name | Version | |------|---------| -| [aws](#provider\_aws) | 4.13.0 | +| [aws](#provider\_aws) | >= 3.71.0 | ## Modules @@ -430,12 +430,12 @@ Available targets: | [nat\_instance\_ami\_id](#output\_nat\_instance\_ami\_id) | ID of AMI used by NAT instance | | [nat\_instance\_ids](#output\_nat\_instance\_ids) | IDs of the NAT Instances created | | [nat\_ips](#output\_nat\_ips) | IP Addresses in use by NAT | -| [private\_network\_acl](#output\_private\_network\_acl) | n/a | +| [private\_network\_acl](#output\_private\_network\_acl) | ID of the Network ACL created for private subnets | | [private\_route\_table\_ids](#output\_private\_route\_table\_ids) | IDs of the created private route tables | | [private\_subnet\_cidrs](#output\_private\_subnet\_cidrs) | IPv4 CIDR blocks of the created private subnets | | [private\_subnet\_ids](#output\_private\_subnet\_ids) | IDs of the created private subnets | | [private\_subnet\_ipv6\_cidrs](#output\_private\_subnet\_ipv6\_cidrs) | IPv6 CIDR blocks of the created private subnets | -| [public\_network\_acl](#output\_public\_network\_acl) | n/a | +| [public\_network\_acl](#output\_public\_network\_acl) | ID of the Network ACL created for public subnets | | [public\_route\_table\_ids](#output\_public\_route\_table\_ids) | IDs of the created public route tables | | [public\_subnet\_cidrs](#output\_public\_subnet\_cidrs) | IPv4 CIDR blocks of the created public subnets | | [public\_subnet\_ids](#output\_public\_subnet\_ids) | IDs of the created public subnets | diff --git a/docs/terraform.md b/docs/terraform.md index aee92426..cc3eee97 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -10,7 +10,7 @@ | Name | Version | |------|---------| -| [aws](#provider\_aws) | 4.13.0 | +| [aws](#provider\_aws) | >= 3.71.0 | ## Modules @@ -154,12 +154,12 @@ | [nat\_instance\_ami\_id](#output\_nat\_instance\_ami\_id) | ID of AMI used by NAT instance | | [nat\_instance\_ids](#output\_nat\_instance\_ids) | IDs of the NAT Instances created | | [nat\_ips](#output\_nat\_ips) | IP Addresses in use by NAT | -| [private\_network\_acl](#output\_private\_network\_acl) | n/a | +| [private\_network\_acl](#output\_private\_network\_acl) | ID of the Network ACL created for private subnets | | [private\_route\_table\_ids](#output\_private\_route\_table\_ids) | IDs of the created private route tables | | [private\_subnet\_cidrs](#output\_private\_subnet\_cidrs) | IPv4 CIDR blocks of the created private subnets | | [private\_subnet\_ids](#output\_private\_subnet\_ids) | IDs of the created private subnets | | [private\_subnet\_ipv6\_cidrs](#output\_private\_subnet\_ipv6\_cidrs) | IPv6 CIDR blocks of the created private subnets | -| [public\_network\_acl](#output\_public\_network\_acl) | n/a | +| [public\_network\_acl](#output\_public\_network\_acl) | ID of the Network ACL created for public subnets | | [public\_route\_table\_ids](#output\_public\_route\_table\_ids) | IDs of the created public route tables | | [public\_subnet\_cidrs](#output\_public\_subnet\_cidrs) | IPv4 CIDR blocks of the created public subnets | | [public\_subnet\_ids](#output\_public\_subnet\_ids) | IDs of the created public subnets | diff --git a/examples/existing-ips/main.tf b/examples/existing-ips/main.tf index 152c1de0..c6f7b989 100644 --- a/examples/existing-ips/main.tf +++ b/examples/existing-ips/main.tf @@ -25,9 +25,9 @@ module "subnets" { availability_zones = var.availability_zones vpc_id = module.vpc.vpc_id - igw_id = module.vpc.igw_id - cidr_block = module.vpc.vpc_cidr_block - nat_elastic_ips = [for az, eip in aws_eip.nat_ips : eip.public_ip] + igw_id = [module.vpc.igw_id] + cidr_block = [module.vpc.vpc_cidr_block] + nat_elastic_ips = aws_eip.nat_ips.*.public_ip nat_gateway_enabled = true nat_instance_enabled = false diff --git a/main.tf b/main.tf index 507c99cb..5e29c666 100644 --- a/main.tf +++ b/main.tf @@ -201,6 +201,7 @@ resource "aws_eip" "default" { lifecycle { create_before_destroy = true } + #bridgecrew:skip=BC_AWS_NETWORKING_48: Skipping requirement for EIPs to be attached to EC2 instances because we are attaching to NAT Gateway. } module "utils" { diff --git a/nat-instance.tf b/nat-instance.tf index 1f5e2f95..4449724d 100644 --- a/nat-instance.tf +++ b/nat-instance.tf @@ -93,6 +93,7 @@ resource "aws_instance" "nat_instance" { #bridgecrew:skip=BC_AWS_PUBLIC_12: Skipping `EC2 Should Not Have Public IPs` check. NAT instance requires public IP. #bridgecrew:skip=BC_AWS_GENERAL_31: Skipping `Ensure Instance Metadata Service Version 1 is not enabled` check until BridgeCrew support condition evaluation. See https://github.com/bridgecrewio/checkov/issues/793 + #bridgecrew:skip=BC_AWS_LOGGING_26: Skipping requirement for detailed monitoring of NAT instance. associate_public_ip_address = true #tfsec:ignore:AWS012 lifecycle { @@ -116,6 +117,8 @@ resource "aws_instance" "nat_instance" { cpu_credits = var.nat_instance_cpu_credits_override } } + + ebs_optimized = true } resource "aws_eip_association" "nat_instance" { diff --git a/outputs.tf b/outputs.tf index adf3f482..9a43cd51 100644 --- a/outputs.tf +++ b/outputs.tf @@ -51,11 +51,13 @@ output "private_route_table_ids" { } output "public_network_acl" { - value = local.public_open_network_acl_enabled ? aws_network_acl.public[0].id : null + description = "ID of the Network ACL created for public subnets" + value = local.public_open_network_acl_enabled ? aws_network_acl.public[0].id : null } output "private_network_acl" { - value = local.private_open_network_acl_enabled ? aws_network_acl.private[0].id : null + description = "ID of the Network ACL created for private subnets" + value = local.private_open_network_acl_enabled ? aws_network_acl.private[0].id : null } output "nat_gateway_ids" { diff --git a/public.tf b/public.tf index 9dd88d9a..0990eee9 100644 --- a/public.tf +++ b/public.tf @@ -23,7 +23,7 @@ resource "aws_subnet" "public" { ipv6_cidr_block = local.ipv6_enabled ? element(local.ipv6_public_subnet_cidrs, count.index) : null ipv6_native = local.ipv6_enabled && !local.ipv4_enabled - + #bridgecrew:skip=BC_AWS_NETWORKING_53:Public VPCs should be allowed to default to public IPs map_public_ip_on_launch = local.ipv4_enabled ? var.map_public_ip_on_launch : null assign_ipv6_address_on_creation = local.ipv6_enabled ? var.public_assign_ipv6_address_on_creation : null From 6d96a8fac1cff6616ff53b1bc61d36b3ab4f0278 Mon Sep 17 00:00:00 2001 From: Nuru Date: Fri, 6 May 2022 19:41:56 -0700 Subject: [PATCH 04/12] fix test input --- examples/existing-ips/main.tf | 6 +++--- examples/existing-ips/outputs.tf | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/existing-ips/main.tf b/examples/existing-ips/main.tf index c6f7b989..d3735b2d 100644 --- a/examples/existing-ips/main.tf +++ b/examples/existing-ips/main.tf @@ -12,8 +12,8 @@ module "vpc" { } resource "aws_eip" "nat_ips" { - for_each = toset(var.availability_zones) - vpc = true + count = length(var.availability_zones) + vpc = true depends_on = [ module.vpc @@ -26,7 +26,7 @@ module "subnets" { availability_zones = var.availability_zones vpc_id = module.vpc.vpc_id igw_id = [module.vpc.igw_id] - cidr_block = [module.vpc.vpc_cidr_block] + ipv4_cidr_block = [module.vpc.vpc_cidr_block] nat_elastic_ips = aws_eip.nat_ips.*.public_ip nat_gateway_enabled = true nat_instance_enabled = false diff --git a/examples/existing-ips/outputs.tf b/examples/existing-ips/outputs.tf index 32469d37..d929e554 100644 --- a/examples/existing-ips/outputs.tf +++ b/examples/existing-ips/outputs.tf @@ -1,5 +1,5 @@ output "existing_ips" { - value = values(aws_eip.nat_ips).*.public_ip + value = aws_eip.nat_ips.*.public_ip } output "nat_ips" { From 2f04b18c2feb8554d99e61a5a0cb7a3c3ec98e3a Mon Sep 17 00:00:00 2001 From: Nuru Date: Sat, 7 May 2022 18:06:00 -0700 Subject: [PATCH 05/12] enable/disable route tables rather than routes --- README.md | 123 +++++++++++++++----------------------- README.yaml | 9 ++- docs/design.md | 93 ++++++++++------------------ docs/terraform.md | 21 +++---- examples/complete/main.tf | 2 - main.tf | 39 +++++++----- nat-gateway.tf | 12 ++-- nat-instance.tf | 4 +- private.tf | 14 ++--- public.tf | 16 ++--- variables.tf | 66 ++++++++++++-------- 11 files changed, 185 insertions(+), 214 deletions(-) diff --git a/README.md b/README.md index 41f2d9d2..abc82968 100644 --- a/README.md +++ b/README.md @@ -31,9 +31,14 @@ Terraform module to provision public and private [`subnets`](http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Subnets.html) in an existing [`VPC`](https://aws.amazon.com/vpc) -__Note:__ this module is intended for use with an existing VPC and existing Internet Gateway. +__Note:__ This module is intended for use with an existing VPC and existing Internet Gateway. To create a new VPC, use [terraform-aws-vpc](https://github.com/cloudposse/terraform-aws-vpc) module. +_Note:__ Due to Terraform [limitations](https://github.com/hashicorp/terraform/issues/26755#issuecomment-719103775), +many optional inputs to this module are specified as a `list(string)` that can have zero or one elements, rather than +as `string` that could be empty or `null`. The designation of an input as a `list` type does not necessarily +mean that you can supply more than one value in the list, so check the input's description before supplying more than one value. + The core feature of this module is dividing up a given CIDR range so that a set of subnets each gets its own distinct CIDR range within that range, and then creating those subnets in the appropriate availability zones. The intention is to keep this module relatively simple and easy to use for the most popular use cases. @@ -70,7 +75,7 @@ all egress, and associates that ACL with all the private subnets. ### Customization for special use cases -Various features are controlled by `bool` arguments with names ending in `_enabled`. By changing the default +Various features are controlled by `bool` inputs with names ending in `_enabled`. By changing the default values, you can enable or disable creation of public subnets, private subnets, route tables, NAT gateways, NAT instances, or Network ACLs. So for example, you could use this module to create only private subnets and the open Network ACL, and then add your own route table associations to the subnets @@ -192,76 +197,43 @@ Learn about [using providers](https://www.terraform.io/docs/configuration-0-11/m ## 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 = ciel(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 ( 24 = 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` (log212) - - For `16 subnets` we need `4` `binary digits` (log216) - - For `7 subnets` we need `2.81` `binary digits` (log27) - - 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(log212)) - - For `16 subnets` we need `4` `binary digits` (ceil(log216)) - - For `7 subnets` we need `3` `binary digits` (ceil(log27)) - - 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 ## Makefile Targets ```text @@ -345,8 +317,8 @@ Available targets: | [additional\_tag\_map](#input\_additional\_tag\_map) | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.
This is for some rare cases where resources want additional configuration of tags
and therefore take a list of maps with tag key, value, and additional configuration. | `map(string)` | `{}` | no | | [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the `delimiter`
and treated as a single ID element. | `list(string)` | `[]` | no | | [availability\_zone\_attribute\_style](#input\_availability\_zone\_attribute\_style) | The style of Availability Zone code to use in tags and names. One of `full`, `short`, or `fixed`.
When using `availability_zone_ids`, IDs will first be translated into AZ names. | `string` | `"short"` | no | -| [availability\_zone\_ids](#input\_availability\_zone\_ids) | List of Availability Zones IDs where subnets will be created. Conflicts with `availability_zones`.
Useful in some regions when using only some AZs and you want to use the same ones across multiple accounts. | `list(string)` | `[]` | no | -| [availability\_zones](#input\_availability\_zones) | List of Availability Zones (AZs) where subnets will be created. Conflicts with `availability_zone_ids`.
The order of zones in the list ***must be stable*** or else Terraform will continually make changes.
If no AZs are specified, then `max_subnet_count` AZs will be selected in alphabetical order.
If `max_subnet_count > 0` and `length(var.availability_zones) > max_subnet_count`, the list
will be truncated. | `list(string)` | `[]` | no | +| [availability\_zone\_ids](#input\_availability\_zone\_ids) | List of Availability Zones IDs where subnets will be created. Overrides `availability_zones`.
Useful in some regions when using only some AZs and you want to use the same ones across multiple accounts. | `list(string)` | `[]` | no | +| [availability\_zones](#input\_availability\_zones) | List of Availability Zones (AZs) where subnets will be created. Ignored when `availability_zone_ids` is set.
The order of zones in the list ***must be stable*** or else Terraform will continually make changes.
If no AZs are specified, then `max_subnet_count` AZs will be selected in alphabetical order.
If `max_subnet_count > 0` and `length(var.availability_zones) > max_subnet_count`, the list
will be truncated. We recommend setting `availability_zones` and `max_subnet_count` explicitly as constant
(not computed) values for predictability, consistency, and stability. | `list(string)` | `[]` | no | | [aws\_route\_create\_timeout](#input\_aws\_route\_create\_timeout) | DEPRECTATED: Use `route_create_timeout` instead.
Time to wait for AWS route creation, specified as a Go Duration, e.g. `2m` | `string` | `null` | no | | [aws\_route\_delete\_timeout](#input\_aws\_route\_delete\_timeout) | DEPRECTATED: Use `route_delete_timeout` instead.
Time to wait for AWS route deletion, specified as a Go Duration, e.g. `2m` | `string` | `null` | no | | [cidr\_block](#input\_cidr\_block) | DEPRECATED: Use `ipv4_cidr_block` instead.
Base IPv4 CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`) | `string` | `null` | no | @@ -356,7 +328,7 @@ Available targets: | [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no | | [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no | | [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).
Set to `0` for unlimited length.
Set to `null` for keep the existing setting, which defaults to `0`.
Does not affect `id_full`. | `number` | `null` | no | -| [igw\_id](#input\_igw\_id) | The Internet Gateway ID that the public subnets will route traffic to.
Used if `public_network_route_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | +| [igw\_id](#input\_igw\_id) | The Internet Gateway ID that the public subnets will route traffic to.
Used if `public_route_table_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | | [ipv4\_cidr\_block](#input\_ipv4\_cidr\_block) | Base IPv4 CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`). Ignored if `ipv4_cidrs` is set.
If no CIDR block is provided, the VPC's default IPv4 CIDR block will be used. | `list(string)` | `[]` | no | | [ipv4\_cidrs](#input\_ipv4\_cidrs) | Lists of CIDRs to assign to subnets. Order of CIDRs in the lists must not change over time.
Lists may contain more CIDRs than needed. |
list(object({
private = list(string)
public = list(string)
}))
| `[]` | no | | [ipv4\_enabled](#input\_ipv4\_enabled) | Set true to enable IPv4 addresses in the subnets | `bool` | `true` | no | @@ -366,7 +338,7 @@ Available targets: | [ipv4\_public\_instance\_hostnames\_enabled](#input\_ipv4\_public\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the public subnets will be answered with A (IPv4) records. | `bool` | `false` | no | | [ipv6\_cidr\_block](#input\_ipv6\_cidr\_block) | Base IPv6 CIDR block from which `/64` subnet CIDRs will be assigned. Must be `/56`. (e.g. `2600:1f16:c52:ab00::/56`).
Ignored if `ipv6_cidrs` is set. If no CIDR block is provided, the VPC's default IPv6 CIDR block will be used. | `list(string)` | `[]` | no | | [ipv6\_cidrs](#input\_ipv6\_cidrs) | Lists of CIDRs to assign to subnets. Order of CIDRs in the lists must not change over time.
Lists may contain more CIDRs than needed. |
list(object({
private = list(string)
public = list(string)
}))
| `[]` | no | -| [ipv6\_egress\_only\_igw\_id](#input\_ipv6\_egress\_only\_igw\_id) | The Egress Only Internet Gateway ID the private IPv6 subnets will route traffic to.
Used if `private_network_route_enabled` is `true` and `ipv6_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | +| [ipv6\_egress\_only\_igw\_id](#input\_ipv6\_egress\_only\_igw\_id) | The Egress Only Internet Gateway ID the private IPv6 subnets will route traffic to.
Used if `private_route_table_enabled` is `true` and `ipv6_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | | [ipv6\_enabled](#input\_ipv6\_enabled) | Set true to enable IPv6 addresses in the subnets | `bool` | `false` | no | | [ipv6\_private\_instance\_hostnames\_enabled](#input\_ipv6\_private\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the private subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | | [ipv6\_public\_instance\_hostnames\_enabled](#input\_ipv6\_public\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the public subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | @@ -375,7 +347,7 @@ Available targets: | [label\_value\_case](#input\_label\_value\_case) | Controls the letter case of ID elements (labels) as included in `id`,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper` and `none` (no transformation).
Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.
Default value: `lower`. | `string` | `null` | no | | [labels\_as\_tags](#input\_labels\_as\_tags) | Set of labels (ID elements) to include as tags in the `tags` output.
Default is to include all labels.
Tags with empty values will not be included in the `tags` output.
Set to `[]` to suppress all generated tags.
**Notes:**
The value of the `name` tag, if included, will be the `id`, not the `name`.
Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be
changed in later chained modules. Attempts to change it will be silently ignored. | `set(string)` |
[
"default"
]
| no | | [map\_public\_ip\_on\_launch](#input\_map\_public\_ip\_on\_launch) | If `true`, instances launched into a public subnet will be assigned a public IPv4 address | `bool` | `true` | no | -| [max\_subnet\_count](#input\_max\_subnet\_count) | Sets the maximum number of each type (public or private) of subnet to deploy.
0 will reserve a CIDR for every Availability Zone (excluding Local Zones) in the region, and
deploy a subnet in each availability zone specified in `availability_zones` or `availability_zone_ids`,
or every zone if none are specified. Recommended to set equal to the maximum number of AZs you anticipate using,
to avoid causing subnets to be destroyed and recreated with smaller IPv4 CIDRs when AWS adds an availability zone. | `number` | `0` | no | +| [max\_subnet\_count](#input\_max\_subnet\_count) | Sets the maximum number of each type (public or private) of subnet to deploy.
0 will reserve a CIDR for every Availability Zone (excluding Local Zones) in the region, and
deploy a subnet in each availability zone specified in `availability_zones` or `availability_zone_ids`,
or every zone if none are specified. We recommend setting this equal to the maximum number of AZs you anticipate using,
to avoid causing subnets to be destroyed and recreated with smaller IPv4 CIDRs when AWS adds an availability zone.
Due to Terraform limitations, you can not set `max_subnet_count` from a computed value, you have to set it
from an explicit constant. For most cases, `3` is a good choice. | `number` | `0` | no | | [metadata\_http\_endpoint\_enabled](#input\_metadata\_http\_endpoint\_enabled) | Whether the metadata service is available on the created NAT instances | `bool` | `true` | no | | [metadata\_http\_put\_response\_hop\_limit](#input\_metadata\_http\_put\_response\_hop\_limit) | The desired HTTP PUT response hop limit (between 1 and 64) for instance metadata requests on the created NAT instances | `number` | `1` | no | | [metadata\_http\_tokens\_required](#input\_metadata\_http\_tokens\_required) | Whether or not the metadata service requires session tokens, also referred to as Instance Metadata Service Version 2, on the created NAT instances | `bool` | `true` | no | @@ -391,18 +363,19 @@ Available targets: | [open\_network\_acl\_ipv4\_rule\_number](#input\_open\_network\_acl\_ipv4\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv4 traffic generated by this module | `number` | `100` | no | | [open\_network\_acl\_ipv6\_rule\_number](#input\_open\_network\_acl\_ipv6\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv6 traffic generated by this module | `number` | `111` | no | | [private\_assign\_ipv6\_address\_on\_creation](#input\_private\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a private subnet will be assigned an IPv6 address | `bool` | `true` | no | -| [private\_dns64\_enabled](#input\_private\_dns64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic IPv6 addresses for IPv4-only destinations.
Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_network_route_enabled` to be `true` to be fully operational. | `bool` | `true` | no | +| [private\_dns64\_nat64\_enabled](#input\_private\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_route_table_enabled` to be `true` to be fully operational. | `bool` | `true` | no | | [private\_network\_acl\_id](#input\_private\_network\_acl\_id) | DEPRECATED: Use `private_open_network_acl_enabled` instead.
If set, no Network ACL will be created or assigned to the public subnets. | `string` | `""` | no | -| [private\_network\_route\_enabled](#input\_private\_network\_route\_enabled) | If true, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway
will be created for each private subnet (1:1). If false, you will need to create your own route table(s) and route(s). | `bool` | `true` | no | | [private\_open\_network\_acl\_enabled](#input\_private\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every private subnet, and a rule (number 100)
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | +| [private\_route\_table\_enabled](#input\_private\_route\_table\_enabled) | If true, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway
will be created for each private subnet (1:1). If false, you will need to create your own route table(s) and route(s). | `bool` | `true` | no | | [private\_subnets\_additional\_tags](#input\_private\_subnets\_additional\_tags) | Additional tags to be added to private subnets | `map(string)` | `{}` | no | | [private\_subnets\_enabled](#input\_private\_subnets\_enabled) | If false, do not create private subnets (or NAT gateways or instances) | `bool` | `true` | no | | [public\_assign\_ipv6\_address\_on\_creation](#input\_public\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a public subnet will be assigned an IPv6 address | `bool` | `true` | no | -| [public\_dns64\_enabled](#input\_public\_dns64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in public subnets will return synthetic IPv6 addresses for IPv4-only destinations.
Requires `nat_gateway_enabled` and `public_network_route_enabled` to be `true` to be fully operational. | `bool` | `false` | no | +| [public\_dns64\_nat64\_enabled](#input\_public\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in public subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `nat_gateway_enabled` and `public_route_table_enabled` to be `true` to be fully operational. | `bool` | `false` | no | | [public\_network\_acl\_id](#input\_public\_network\_acl\_id) | DEPRECATED: Use `public_open_network_acl_enabled` instead.
If set, no Network ACL will be created or assigned to the public subnets. | `string` | `""` | no | -| [public\_network\_route\_enabled](#input\_public\_network\_route\_enabled) | If `true`, a default route to the internet gateway will be added route table(s) associated with the public subnets.
If `public_dns64_enabled` and `nat_gateway_enabled` are also `true`, a route for NAT64 addresses to the NAT gateway will also be created.
If `false`, you will need to create your own route(s). | `bool` | `true` | no | | [public\_open\_network\_acl\_enabled](#input\_public\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every public subnet, and a rule
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | -| [public\_route\_table\_id](#input\_public\_route\_table\_id) | List optionally containing the ID of a single route table shared by all public subnets.
If provided, and `public_network_route_enabled` is `true`,
a route will be added to it, directing traffic to the VPC's Internet Gateway.
Ignored (treated as omitted) if `public_dns64_enabled` is `true`.
If omitted (or ignored), and `public_network_route_enabled` is `true`,
a network route table will be created for each public subnet (1:1) and routes added to it. | `list(string)` | `[]` | no | +| [public\_route\_table\_enabled](#input\_public\_route\_table\_enabled) | If `true`, network route table(s) will be created as determined by `public_route_table_per_subnet_enabled` and
appropriate routes will be added to destinations this module knows about.
If `false`, you will need to create your own route table(s) and route(s).
Ignored if `public_route_table_ids` is non-empty. | `bool` | `true` | no | +| [public\_route\_table\_ids](#input\_public\_route\_table\_ids) | List optionally containing the ID of a single route table shared by all public subnets
or exactly one route table ID for each public subnet.
If provided, it overrides `public_route_table_per_subnet_enabled`.
If omitted and `public_route_table_enabled` is `true`,
one or more network route tables will be created for the public subnets,
according to the setting of `public_route_table_per_subnet_enabled`. | `list(string)` | `[]` | no | +| [public\_route\_table\_per\_subnet\_enabled](#input\_public\_route\_table\_per\_subnet\_enabled) | If `true` (and `public_route_table_enabled` is `true), a separate network route table will be created for and associated with each public subnet.
If `false` (and `public\_route\_table\_enabled` is `true), a single network route table will be created and it will be associated with every public subnet.
If not set, it will be set to the value of `public_dns64_nat64_enabled`. | `bool` | `null` | no | | [public\_subnets\_additional\_tags](#input\_public\_subnets\_additional\_tags) | Additional tags to be added to public subnets | `map(string)` | `{}` | no | | [public\_subnets\_enabled](#input\_public\_subnets\_enabled) | If false, do not create public subnets.
Since NAT gateways and instances must be created in public subnets, these will also not be created when `false`.
Since the only default route for a private subnet is to a NAT gateway or instance,
route tables and routes will also not be created when `false`. | `bool` | `true` | no | | [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no | diff --git a/README.yaml b/README.yaml index 5c572458..d399eb25 100644 --- a/README.yaml +++ b/README.yaml @@ -50,9 +50,14 @@ description: |- Terraform module to provision public and private [`subnets`](http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Subnets.html) in an existing [`VPC`](https://aws.amazon.com/vpc) - __Note:__ this module is intended for use with an existing VPC and existing Internet Gateway. + __Note:__ This module is intended for use with an existing VPC and existing Internet Gateway. To create a new VPC, use [terraform-aws-vpc](https://github.com/cloudposse/terraform-aws-vpc) module. + _Note:__ Due to Terraform [limitations](https://github.com/hashicorp/terraform/issues/26755#issuecomment-719103775), + many optional inputs to this module are specified as a `list(string)` that can have zero or one elements, rather than + as `string` that could be empty or `null`. The designation of an input as a `list` type does not necessarily + mean that you can supply more than one value in the list, so check the input's description before supplying more than one value. + The core feature of this module is dividing up a given CIDR range so that a set of subnets each gets its own distinct CIDR range within that range, and then creating those subnets in the appropriate availability zones. The intention is to keep this module relatively simple and easy to use for the most popular use cases. @@ -89,7 +94,7 @@ description: |- ### Customization for special use cases - Various features are controlled by `bool` arguments with names ending in `_enabled`. By changing the default + Various features are controlled by `bool` inputs with names ending in `_enabled`. By changing the default values, you can enable or disable creation of public subnets, private subnets, route tables, NAT gateways, NAT instances, or Network ACLs. So for example, you could use this module to create only private subnets and the open Network ACL, and then add your own route table associations to the subnets diff --git a/docs/design.md b/docs/design.md index 4764b1df..f97abcd3 100644 --- a/docs/design.md +++ b/docs/design.md @@ -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 = ciel(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 ( 24 = 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` (log212) - - For `16 subnets` we need `4` `binary digits` (log216) - - For `7 subnets` we need `2.81` `binary digits` (log27) - - 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(log212)) - - For `16 subnets` we need `4` `binary digits` (ceil(log216)) - - For `7 subnets` we need `3` `binary digits` (ceil(log27)) - - 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 diff --git a/docs/terraform.md b/docs/terraform.md index cc3eee97..c55aef5f 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -69,8 +69,8 @@ | [additional\_tag\_map](#input\_additional\_tag\_map) | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.
This is for some rare cases where resources want additional configuration of tags
and therefore take a list of maps with tag key, value, and additional configuration. | `map(string)` | `{}` | no | | [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the `delimiter`
and treated as a single ID element. | `list(string)` | `[]` | no | | [availability\_zone\_attribute\_style](#input\_availability\_zone\_attribute\_style) | The style of Availability Zone code to use in tags and names. One of `full`, `short`, or `fixed`.
When using `availability_zone_ids`, IDs will first be translated into AZ names. | `string` | `"short"` | no | -| [availability\_zone\_ids](#input\_availability\_zone\_ids) | List of Availability Zones IDs where subnets will be created. Conflicts with `availability_zones`.
Useful in some regions when using only some AZs and you want to use the same ones across multiple accounts. | `list(string)` | `[]` | no | -| [availability\_zones](#input\_availability\_zones) | List of Availability Zones (AZs) where subnets will be created. Conflicts with `availability_zone_ids`.
The order of zones in the list ***must be stable*** or else Terraform will continually make changes.
If no AZs are specified, then `max_subnet_count` AZs will be selected in alphabetical order.
If `max_subnet_count > 0` and `length(var.availability_zones) > max_subnet_count`, the list
will be truncated. | `list(string)` | `[]` | no | +| [availability\_zone\_ids](#input\_availability\_zone\_ids) | List of Availability Zones IDs where subnets will be created. Overrides `availability_zones`.
Useful in some regions when using only some AZs and you want to use the same ones across multiple accounts. | `list(string)` | `[]` | no | +| [availability\_zones](#input\_availability\_zones) | List of Availability Zones (AZs) where subnets will be created. Ignored when `availability_zone_ids` is set.
The order of zones in the list ***must be stable*** or else Terraform will continually make changes.
If no AZs are specified, then `max_subnet_count` AZs will be selected in alphabetical order.
If `max_subnet_count > 0` and `length(var.availability_zones) > max_subnet_count`, the list
will be truncated. We recommend setting `availability_zones` and `max_subnet_count` explicitly as constant
(not computed) values for predictability, consistency, and stability. | `list(string)` | `[]` | no | | [aws\_route\_create\_timeout](#input\_aws\_route\_create\_timeout) | DEPRECTATED: Use `route_create_timeout` instead.
Time to wait for AWS route creation, specified as a Go Duration, e.g. `2m` | `string` | `null` | no | | [aws\_route\_delete\_timeout](#input\_aws\_route\_delete\_timeout) | DEPRECTATED: Use `route_delete_timeout` instead.
Time to wait for AWS route deletion, specified as a Go Duration, e.g. `2m` | `string` | `null` | no | | [cidr\_block](#input\_cidr\_block) | DEPRECATED: Use `ipv4_cidr_block` instead.
Base IPv4 CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`) | `string` | `null` | no | @@ -80,7 +80,7 @@ | [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no | | [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no | | [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).
Set to `0` for unlimited length.
Set to `null` for keep the existing setting, which defaults to `0`.
Does not affect `id_full`. | `number` | `null` | no | -| [igw\_id](#input\_igw\_id) | The Internet Gateway ID that the public subnets will route traffic to.
Used if `public_network_route_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | +| [igw\_id](#input\_igw\_id) | The Internet Gateway ID that the public subnets will route traffic to.
Used if `public_route_table_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | | [ipv4\_cidr\_block](#input\_ipv4\_cidr\_block) | Base IPv4 CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`). Ignored if `ipv4_cidrs` is set.
If no CIDR block is provided, the VPC's default IPv4 CIDR block will be used. | `list(string)` | `[]` | no | | [ipv4\_cidrs](#input\_ipv4\_cidrs) | Lists of CIDRs to assign to subnets. Order of CIDRs in the lists must not change over time.
Lists may contain more CIDRs than needed. |
list(object({
private = list(string)
public = list(string)
}))
| `[]` | no | | [ipv4\_enabled](#input\_ipv4\_enabled) | Set true to enable IPv4 addresses in the subnets | `bool` | `true` | no | @@ -90,7 +90,7 @@ | [ipv4\_public\_instance\_hostnames\_enabled](#input\_ipv4\_public\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the public subnets will be answered with A (IPv4) records. | `bool` | `false` | no | | [ipv6\_cidr\_block](#input\_ipv6\_cidr\_block) | Base IPv6 CIDR block from which `/64` subnet CIDRs will be assigned. Must be `/56`. (e.g. `2600:1f16:c52:ab00::/56`).
Ignored if `ipv6_cidrs` is set. If no CIDR block is provided, the VPC's default IPv6 CIDR block will be used. | `list(string)` | `[]` | no | | [ipv6\_cidrs](#input\_ipv6\_cidrs) | Lists of CIDRs to assign to subnets. Order of CIDRs in the lists must not change over time.
Lists may contain more CIDRs than needed. |
list(object({
private = list(string)
public = list(string)
}))
| `[]` | no | -| [ipv6\_egress\_only\_igw\_id](#input\_ipv6\_egress\_only\_igw\_id) | The Egress Only Internet Gateway ID the private IPv6 subnets will route traffic to.
Used if `private_network_route_enabled` is `true` and `ipv6_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | +| [ipv6\_egress\_only\_igw\_id](#input\_ipv6\_egress\_only\_igw\_id) | The Egress Only Internet Gateway ID the private IPv6 subnets will route traffic to.
Used if `private_route_table_enabled` is `true` and `ipv6_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | | [ipv6\_enabled](#input\_ipv6\_enabled) | Set true to enable IPv6 addresses in the subnets | `bool` | `false` | no | | [ipv6\_private\_instance\_hostnames\_enabled](#input\_ipv6\_private\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the private subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | | [ipv6\_public\_instance\_hostnames\_enabled](#input\_ipv6\_public\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the public subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | @@ -99,7 +99,7 @@ | [label\_value\_case](#input\_label\_value\_case) | Controls the letter case of ID elements (labels) as included in `id`,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper` and `none` (no transformation).
Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.
Default value: `lower`. | `string` | `null` | no | | [labels\_as\_tags](#input\_labels\_as\_tags) | Set of labels (ID elements) to include as tags in the `tags` output.
Default is to include all labels.
Tags with empty values will not be included in the `tags` output.
Set to `[]` to suppress all generated tags.
**Notes:**
The value of the `name` tag, if included, will be the `id`, not the `name`.
Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be
changed in later chained modules. Attempts to change it will be silently ignored. | `set(string)` |
[
"default"
]
| no | | [map\_public\_ip\_on\_launch](#input\_map\_public\_ip\_on\_launch) | If `true`, instances launched into a public subnet will be assigned a public IPv4 address | `bool` | `true` | no | -| [max\_subnet\_count](#input\_max\_subnet\_count) | Sets the maximum number of each type (public or private) of subnet to deploy.
0 will reserve a CIDR for every Availability Zone (excluding Local Zones) in the region, and
deploy a subnet in each availability zone specified in `availability_zones` or `availability_zone_ids`,
or every zone if none are specified. Recommended to set equal to the maximum number of AZs you anticipate using,
to avoid causing subnets to be destroyed and recreated with smaller IPv4 CIDRs when AWS adds an availability zone. | `number` | `0` | no | +| [max\_subnet\_count](#input\_max\_subnet\_count) | Sets the maximum number of each type (public or private) of subnet to deploy.
0 will reserve a CIDR for every Availability Zone (excluding Local Zones) in the region, and
deploy a subnet in each availability zone specified in `availability_zones` or `availability_zone_ids`,
or every zone if none are specified. We recommend setting this equal to the maximum number of AZs you anticipate using,
to avoid causing subnets to be destroyed and recreated with smaller IPv4 CIDRs when AWS adds an availability zone.
Due to Terraform limitations, you can not set `max_subnet_count` from a computed value, you have to set it
from an explicit constant. For most cases, `3` is a good choice. | `number` | `0` | no | | [metadata\_http\_endpoint\_enabled](#input\_metadata\_http\_endpoint\_enabled) | Whether the metadata service is available on the created NAT instances | `bool` | `true` | no | | [metadata\_http\_put\_response\_hop\_limit](#input\_metadata\_http\_put\_response\_hop\_limit) | The desired HTTP PUT response hop limit (between 1 and 64) for instance metadata requests on the created NAT instances | `number` | `1` | no | | [metadata\_http\_tokens\_required](#input\_metadata\_http\_tokens\_required) | Whether or not the metadata service requires session tokens, also referred to as Instance Metadata Service Version 2, on the created NAT instances | `bool` | `true` | no | @@ -115,18 +115,19 @@ | [open\_network\_acl\_ipv4\_rule\_number](#input\_open\_network\_acl\_ipv4\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv4 traffic generated by this module | `number` | `100` | no | | [open\_network\_acl\_ipv6\_rule\_number](#input\_open\_network\_acl\_ipv6\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv6 traffic generated by this module | `number` | `111` | no | | [private\_assign\_ipv6\_address\_on\_creation](#input\_private\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a private subnet will be assigned an IPv6 address | `bool` | `true` | no | -| [private\_dns64\_enabled](#input\_private\_dns64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic IPv6 addresses for IPv4-only destinations.
Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_network_route_enabled` to be `true` to be fully operational. | `bool` | `true` | no | +| [private\_dns64\_nat64\_enabled](#input\_private\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_route_table_enabled` to be `true` to be fully operational. | `bool` | `true` | no | | [private\_network\_acl\_id](#input\_private\_network\_acl\_id) | DEPRECATED: Use `private_open_network_acl_enabled` instead.
If set, no Network ACL will be created or assigned to the public subnets. | `string` | `""` | no | -| [private\_network\_route\_enabled](#input\_private\_network\_route\_enabled) | If true, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway
will be created for each private subnet (1:1). If false, you will need to create your own route table(s) and route(s). | `bool` | `true` | no | | [private\_open\_network\_acl\_enabled](#input\_private\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every private subnet, and a rule (number 100)
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | +| [private\_route\_table\_enabled](#input\_private\_route\_table\_enabled) | If true, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway
will be created for each private subnet (1:1). If false, you will need to create your own route table(s) and route(s). | `bool` | `true` | no | | [private\_subnets\_additional\_tags](#input\_private\_subnets\_additional\_tags) | Additional tags to be added to private subnets | `map(string)` | `{}` | no | | [private\_subnets\_enabled](#input\_private\_subnets\_enabled) | If false, do not create private subnets (or NAT gateways or instances) | `bool` | `true` | no | | [public\_assign\_ipv6\_address\_on\_creation](#input\_public\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a public subnet will be assigned an IPv6 address | `bool` | `true` | no | -| [public\_dns64\_enabled](#input\_public\_dns64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in public subnets will return synthetic IPv6 addresses for IPv4-only destinations.
Requires `nat_gateway_enabled` and `public_network_route_enabled` to be `true` to be fully operational. | `bool` | `false` | no | +| [public\_dns64\_nat64\_enabled](#input\_public\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in public subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `nat_gateway_enabled` and `public_route_table_enabled` to be `true` to be fully operational. | `bool` | `false` | no | | [public\_network\_acl\_id](#input\_public\_network\_acl\_id) | DEPRECATED: Use `public_open_network_acl_enabled` instead.
If set, no Network ACL will be created or assigned to the public subnets. | `string` | `""` | no | -| [public\_network\_route\_enabled](#input\_public\_network\_route\_enabled) | If `true`, a default route to the internet gateway will be added route table(s) associated with the public subnets.
If `public_dns64_enabled` and `nat_gateway_enabled` are also `true`, a route for NAT64 addresses to the NAT gateway will also be created.
If `false`, you will need to create your own route(s). | `bool` | `true` | no | | [public\_open\_network\_acl\_enabled](#input\_public\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every public subnet, and a rule
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | -| [public\_route\_table\_id](#input\_public\_route\_table\_id) | List optionally containing the ID of a single route table shared by all public subnets.
If provided, and `public_network_route_enabled` is `true`,
a route will be added to it, directing traffic to the VPC's Internet Gateway.
Ignored (treated as omitted) if `public_dns64_enabled` is `true`.
If omitted (or ignored), and `public_network_route_enabled` is `true`,
a network route table will be created for each public subnet (1:1) and routes added to it. | `list(string)` | `[]` | no | +| [public\_route\_table\_enabled](#input\_public\_route\_table\_enabled) | If `true`, network route table(s) will be created as determined by `public_route_table_per_subnet_enabled` and
appropriate routes will be added to destinations this module knows about.
If `false`, you will need to create your own route table(s) and route(s).
Ignored if `public_route_table_ids` is non-empty. | `bool` | `true` | no | +| [public\_route\_table\_ids](#input\_public\_route\_table\_ids) | List optionally containing the ID of a single route table shared by all public subnets
or exactly one route table ID for each public subnet.
If provided, it overrides `public_route_table_per_subnet_enabled`.
If omitted and `public_route_table_enabled` is `true`,
one or more network route tables will be created for the public subnets,
according to the setting of `public_route_table_per_subnet_enabled`. | `list(string)` | `[]` | no | +| [public\_route\_table\_per\_subnet\_enabled](#input\_public\_route\_table\_per\_subnet\_enabled) | If `true` (and `public_route_table_enabled` is `true), a separate network route table will be created for and associated with each public subnet.
If `false` (and `public\_route\_table\_enabled` is `true), a single network route table will be created and it will be associated with every public subnet.
If not set, it will be set to the value of `public_dns64_nat64_enabled`. | `bool` | `null` | no | | [public\_subnets\_additional\_tags](#input\_public\_subnets\_additional\_tags) | Additional tags to be added to public subnets | `map(string)` | `{}` | no | | [public\_subnets\_enabled](#input\_public\_subnets\_enabled) | If false, do not create public subnets.
Since NAT gateways and instances must be created in public subnets, these will also not be created when `false`.
Since the only default route for a private subnet is to a NAT gateway or instance,
route tables and routes will also not be created when `false`. | `bool` | `true` | no | | [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no | diff --git a/examples/complete/main.tf b/examples/complete/main.tf index ab22429d..7fabd4e0 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -16,8 +16,6 @@ module "vpc" { module "subnets" { source = "../../" - #source = "cloudposse/dynamic-subnets/aws" - #version = "1.0.0" availability_zones = var.availability_zones vpc_id = module.vpc.vpc_id diff --git a/main.tf b/main.tf index 5e29c666..edbfc361 100644 --- a/main.tf +++ b/main.tf @@ -129,18 +129,30 @@ locals { private4_enabled = local.private_enabled && local.ipv4_enabled private6_enabled = local.private_enabled && local.ipv6_enabled - public_dns64_enabled = local.public6_enabled && var.public_dns64_enabled - private_dns64_enabled = local.private6_enabled && var.private_dns64_enabled - - public_network_route_enabled = local.public_enabled && var.public_network_route_enabled - public_network_table_enabled = local.public_network_route_enabled && (length(var.public_route_table_id) == 0 || local.public_dns64_enabled) - public_network_table_count = local.public_network_table_enabled ? (local.public_dns64_enabled ? local.subnet_az_count : 1) : 0 - public_network_table_ids = local.public_network_table_enabled ? aws_route_table.public.*.id : var.public_route_table_id + public_dns64_enabled = local.public6_enabled && var.public_dns64_nat64_enabled + private_dns64_enabled = local.private6_enabled && var.private_dns64_nat64_enabled + + public_route_table_enabled = local.public_enabled && var.public_route_table_enabled + # Use `coalesce` to pick the highest priority value (null means go to next test) + public_route_table_count = coalesce( + # Do not bother with route tables if not creating subnets + local.public_enabled ? null : 0, + # Use provided route tables when provided + length(var.public_route_table_ids) == 0 ? null : length(var.public_route_table_ids), + # Do not create route tables when told not to: + var.public_route_table_enabled ? null : 0, + # Explicitly test var.public_route_table_per_subnet_enabled == true or == false + # because both will be false when var.public_route_table_per_subnet_enabled == null + var.public_route_table_per_subnet_enabled == true ? local.subnet_az_count : null, + var.public_route_table_per_subnet_enabled == false ? 1 : null, + local.public_dns64_enabled ? local.subnet_az_count : 1 + ) + create_public_route_tables = local.public_route_table_enabled && length(var.public_route_table_ids) == 0 + public_route_table_ids = local.create_public_route_tables ? aws_route_table.public.*.id : var.public_route_table_ids - private_network_route_enabled = local.private_enabled && (local.nat_enabled || local.ipv6_egress_only_configured) && var.private_network_route_enabled - private_network_table_enabled = local.private_network_route_enabled - private_network_table_count = local.private_network_table_enabled ? local.subnet_az_count : 0 - private_network_table_ids = local.private_network_table_enabled ? aws_route_table.private.*.id : [] + private_route_table_enabled = local.private_enabled && var.private_route_table_enabled + private_route_table_count = local.private_route_table_enabled ? local.subnet_az_count : 0 + private_route_table_ids = local.private_route_table_enabled ? aws_route_table.private.*.id : [] # public and private network ACLs # Support deprecated var.public_network_acl_id @@ -152,9 +164,8 @@ locals { # but since it must be placed in a public subnet, we consider it not required if we are not creating public subnets. nat_required = local.public_enabled && (local.private4_enabled || local.public_dns64_enabled) nat_gateway_enabled = local.nat_required && var.nat_gateway_enabled - # An AWS NAT instance does not perform NAT64, but we allow for one anyway because - # the user can supply their own NAT instance AMI that does support it. - nat_instance_enabled = local.nat_required && var.nat_instance_enabled + # An AWS NAT instance does not perform NAT64, and we choose not to support it at this time + nat_instance_enabled = local.public_enabled && local.private4_enabled && var.nat_instance_enabled nat_enabled = local.nat_gateway_enabled || local.nat_instance_enabled need_nat_eips = local.nat_enabled && length(var.nat_elastic_ips) == 0 need_nat_eip_data = local.nat_enabled && length(var.nat_elastic_ips) > 0 diff --git a/nat-gateway.tf b/nat-gateway.tf index e92da423..afbb8f65 100644 --- a/nat-gateway.tf +++ b/nat-gateway.tf @@ -26,9 +26,9 @@ resource "aws_nat_gateway" "default" { } resource "aws_route" "nat4" { - count = local.nat_gateway_enabled && local.private_network_route_enabled && local.ipv4_enabled ? local.private_network_table_count : 0 + count = local.nat_gateway_enabled && local.ipv4_enabled ? local.private_route_table_count : 0 - route_table_id = aws_route_table.private[count.index].id + route_table_id = local.private_route_table_ids[count.index] nat_gateway_id = aws_nat_gateway.default[count.index].id destination_cidr_block = "0.0.0.0/0" depends_on = [aws_route_table.private] @@ -40,9 +40,9 @@ resource "aws_route" "nat4" { } resource "aws_route" "private_nat64" { - count = local.nat_gateway_enabled && local.private_network_route_enabled && local.private_dns64_enabled ? local.private_network_table_count : 0 + count = local.nat_gateway_enabled && local.private_dns64_enabled ? local.private_route_table_count : 0 - route_table_id = aws_route_table.private[count.index].id + route_table_id = local.private_route_table_ids[count.index] nat_gateway_id = aws_nat_gateway.default[count.index].id destination_ipv6_cidr_block = local.nat64_cidr depends_on = [aws_route_table.private] @@ -54,9 +54,9 @@ resource "aws_route" "private_nat64" { } resource "aws_route" "public_nat64" { - count = local.nat_gateway_enabled && local.public_network_route_enabled && local.public_dns64_enabled ? local.public_network_table_count : 0 + count = local.nat_gateway_enabled && local.public_dns64_enabled ? local.public_route_table_count : 0 - route_table_id = aws_route_table.public[count.index].id + route_table_id = local.public_route_table_ids[count.index] nat_gateway_id = aws_nat_gateway.default[count.index].id destination_ipv6_cidr_block = local.nat64_cidr depends_on = [aws_route_table.public] diff --git a/nat-instance.tf b/nat-instance.tf index 4449724d..923d4864 100644 --- a/nat-instance.tf +++ b/nat-instance.tf @@ -129,9 +129,9 @@ resource "aws_eip_association" "nat_instance" { } resource "aws_route" "nat_instance" { - count = local.nat_instance_enabled && local.private_network_route_enabled ? local.private_network_table_count : 0 + count = local.nat_instance_enabled ? local.private_route_table_count : 0 - route_table_id = aws_route_table.private[count.index].id + route_table_id = local.private_route_table_ids[count.index] network_interface_id = aws_instance.nat_instance[count.index].primary_network_interface_id destination_cidr_block = "0.0.0.0/0" depends_on = [aws_route_table.private] diff --git a/private.tf b/private.tf index 7031b998..4d9e6b6a 100644 --- a/private.tf +++ b/private.tf @@ -29,7 +29,7 @@ resource "aws_subnet" "private" { ) assign_ipv6_address_on_creation = local.ipv6_enabled ? var.private_assign_ipv6_address_on_creation : null - enable_dns64 = local.ipv6_enabled ? var.private_dns64_enabled : null + enable_dns64 = local.ipv6_enabled ? local.private_dns64_enabled : null enable_resource_name_dns_a_record_on_launch = local.ipv4_enabled ? var.ipv4_private_instance_hostnames_enabled : null enable_resource_name_dns_aaaa_record_on_launch = local.ipv6_enabled ? var.ipv6_private_instance_hostnames_enabled : null @@ -43,9 +43,9 @@ resource "aws_subnet" "private" { } resource "aws_route_table" "private" { - # Currently private_network_table_count == subnet_az_count, + # Currently private_route_table_count == subnet_az_count, # but keep parallel to public route table configuration - count = local.private_network_table_count + count = local.private_route_table_count vpc_id = local.vpc_id @@ -58,9 +58,9 @@ resource "aws_route_table" "private" { } resource "aws_route" "private6" { - count = local.ipv6_egress_only_configured ? local.private_network_table_count : 0 + count = local.ipv6_egress_only_configured ? local.private_route_table_count : 0 - route_table_id = aws_route_table.private[count.index].id + route_table_id = local.private_route_table_ids[count.index] destination_ipv6_cidr_block = "::/0" egress_only_gateway_id = var.ipv6_egress_only_igw_id[0] @@ -71,11 +71,11 @@ resource "aws_route" "private6" { } resource "aws_route_table_association" "private" { - count = local.private_network_route_enabled ? local.subnet_az_count : 0 + count = local.private_route_table_enabled ? local.subnet_az_count : 0 subnet_id = aws_subnet.private[count.index].id # Use element() to "wrap around" and allow for a single table to be associated with all subnets - route_table_id = element(local.private_network_table_ids, count.index) + route_table_id = element(local.private_route_table_ids, count.index) } resource "aws_network_acl" "private" { diff --git a/public.tf b/public.tf index 0990eee9..fd83494d 100644 --- a/public.tf +++ b/public.tf @@ -27,7 +27,7 @@ resource "aws_subnet" "public" { map_public_ip_on_launch = local.ipv4_enabled ? var.map_public_ip_on_launch : null assign_ipv6_address_on_creation = local.ipv6_enabled ? var.public_assign_ipv6_address_on_creation : null - enable_dns64 = local.ipv6_enabled ? var.public_dns64_enabled : null + enable_dns64 = local.ipv6_enabled ? local.public_dns64_enabled : null enable_resource_name_dns_a_record_on_launch = local.ipv4_enabled ? var.ipv4_public_instance_hostnames_enabled : null enable_resource_name_dns_aaaa_record_on_launch = local.ipv6_enabled ? var.ipv6_public_instance_hostnames_enabled : null @@ -49,7 +49,7 @@ resource "aws_subnet" "public" { resource "aws_route_table" "public" { # May need 1 table or 1 per AZ - count = local.public_network_table_count + count = local.create_public_route_tables ? local.public_route_table_count : 0 vpc_id = local.vpc_id @@ -57,9 +57,9 @@ resource "aws_route_table" "public" { } resource "aws_route" "public" { - count = local.ipv4_enabled && local.igw_configured ? local.public_network_table_count : 0 + count = local.ipv4_enabled && local.igw_configured ? local.public_route_table_count : 0 - route_table_id = aws_route_table.public[count.index].id + route_table_id = local.public_route_table_ids[count.index] destination_cidr_block = "0.0.0.0/0" gateway_id = var.igw_id[0] @@ -70,9 +70,9 @@ resource "aws_route" "public" { } resource "aws_route" "public6" { - count = local.ipv6_enabled && local.igw_configured ? local.public_network_table_count : 0 + count = local.ipv6_enabled && local.igw_configured ? local.public_route_table_count : 0 - route_table_id = aws_route_table.public[count.index].id + route_table_id = local.public_route_table_ids[count.index] destination_ipv6_cidr_block = "::/0" gateway_id = var.igw_id[0] @@ -83,10 +83,10 @@ resource "aws_route" "public6" { } resource "aws_route_table_association" "public" { - count = local.public_network_table_enabled ? local.subnet_az_count : 0 + count = local.public_route_table_enabled ? local.subnet_az_count : 0 subnet_id = aws_subnet.public[count.index].id - route_table_id = element(local.public_network_table_ids, count.index) + route_table_id = element(local.public_route_table_ids, count.index) } resource "aws_network_acl" "public" { diff --git a/variables.tf b/variables.tf index 6bc7b7fd..9b48dc6b 100644 --- a/variables.tf +++ b/variables.tf @@ -7,7 +7,7 @@ variable "igw_id" { type = list(string) description = <<-EOT The Internet Gateway ID that the public subnets will route traffic to. - Used if `public_network_route_enabled` is `true`, ignored otherwise. + Used if `public_route_table_enabled` is `true`, ignored otherwise. EOT default = [] validation { @@ -20,7 +20,7 @@ variable "ipv6_egress_only_igw_id" { type = list(string) description = <<-EOT The Egress Only Internet Gateway ID the private IPv6 subnets will route traffic to. - Used if `private_network_route_enabled` is `true` and `ipv6_enabled` is `true`, ignored otherwise. + Used if `private_route_table_enabled` is `true` and `ipv6_enabled` is `true`, ignored otherwise. EOT default = [] validation { @@ -35,8 +35,10 @@ variable "max_subnet_count" { Sets the maximum number of each type (public or private) of subnet to deploy. 0 will reserve a CIDR for every Availability Zone (excluding Local Zones) in the region, and deploy a subnet in each availability zone specified in `availability_zones` or `availability_zone_ids`, - or every zone if none are specified. Recommended to set equal to the maximum number of AZs you anticipate using, + or every zone if none are specified. We recommend setting this equal to the maximum number of AZs you anticipate using, to avoid causing subnets to be destroyed and recreated with smaller IPv4 CIDRs when AWS adds an availability zone. + Due to Terraform limitations, you can not set `max_subnet_count` from a computed value, you have to set it + from an explicit constant. For most cases, `3` is a good choice. EOT default = 0 } @@ -133,11 +135,12 @@ variable "ipv6_cidrs" { variable "availability_zones" { type = list(string) description = <<-EOT - List of Availability Zones (AZs) where subnets will be created. Conflicts with `availability_zone_ids`. + List of Availability Zones (AZs) where subnets will be created. Ignored when `availability_zone_ids` is set. The order of zones in the list ***must be stable*** or else Terraform will continually make changes. If no AZs are specified, then `max_subnet_count` AZs will be selected in alphabetical order. If `max_subnet_count > 0` and `length(var.availability_zones) > max_subnet_count`, the list - will be truncated. + will be truncated. We recommend setting `availability_zones` and `max_subnet_count` explicitly as constant + (not computed) values for predictability, consistency, and stability. EOT default = [] } @@ -145,7 +148,7 @@ variable "availability_zones" { variable "availability_zone_ids" { type = list(string) description = <<-EOT - List of Availability Zones IDs where subnets will be created. Conflicts with `availability_zones`. + List of Availability Zones IDs where subnets will be created. Overrides `availability_zones`. Useful in some regions when using only some AZs and you want to use the same ones across multiple accounts. EOT default = [] @@ -196,20 +199,22 @@ variable "public_assign_ipv6_address_on_creation" { default = true } -variable "private_dns64_enabled" { +variable "private_dns64_nat64_enabled" { type = bool description = <<-EOT - If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic IPv6 addresses for IPv4-only destinations. - Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_network_route_enabled` to be `true` to be fully operational. + If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic + IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway. + Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_route_table_enabled` to be `true` to be fully operational. EOT default = true } -variable "public_dns64_enabled" { +variable "public_dns64_nat64_enabled" { type = bool description = <<-EOT - If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in public subnets will return synthetic IPv6 addresses for IPv4-only destinations. - Requires `nat_gateway_enabled` and `public_network_route_enabled` to be `true` to be fully operational. + If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in public subnets will return synthetic + IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway. + Requires `nat_gateway_enabled` and `public_route_table_enabled` to be `true` to be fully operational. EOT default = false } @@ -292,7 +297,7 @@ variable "open_network_acl_ipv6_rule_number" { default = 111 } -variable "private_network_route_enabled" { +variable "private_route_table_enabled" { type = bool description = <<-EOT If true, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway @@ -301,30 +306,41 @@ variable "private_network_route_enabled" { default = true } -# TODO Number of public route tables depends on public_dns64_enabled -variable "public_route_table_id" { +# TODO Number of public route tables depends on public_dns64_nat64_enabled +variable "public_route_table_ids" { type = list(string) description = <<-EOT - List optionally containing the ID of a single route table shared by all public subnets. - If provided, and `public_network_route_enabled` is `true`, - a route will be added to it, directing traffic to the VPC's Internet Gateway. - Ignored (treated as omitted) if `public_dns64_enabled` is `true`. - If omitted (or ignored), and `public_network_route_enabled` is `true`, - a network route table will be created for each public subnet (1:1) and routes added to it. + List optionally containing the ID of a single route table shared by all public subnets + or exactly one route table ID for each public subnet. + If provided, it overrides `public_route_table_per_subnet_enabled`. + If omitted and `public_route_table_enabled` is `true`, + one or more network route tables will be created for the public subnets, + according to the setting of `public_route_table_per_subnet_enabled`. EOT default = [] } -variable "public_network_route_enabled" { +variable "public_route_table_enabled" { type = bool description = <<-EOT - If `true`, a default route to the internet gateway will be added route table(s) associated with the public subnets. - If `public_dns64_enabled` and `nat_gateway_enabled` are also `true`, a route for NAT64 addresses to the NAT gateway will also be created. - If `false`, you will need to create your own route(s). + If `true`, network route table(s) will be created as determined by `public_route_table_per_subnet_enabled` and + appropriate routes will be added to destinations this module knows about. + If `false`, you will need to create your own route table(s) and route(s). + Ignored if `public_route_table_ids` is non-empty. EOT default = true } +variable "public_route_table_per_subnet_enabled" { + type = bool + description = <<-EOT + If `true` (and `public_route_table_enabled` is `true), a separate network route table will be created for and associated with each public subnet. + If `false` (and `public_route_table_enabled` is `true), a single network route table will be created and it will be associated with every public subnet. + If not set, it will be set to the value of `public_dns64_nat64_enabled`. + EOT + default = null +} + variable "route_create_timeout" { type = string default = "5m" From dde85f4fcda38ba3045258a1fb5c590cd06a198c Mon Sep 17 00:00:00 2001 From: Nuru Date: Wed, 11 May 2022 17:54:25 -0700 Subject: [PATCH 06/12] Add public and private label names --- main.tf | 11 +++++++---- private.tf | 4 ++-- public.tf | 4 ++-- variables-deprecated.tf | 27 --------------------------- variables.tf | 15 +++++++++++++-- 5 files changed, 24 insertions(+), 37 deletions(-) diff --git a/main.tf b/main.tf index edbfc361..834b88a1 100644 --- a/main.tf +++ b/main.tf @@ -156,16 +156,19 @@ locals { # public and private network ACLs # Support deprecated var.public_network_acl_id - public_open_network_acl_enabled = local.public_enabled && (length(var.public_network_acl_id) > 0 ? false : var.public_open_network_acl_enabled) + public_open_network_acl_enabled = local.public_enabled && var.public_open_network_acl_enabled # Support deprecated var.private_network_acl_id - private_open_network_acl_enabled = local.private_enabled && (length(var.private_network_acl_id) > 0 ? false : var.private_open_network_acl_enabled) + private_open_network_acl_enabled = local.private_enabled && var.private_open_network_acl_enabled # A NAT device is needed to NAT from private IPv4 to public IPv4 or to perform NAT64 for IPv6, # but since it must be placed in a public subnet, we consider it not required if we are not creating public subnets. nat_required = local.public_enabled && (local.private4_enabled || local.public_dns64_enabled) nat_gateway_enabled = local.nat_required && var.nat_gateway_enabled - # An AWS NAT instance does not perform NAT64, and we choose not to support it at this time - nat_instance_enabled = local.public_enabled && local.private4_enabled && var.nat_instance_enabled + # An AWS NAT instance does not perform NAT64, and we choose not to try to support NAT64 via NAT instances at this time. + # It does not make sense to create both a NAT Gateway and a NAT instance, since they perform the same function + # and occupy the same slot in a network routing table. Rather than try to create both, + # we favor the more powerful NAT Gateway over the deprecated NAT Instance and make the former suppress the latter. + nat_instance_enabled = local.public_enabled && local.private4_enabled && var.nat_instance_enabled && !local.nat_gateway_enabled nat_enabled = local.nat_gateway_enabled || local.nat_instance_enabled need_nat_eips = local.nat_enabled && length(var.nat_elastic_ips) == 0 need_nat_eip_data = local.nat_enabled && length(var.nat_elastic_ips) > 0 diff --git a/private.tf b/private.tf index 4d9e6b6a..7cee71dd 100644 --- a/private.tf +++ b/private.tf @@ -2,10 +2,10 @@ module "private_label" { source = "cloudposse/label/null" version = "0.25.0" - attributes = ["private"] + attributes = [var.private_label] tags = merge( var.private_subnets_additional_tags, - var.subnet_type_tag_key != null && var.subnet_type_tag_value_format != null ? { (var.subnet_type_tag_key) = format(var.subnet_type_tag_value_format, "private") } : {} + var.subnet_type_tag_key != null && var.subnet_type_tag_value_format != null ? { (var.subnet_type_tag_key) = format(var.subnet_type_tag_value_format, var.private_label) } : {} ) context = module.this.context diff --git a/public.tf b/public.tf index fd83494d..d52f1f29 100644 --- a/public.tf +++ b/public.tf @@ -2,10 +2,10 @@ module "public_label" { source = "cloudposse/label/null" version = "0.25.0" - attributes = ["public"] + attributes = [var.public_label] tags = merge( var.public_subnets_additional_tags, - var.subnet_type_tag_key != null && var.subnet_type_tag_value_format != null ? { (var.subnet_type_tag_key) = format(var.subnet_type_tag_value_format, "public") } : {} + var.subnet_type_tag_key != null && var.subnet_type_tag_value_format != null ? { (var.subnet_type_tag_key) = format(var.subnet_type_tag_value_format, var.public_label) } : {} ) context = module.this.context diff --git a/variables-deprecated.tf b/variables-deprecated.tf index 7db790f2..8749dba8 100644 --- a/variables-deprecated.tf +++ b/variables-deprecated.tf @@ -1,12 +1,3 @@ -variable "cidr_block" { - type = string - description = <<-EOT - DEPRECATED: Use `ipv4_cidr_block` instead. - Base IPv4 CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`) - EOT - default = null -} - variable "aws_route_create_timeout" { type = string description = <<-EOT @@ -44,24 +35,6 @@ variable "subnet_type_tag_value_format" { default = "%s" } -variable "public_network_acl_id" { - type = string - default = "" - description = <<-EOT - DEPRECATED: Use `public_open_network_acl_enabled` instead. - If set, no Network ACL will be created or assigned to the public subnets. - EOT -} - -variable "private_network_acl_id" { - type = string - default = "" - description = <<-EOT - DEPRECATED: Use `private_open_network_acl_enabled` instead. - If set, no Network ACL will be created or assigned to the public subnets. - EOT -} - variable "root_block_device_encrypted" { type = bool default = null diff --git a/variables.tf b/variables.tf index 9b48dc6b..b089f52c 100644 --- a/variables.tf +++ b/variables.tf @@ -60,6 +60,19 @@ variable "public_subnets_enabled" { default = true } +variable "private_label" { + type = string + description = "The string to use in IDs and elsewhere to distinguish resources for the private subnets from resources for the public subnets" + default = "private" +} + + +variable "public_label" { + type = string + description = "The string to use in IDs and elsewhere to distinguish resources for the private subnets from resources for the public subnets" + default = "public" +} + variable "ipv4_enabled" { type = bool description = "Set true to enable IPv4 addresses in the subnets" @@ -84,7 +97,6 @@ variable "ipv4_cidr_block" { error_message = "Only 1 ipv4_cidr_block can be provided. Use ipv4_cidrs to provide a CIDR per subnet." } } -locals { ipv4_cidr_block = var.cidr_block == null ? var.ipv4_cidr_block : [var.cidr_block] } variable "ipv6_cidr_block" { type = list(string) @@ -306,7 +318,6 @@ variable "private_route_table_enabled" { default = true } -# TODO Number of public route tables depends on public_dns64_nat64_enabled variable "public_route_table_ids" { type = list(string) description = <<-EOT From f3ce75d73a331ba4265cfefca377c847a94797c4 Mon Sep 17 00:00:00 2001 From: Nuru Date: Wed, 11 May 2022 17:54:54 -0700 Subject: [PATCH 07/12] Add v2 documentation --- README.md | 27 ++++--- README.yaml | 17 ++--- docs/migration-v1-v2.md | 152 ++++++++++++++++++++++++++++++++++++++++ docs/terraform.md | 5 +- 4 files changed, 176 insertions(+), 25 deletions(-) create mode 100644 docs/migration-v1-v2.md diff --git a/README.md b/README.md index abc82968..93f9d17c 100644 --- a/README.md +++ b/README.md @@ -28,15 +28,15 @@ --> -Terraform module to provision public and private [`subnets`](http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Subnets.html) in an existing [`VPC`](https://aws.amazon.com/vpc) +Terraform module to provision public and private [`subnets`](https://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Subnets.html) in an existing [`VPC`](https://aws.amazon.com/vpc) __Note:__ This module is intended for use with an existing VPC and existing Internet Gateway. To create a new VPC, use [terraform-aws-vpc](https://github.com/cloudposse/terraform-aws-vpc) module. -_Note:__ Due to Terraform [limitations](https://github.com/hashicorp/terraform/issues/26755#issuecomment-719103775), -many optional inputs to this module are specified as a `list(string)` that can have zero or one elements, rather than -as `string` that could be empty or `null`. The designation of an input as a `list` type does not necessarily +__Note:__ Due to Terraform [limitations](https://github.com/hashicorp/terraform/issues/26755#issuecomment-719103775), +many optional inputs to this module are specified as a `list(string)` that can have zero or one element, rather than +as a `string` that could be empty or `null`. The designation of an input as a `list` type does not necessarily mean that you can supply more than one value in the list, so check the input's description before supplying more than one value. The core feature of this module is dividing up a given CIDR range so that a set of subnets each gets its own @@ -165,8 +165,8 @@ module "subnets" { stage = "prod" name = "app" vpc_id = "vpc-XXXXXXXX" - igw_id = "igw-XXXXXXXX" - cidr_block = "10.0.0.0/16" + igw_id = ["igw-XXXXXXXX"] + ipv4_cidr_block = "10.0.0.0/16" availability_zones = ["us-east-1a", "us-east-1b"] } ``` @@ -180,7 +180,7 @@ module "subnets_with_existing_ips" { stage = "prod" name = "app" vpc_id = "vpc-XXXXXXXX" - igw_id = "igw-XXXXXXXX" + igw_id = ["igw-XXXXXXXX"] cidr_block = "10.0.0.0/16" availability_zones = ["us-east-1a", "us-east-1b"] nat_gateway_enabled = true @@ -188,8 +188,6 @@ module "subnets_with_existing_ips" { } ``` -Learn about [using providers](https://www.terraform.io/docs/configuration-0-11/modules.html#providers-within-modules) with terraform modules. - @@ -321,7 +319,6 @@ Available targets: | [availability\_zones](#input\_availability\_zones) | List of Availability Zones (AZs) where subnets will be created. Ignored when `availability_zone_ids` is set.
The order of zones in the list ***must be stable*** or else Terraform will continually make changes.
If no AZs are specified, then `max_subnet_count` AZs will be selected in alphabetical order.
If `max_subnet_count > 0` and `length(var.availability_zones) > max_subnet_count`, the list
will be truncated. We recommend setting `availability_zones` and `max_subnet_count` explicitly as constant
(not computed) values for predictability, consistency, and stability. | `list(string)` | `[]` | no | | [aws\_route\_create\_timeout](#input\_aws\_route\_create\_timeout) | DEPRECTATED: Use `route_create_timeout` instead.
Time to wait for AWS route creation, specified as a Go Duration, e.g. `2m` | `string` | `null` | no | | [aws\_route\_delete\_timeout](#input\_aws\_route\_delete\_timeout) | DEPRECTATED: Use `route_delete_timeout` instead.
Time to wait for AWS route deletion, specified as a Go Duration, e.g. `2m` | `string` | `null` | no | -| [cidr\_block](#input\_cidr\_block) | DEPRECATED: Use `ipv4_cidr_block` instead.
Base IPv4 CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`) | `string` | `null` | no | | [context](#input\_context) | Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as `null` to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional\_tag\_map, which are merged. | `any` |
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"unset"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
| no | | [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no | | [descriptor\_formats](#input\_descriptor\_formats) | Describe additional descriptors to be output in the `descriptors` output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
`{
format = string
labels = list(string)
}`
(Type is `any` so the map values can later be enhanced to provide additional options.)
`format` is a Terraform format string to be passed to the `format()` function.
`labels` is a list of labels, in order, to pass to `format()` function.
Label values will be normalized before being passed to `format()` so they will be
identical to how they appear in `id`.
Default is `{}` (`descriptors` output will be empty). | `any` | `{}` | no | @@ -364,14 +361,14 @@ Available targets: | [open\_network\_acl\_ipv6\_rule\_number](#input\_open\_network\_acl\_ipv6\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv6 traffic generated by this module | `number` | `111` | no | | [private\_assign\_ipv6\_address\_on\_creation](#input\_private\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a private subnet will be assigned an IPv6 address | `bool` | `true` | no | | [private\_dns64\_nat64\_enabled](#input\_private\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_route_table_enabled` to be `true` to be fully operational. | `bool` | `true` | no | -| [private\_network\_acl\_id](#input\_private\_network\_acl\_id) | DEPRECATED: Use `private_open_network_acl_enabled` instead.
If set, no Network ACL will be created or assigned to the public subnets. | `string` | `""` | no | +| [private\_label](#input\_private\_label) | The string to use in IDs and elsewhere to distinguish resources for the private subnets from resources for the public subnets | `string` | `"private"` | no | | [private\_open\_network\_acl\_enabled](#input\_private\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every private subnet, and a rule (number 100)
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | | [private\_route\_table\_enabled](#input\_private\_route\_table\_enabled) | If true, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway
will be created for each private subnet (1:1). If false, you will need to create your own route table(s) and route(s). | `bool` | `true` | no | | [private\_subnets\_additional\_tags](#input\_private\_subnets\_additional\_tags) | Additional tags to be added to private subnets | `map(string)` | `{}` | no | | [private\_subnets\_enabled](#input\_private\_subnets\_enabled) | If false, do not create private subnets (or NAT gateways or instances) | `bool` | `true` | no | | [public\_assign\_ipv6\_address\_on\_creation](#input\_public\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a public subnet will be assigned an IPv6 address | `bool` | `true` | no | | [public\_dns64\_nat64\_enabled](#input\_public\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in public subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `nat_gateway_enabled` and `public_route_table_enabled` to be `true` to be fully operational. | `bool` | `false` | no | -| [public\_network\_acl\_id](#input\_public\_network\_acl\_id) | DEPRECATED: Use `public_open_network_acl_enabled` instead.
If set, no Network ACL will be created or assigned to the public subnets. | `string` | `""` | no | +| [public\_label](#input\_public\_label) | The string to use in IDs and elsewhere to distinguish resources for the private subnets from resources for the public subnets | `string` | `"public"` | no | | [public\_open\_network\_acl\_enabled](#input\_public\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every public subnet, and a rule
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | | [public\_route\_table\_enabled](#input\_public\_route\_table\_enabled) | If `true`, network route table(s) will be created as determined by `public_route_table_per_subnet_enabled` and
appropriate routes will be added to destinations this module knows about.
If `false`, you will need to create your own route table(s) and route(s).
Ignored if `public_route_table_ids` is non-empty. | `bool` | `true` | no | | [public\_route\_table\_ids](#input\_public\_route\_table\_ids) | List optionally containing the ID of a single route table shared by all public subnets
or exactly one route table ID for each public subnet.
If provided, it overrides `public_route_table_per_subnet_enabled`.
If omitted and `public_route_table_enabled` is `true`,
one or more network route tables will be created for the public subnets,
according to the setting of `public_route_table_per_subnet_enabled`. | `list(string)` | `[]` | no | @@ -563,14 +560,16 @@ Check out [our other projects][github], [follow us on twitter][twitter], [apply ### Contributors -| [![Erik Osterman][osterman_avatar]][osterman_homepage]
[Erik Osterman][osterman_homepage] | [![Andriy Knysh][aknysh_avatar]][aknysh_homepage]
[Andriy Knysh][aknysh_homepage] | [![Sergey Vasilyev][s2504s_avatar]][s2504s_homepage]
[Sergey Vasilyev][s2504s_homepage] | [![Vladimir][SweetOps_avatar]][SweetOps_homepage]
[Vladimir][SweetOps_homepage] | [![Konstantin B][comeanother_avatar]][comeanother_homepage]
[Konstantin B][comeanother_homepage] | [![dcowan-vestmark][dcowan-vestmark_avatar]][dcowan-vestmark_homepage]
[dcowan-vestmark][dcowan-vestmark_homepage] | [![Ivan Pinatti][ivan-pinatti_avatar]][ivan-pinatti_homepage]
[Ivan Pinatti][ivan-pinatti_homepage] | [![Oscar Sullivan][osulli_avatar]][osulli_homepage]
[Oscar Sullivan][osulli_homepage] | [![Joe Niland][joe-niland_avatar]][joe-niland_homepage]
[Joe Niland][joe-niland_homepage] | -|---|---|---|---|---|---|---|---|---| +| [![Erik Osterman][osterman_avatar]][osterman_homepage]
[Erik Osterman][osterman_homepage] | [![Andriy Knysh][aknysh_avatar]][aknysh_homepage]
[Andriy Knysh][aknysh_homepage] | [![Nuru][Nuru_avatar]][Nuru_homepage]
[Nuru][Nuru_homepage] | [![Sergey Vasilyev][s2504s_avatar]][s2504s_homepage]
[Sergey Vasilyev][s2504s_homepage] | [![Vladimir][SweetOps_avatar]][SweetOps_homepage]
[Vladimir][SweetOps_homepage] | [![Konstantin B][comeanother_avatar]][comeanother_homepage]
[Konstantin B][comeanother_homepage] | [![dcowan-vestmark][dcowan-vestmark_avatar]][dcowan-vestmark_homepage]
[dcowan-vestmark][dcowan-vestmark_homepage] | [![Ivan Pinatti][ivan-pinatti_avatar]][ivan-pinatti_homepage]
[Ivan Pinatti][ivan-pinatti_homepage] | [![Oscar Sullivan][osulli_avatar]][osulli_homepage]
[Oscar Sullivan][osulli_homepage] | [![Joe Niland][joe-niland_avatar]][joe-niland_homepage]
[Joe Niland][joe-niland_homepage] | +|---|---|---|---|---|---|---|---|---|---| [osterman_homepage]: https://github.com/osterman [osterman_avatar]: https://img.cloudposse.com/150x150/https://github.com/osterman.png [aknysh_homepage]: https://github.com/aknysh [aknysh_avatar]: https://img.cloudposse.com/150x150/https://github.com/aknysh.png + [Nuru_homepage]: https://github.com/Nuru + [Nuru_avatar]: https://img.cloudposse.com/150x150/https://github.com/Nuru.png [s2504s_homepage]: https://github.com/s2504s [s2504s_avatar]: https://img.cloudposse.com/150x150/https://github.com/s2504s.png [SweetOps_homepage]: https://github.com/SweetOps diff --git a/README.yaml b/README.yaml index d399eb25..1623c780 100644 --- a/README.yaml +++ b/README.yaml @@ -47,15 +47,15 @@ related: url: "https://github.com/cloudposse/terraform-aws-named-subnets" # Short description of this project description: |- - Terraform module to provision public and private [`subnets`](http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Subnets.html) in an existing [`VPC`](https://aws.amazon.com/vpc) + Terraform module to provision public and private [`subnets`](https://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Subnets.html) in an existing [`VPC`](https://aws.amazon.com/vpc) __Note:__ This module is intended for use with an existing VPC and existing Internet Gateway. To create a new VPC, use [terraform-aws-vpc](https://github.com/cloudposse/terraform-aws-vpc) module. - _Note:__ Due to Terraform [limitations](https://github.com/hashicorp/terraform/issues/26755#issuecomment-719103775), - many optional inputs to this module are specified as a `list(string)` that can have zero or one elements, rather than - as `string` that could be empty or `null`. The designation of an input as a `list` type does not necessarily + __Note:__ Due to Terraform [limitations](https://github.com/hashicorp/terraform/issues/26755#issuecomment-719103775), + many optional inputs to this module are specified as a `list(string)` that can have zero or one element, rather than + as a `string` that could be empty or `null`. The designation of an input as a `list` type does not necessarily mean that you can supply more than one value in the list, so check the input's description before supplying more than one value. The core feature of this module is dividing up a given CIDR range so that a set of subnets each gets its own @@ -123,8 +123,8 @@ usage: |- stage = "prod" name = "app" vpc_id = "vpc-XXXXXXXX" - igw_id = "igw-XXXXXXXX" - cidr_block = "10.0.0.0/16" + igw_id = ["igw-XXXXXXXX"] + ipv4_cidr_block = "10.0.0.0/16" availability_zones = ["us-east-1a", "us-east-1b"] } ``` @@ -138,7 +138,7 @@ usage: |- stage = "prod" name = "app" vpc_id = "vpc-XXXXXXXX" - igw_id = "igw-XXXXXXXX" + igw_id = ["igw-XXXXXXXX"] cidr_block = "10.0.0.0/16" availability_zones = ["us-east-1a", "us-east-1b"] nat_gateway_enabled = true @@ -146,7 +146,6 @@ usage: |- } ``` - Learn about [using providers](https://www.terraform.io/docs/configuration-0-11/modules.html#providers-within-modules) with terraform modules. include: - "docs/design.md" - "docs/targets.md" @@ -157,6 +156,8 @@ contributors: github: "osterman" - name: "Andriy Knysh" github: "aknysh" + - name: "Nuru" + github: "Nuru" - name: "Sergey Vasilyev" github: "s2504s" - name: "Vladimir" diff --git a/docs/migration-v1-v2.md b/docs/migration-v1-v2.md new file mode 100644 index 00000000..00e4bb26 --- /dev/null +++ b/docs/migration-v1-v2.md @@ -0,0 +1,152 @@ +## Migration Notes for Dynamic Subnets v2.0 + +The first version of `terraform-aws-dynamic-subnets` was written for Terraform v0.9.1, +which was so limited that it do not even have a boolean data type, and +lists did not have first-class support. Cloud Posse continued to upgrade +the module over time, but retained many of the awkward constructions required +by those early Terraform versions. + +Version 2.0 of this module is nearly a complete rewrite, taking advantage +of the features available in Terraform v1.1, yet attempts to maintain +backward compatibility to a great extent, making migrating to it relatively easy. +Once you have adapted to the new inputs, Terraform should be able to +plan and apply with no substantive changes. + + + +#### Changes to Inputs + +All the breaking changes are due to changes in inputs. Fortunately, adapting to the +changes should be easy. In most cases, you simply change the name of the +input and put the old value inside a list. The notable exception +is [`subnet_type_tag_key`](#subnet_type_tag_key-and-subnet_type_tag_value_format). + + +## Optional Inputs + +Several inputs are optional. Previously, optional inputs were often +`string`s that could be empty or `null`. Unfortunately, due to Terraform [limitations](https://github.com/hashicorp/terraform/issues/26755#issuecomment-719103775), +we cannot condition the creation of a resource based on a value not known at +plan time, which put severe limitations on when and how this module can be used. +To work around this limitation, any optional string input is now a `list(string)` that can have zero or one element, +rather than a `string` that could be empty or `null`. + +### `cidr_block` replaced with `ipv4_cidr_block` + +Previously this module required an IPv4 CIDR block input as `cidr_block`. +This value is now optional, and, Since we had to make +a breaking change in type, we took the opportunity to reduce the ambiguity +of `cidr_block` and renamed it `ipv4_cidr_block`. + +***Migration***: Replace + +```hcl +cidr_block = aws_vpc.main.cidr_block +``` + +with: + +```hcl +ipv4_cidr_block = [aws_vpc.main.cidr_block] +``` + + +### `igw_id` + +Like `cidr_block`, the Internet Gateway ID `igw_id` +is now optional, and therefore was changed from `string` to `list(string)`. + +***Migration***: Replace + +```hcl +igw_id = aws_internet_gateway.default.id +``` + +with + +```hcl +igw_id = [aws_internet_gateway.default.id] +``` + +Because the Internet Gateway ID is now optional, you +can create a "public" set of subnets but not have the routed directly +to the internet. You could, instead, route them to a Transit Gateway, +VPC Endpoint, or other egress by adding your own entry into the route table. + + +### `subnet_type_tag_key` and `subnet_type_tag_value_format` +The inputs `subnet_type_tag_key` and `subnet_type_tag_value_format` have +been deprecated, and, critically, the default for `subnet_type_tag_key` +has changed from `"cpco.io/subnet/type"` to `null`. This means that +if you did not previously set a value for `subnet_type_tag_key`, your +subnets would have been tagged with the `"cpco.io/subnet/type"` tag in v1.0 +and those tags will be removed in v2.0. One mitigating circumstance is +that likely, if you were using the default value, you were not actually +using the resulting tags for anything, so removing the tags will likely +not affect you. + +The purpose of these tags was to be used as a filter to `data.aws_subnet_ids` +so you could automatically find all the public or private subnets and not +require them as inputs. Since there was no standard tag key for this purpose, +we provided `subnet_type_tag_key` as an option and automatically generated +those tags with values of either `public` or `private`. Unfortunately, +since there was no standard tag key for this purpose, we created a default +tag key of our own, which is less than ideal. + +If you were depending on these tags, then you should have been setting +your own value for `subnet_type_tag_key`, and the good news is that if you +continue to do so, then the tags will continue to be created as before. We do recommend you stop using +these particular tags at some convenient point in the future. If you want to tag public and/or private +subnets in some way to distinguish them, we suggest that instead you look at [standard +tag keys](https://aws.amazon.com/premiumsupport/knowledge-center/eks-vpc-subnet-discovery/) +like `kubernetes.io/role/elb` and `kubernetes.io/role/internal-elb` +and add them to `public_subnets_additional_tags` and `private_subnets_additional_tags` +respectively. + + +### vpc_default_route_table_id -> public_route_table_ids (list) + +Providing a route table ID for the public subnets was always optional, +but previously you could only supply a single route table ID to cover +all the public subnets. We have enhanced this feature to allow you to +supply separate route tables for each subnet, and so we have replaced +`vpc_default_route_table_id` with the more powerful `public_route_table_ids` variable. + +***Migration***: Replace + +```hcl +vpc_default_route_table_id = aws_vpc.main.default_route_table_id +``` + +with: + +```hcl +public_route_table_ids = [aws_vpc.main.default_route_table_id] +``` + +## Removed variables + +### `public_network_acl_id` and `private_network_acl_id` + +The variables `public_network_acl_id` and `private_network_acl_id` have been removed. +They did not work properly anyway, so it is unlikely you were using them. +You can now control the creation of network ACLs via `public_open_network_acl_enabled` +and `private_open_network_acl_enabled`. + +## Deprecated variables + +### `subnet_type_tag_key` and `subnet_type_tag_value_format` + +(See [above](#subnet_type_tag_key-and-subnet_type_tag_value_format)) + +### `aws_route_create_timeout` and `aws_route_delete_timeout` + +For clarity and predictability, `aws_route_create_timeout` and `aws_route_delete_timeout` +are deprecated in favor of `route_create_timeout` and `route_delete_timeout`. + +Also, the default values have changed from `2m` and `5m` to `5m` and `10m` respectively. + +### `root_block_device_encrypted` + +For clarity, `root_block_device_encrypted` is deprecated in favor of +`nat_instance_root_block_device_encrypted`. \ No newline at end of file diff --git a/docs/terraform.md b/docs/terraform.md index c55aef5f..699f9f3d 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -73,7 +73,6 @@ | [availability\_zones](#input\_availability\_zones) | List of Availability Zones (AZs) where subnets will be created. Ignored when `availability_zone_ids` is set.
The order of zones in the list ***must be stable*** or else Terraform will continually make changes.
If no AZs are specified, then `max_subnet_count` AZs will be selected in alphabetical order.
If `max_subnet_count > 0` and `length(var.availability_zones) > max_subnet_count`, the list
will be truncated. We recommend setting `availability_zones` and `max_subnet_count` explicitly as constant
(not computed) values for predictability, consistency, and stability. | `list(string)` | `[]` | no | | [aws\_route\_create\_timeout](#input\_aws\_route\_create\_timeout) | DEPRECTATED: Use `route_create_timeout` instead.
Time to wait for AWS route creation, specified as a Go Duration, e.g. `2m` | `string` | `null` | no | | [aws\_route\_delete\_timeout](#input\_aws\_route\_delete\_timeout) | DEPRECTATED: Use `route_delete_timeout` instead.
Time to wait for AWS route deletion, specified as a Go Duration, e.g. `2m` | `string` | `null` | no | -| [cidr\_block](#input\_cidr\_block) | DEPRECATED: Use `ipv4_cidr_block` instead.
Base IPv4 CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`) | `string` | `null` | no | | [context](#input\_context) | Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as `null` to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional\_tag\_map, which are merged. | `any` |
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"unset"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
| no | | [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no | | [descriptor\_formats](#input\_descriptor\_formats) | Describe additional descriptors to be output in the `descriptors` output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
`{
format = string
labels = list(string)
}`
(Type is `any` so the map values can later be enhanced to provide additional options.)
`format` is a Terraform format string to be passed to the `format()` function.
`labels` is a list of labels, in order, to pass to `format()` function.
Label values will be normalized before being passed to `format()` so they will be
identical to how they appear in `id`.
Default is `{}` (`descriptors` output will be empty). | `any` | `{}` | no | @@ -116,14 +115,14 @@ | [open\_network\_acl\_ipv6\_rule\_number](#input\_open\_network\_acl\_ipv6\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv6 traffic generated by this module | `number` | `111` | no | | [private\_assign\_ipv6\_address\_on\_creation](#input\_private\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a private subnet will be assigned an IPv6 address | `bool` | `true` | no | | [private\_dns64\_nat64\_enabled](#input\_private\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_route_table_enabled` to be `true` to be fully operational. | `bool` | `true` | no | -| [private\_network\_acl\_id](#input\_private\_network\_acl\_id) | DEPRECATED: Use `private_open_network_acl_enabled` instead.
If set, no Network ACL will be created or assigned to the public subnets. | `string` | `""` | no | +| [private\_label](#input\_private\_label) | The string to use in IDs and elsewhere to distinguish resources for the private subnets from resources for the public subnets | `string` | `"private"` | no | | [private\_open\_network\_acl\_enabled](#input\_private\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every private subnet, and a rule (number 100)
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | | [private\_route\_table\_enabled](#input\_private\_route\_table\_enabled) | If true, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway
will be created for each private subnet (1:1). If false, you will need to create your own route table(s) and route(s). | `bool` | `true` | no | | [private\_subnets\_additional\_tags](#input\_private\_subnets\_additional\_tags) | Additional tags to be added to private subnets | `map(string)` | `{}` | no | | [private\_subnets\_enabled](#input\_private\_subnets\_enabled) | If false, do not create private subnets (or NAT gateways or instances) | `bool` | `true` | no | | [public\_assign\_ipv6\_address\_on\_creation](#input\_public\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a public subnet will be assigned an IPv6 address | `bool` | `true` | no | | [public\_dns64\_nat64\_enabled](#input\_public\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in public subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `nat_gateway_enabled` and `public_route_table_enabled` to be `true` to be fully operational. | `bool` | `false` | no | -| [public\_network\_acl\_id](#input\_public\_network\_acl\_id) | DEPRECATED: Use `public_open_network_acl_enabled` instead.
If set, no Network ACL will be created or assigned to the public subnets. | `string` | `""` | no | +| [public\_label](#input\_public\_label) | The string to use in IDs and elsewhere to distinguish resources for the private subnets from resources for the public subnets | `string` | `"public"` | no | | [public\_open\_network\_acl\_enabled](#input\_public\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every public subnet, and a rule
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | | [public\_route\_table\_enabled](#input\_public\_route\_table\_enabled) | If `true`, network route table(s) will be created as determined by `public_route_table_per_subnet_enabled` and
appropriate routes will be added to destinations this module knows about.
If `false`, you will need to create your own route table(s) and route(s).
Ignored if `public_route_table_ids` is non-empty. | `bool` | `true` | no | | [public\_route\_table\_ids](#input\_public\_route\_table\_ids) | List optionally containing the ID of a single route table shared by all public subnets
or exactly one route table ID for each public subnet.
If provided, it overrides `public_route_table_per_subnet_enabled`.
If omitted and `public_route_table_enabled` is `true`,
one or more network route tables will be created for the public subnets,
according to the setting of `public_route_table_per_subnet_enabled`. | `list(string)` | `[]` | no | From 0c54dcc00a5c8721ddae9c9eacebb96c232035d0 Mon Sep 17 00:00:00 2001 From: Nuru Date: Wed, 11 May 2022 20:54:22 -0700 Subject: [PATCH 08/12] Fix #120, implement #154 --- README.md | 1 + docs/migration-v1-v2.md | 12 +++++++++++- docs/terraform.md | 1 + main.tf | 11 +++++++++-- nat-gateway.tf | 12 +++++------- nat-instance.tf | 10 +++------- outputs.tf | 2 +- variables.tf | 6 ++++++ 8 files changed, 37 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 93f9d17c..5fa4cdb6 100644 --- a/README.md +++ b/README.md @@ -344,6 +344,7 @@ Available targets: | [label\_value\_case](#input\_label\_value\_case) | Controls the letter case of ID elements (labels) as included in `id`,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper` and `none` (no transformation).
Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.
Default value: `lower`. | `string` | `null` | no | | [labels\_as\_tags](#input\_labels\_as\_tags) | Set of labels (ID elements) to include as tags in the `tags` output.
Default is to include all labels.
Tags with empty values will not be included in the `tags` output.
Set to `[]` to suppress all generated tags.
**Notes:**
The value of the `name` tag, if included, will be the `id`, not the `name`.
Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be
changed in later chained modules. Attempts to change it will be silently ignored. | `set(string)` |
[
"default"
]
| no | | [map\_public\_ip\_on\_launch](#input\_map\_public\_ip\_on\_launch) | If `true`, instances launched into a public subnet will be assigned a public IPv4 address | `bool` | `true` | no | +| [max\_nats](#input\_max\_nats) | Maximum number of NAT Gateways or NAT instances to create | `number` | `999` | no | | [max\_subnet\_count](#input\_max\_subnet\_count) | Sets the maximum number of each type (public or private) of subnet to deploy.
0 will reserve a CIDR for every Availability Zone (excluding Local Zones) in the region, and
deploy a subnet in each availability zone specified in `availability_zones` or `availability_zone_ids`,
or every zone if none are specified. We recommend setting this equal to the maximum number of AZs you anticipate using,
to avoid causing subnets to be destroyed and recreated with smaller IPv4 CIDRs when AWS adds an availability zone.
Due to Terraform limitations, you can not set `max_subnet_count` from a computed value, you have to set it
from an explicit constant. For most cases, `3` is a good choice. | `number` | `0` | no | | [metadata\_http\_endpoint\_enabled](#input\_metadata\_http\_endpoint\_enabled) | Whether the metadata service is available on the created NAT instances | `bool` | `true` | no | | [metadata\_http\_put\_response\_hop\_limit](#input\_metadata\_http\_put\_response\_hop\_limit) | The desired HTTP PUT response hop limit (between 1 and 64) for instance metadata requests on the created NAT instances | `number` | `1` | no | diff --git a/docs/migration-v1-v2.md b/docs/migration-v1-v2.md index 00e4bb26..586ef425 100644 --- a/docs/migration-v1-v2.md +++ b/docs/migration-v1-v2.md @@ -149,4 +149,14 @@ Also, the default values have changed from `2m` and `5m` to `5m` and `10m` respe ### `root_block_device_encrypted` For clarity, `root_block_device_encrypted` is deprecated in favor of -`nat_instance_root_block_device_encrypted`. \ No newline at end of file +`nat_instance_root_block_device_encrypted`. + +## Deprecated outputs + +### `nat_gateway_public_ips` + +This module now uses the same EIPs for both NAT Gateways and NAT instances, so +that switching from one to the other is possible without changing IPs. +Therefore the `nat_gateway_public_ips` output is deprecated in favor +of the `nat_ips` output. + diff --git a/docs/terraform.md b/docs/terraform.md index 699f9f3d..b7c3d5a2 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -98,6 +98,7 @@ | [label\_value\_case](#input\_label\_value\_case) | Controls the letter case of ID elements (labels) as included in `id`,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper` and `none` (no transformation).
Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.
Default value: `lower`. | `string` | `null` | no | | [labels\_as\_tags](#input\_labels\_as\_tags) | Set of labels (ID elements) to include as tags in the `tags` output.
Default is to include all labels.
Tags with empty values will not be included in the `tags` output.
Set to `[]` to suppress all generated tags.
**Notes:**
The value of the `name` tag, if included, will be the `id`, not the `name`.
Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be
changed in later chained modules. Attempts to change it will be silently ignored. | `set(string)` |
[
"default"
]
| no | | [map\_public\_ip\_on\_launch](#input\_map\_public\_ip\_on\_launch) | If `true`, instances launched into a public subnet will be assigned a public IPv4 address | `bool` | `true` | no | +| [max\_nats](#input\_max\_nats) | Maximum number of NAT Gateways or NAT instances to create | `number` | `999` | no | | [max\_subnet\_count](#input\_max\_subnet\_count) | Sets the maximum number of each type (public or private) of subnet to deploy.
0 will reserve a CIDR for every Availability Zone (excluding Local Zones) in the region, and
deploy a subnet in each availability zone specified in `availability_zones` or `availability_zone_ids`,
or every zone if none are specified. We recommend setting this equal to the maximum number of AZs you anticipate using,
to avoid causing subnets to be destroyed and recreated with smaller IPv4 CIDRs when AWS adds an availability zone.
Due to Terraform limitations, you can not set `max_subnet_count` from a computed value, you have to set it
from an explicit constant. For most cases, `3` is a good choice. | `number` | `0` | no | | [metadata\_http\_endpoint\_enabled](#input\_metadata\_http\_endpoint\_enabled) | Whether the metadata service is available on the created NAT instances | `bool` | `true` | no | | [metadata\_http\_put\_response\_hop\_limit](#input\_metadata\_http\_put\_response\_hop\_limit) | The desired HTTP PUT response hop limit (between 1 and 64) for instance metadata requests on the created NAT instances | `number` | `1` | no | diff --git a/main.tf b/main.tf index 834b88a1..530952e4 100644 --- a/main.tf +++ b/main.tf @@ -47,7 +47,12 @@ locals { subnet_possible_availability_zones = local.az_option_map[local.subnet_availability_zone_option] # Adjust list according to `max_subnet_count` - subnet_availability_zones = slice(local.subnet_possible_availability_zones, 0, var.max_subnet_count > 0 ? var.max_subnet_count : length(local.subnet_possible_availability_zones)) + subnet_availability_zones = ( + var.max_subnet_count == 0 || var.max_subnet_count >= length(local.subnet_possible_availability_zones) + ) ? ( + local.subnet_possible_availability_zones + ) : slice(local.subnet_possible_availability_zones, 0, var.max_subnet_count) + subnet_az_count = local.e ? length(local.subnet_availability_zones) : 0 subnet_count = ((local.public_enabled ? 1 : 0) + (local.private_enabled ? 1 : 0)) * local.subnet_az_count @@ -162,7 +167,9 @@ locals { # A NAT device is needed to NAT from private IPv4 to public IPv4 or to perform NAT64 for IPv6, # but since it must be placed in a public subnet, we consider it not required if we are not creating public subnets. - nat_required = local.public_enabled && (local.private4_enabled || local.public_dns64_enabled) + nat_required = local.public_enabled && (local.private4_enabled || local.public_dns64_enabled) + nat_count = min(local.subnet_az_count, var.max_nats) + nat_gateway_enabled = local.nat_required && var.nat_gateway_enabled # An AWS NAT instance does not perform NAT64, and we choose not to try to support NAT64 via NAT instances at this time. # It does not make sense to create both a NAT Gateway and a NAT instance, since they perform the same function diff --git a/nat-gateway.tf b/nat-gateway.tf index afbb8f65..dab4d53f 100644 --- a/nat-gateway.tf +++ b/nat-gateway.tf @@ -8,7 +8,7 @@ module "nat_label" { } resource "aws_nat_gateway" "default" { - count = local.nat_gateway_enabled ? local.subnet_az_count : 0 + count = local.nat_gateway_enabled ? local.nat_count : 0 allocation_id = local.nat_eip_allocations[count.index] subnet_id = aws_subnet.public[count.index].id @@ -20,16 +20,14 @@ resource "aws_nat_gateway" "default" { } ) - lifecycle { - create_before_destroy = true - } + depends_on = [aws_eip_association.nat_instance] } resource "aws_route" "nat4" { count = local.nat_gateway_enabled && local.ipv4_enabled ? local.private_route_table_count : 0 route_table_id = local.private_route_table_ids[count.index] - nat_gateway_id = aws_nat_gateway.default[count.index].id + nat_gateway_id = element(aws_nat_gateway.default.*.id, count.index) destination_cidr_block = "0.0.0.0/0" depends_on = [aws_route_table.private] @@ -43,7 +41,7 @@ resource "aws_route" "private_nat64" { count = local.nat_gateway_enabled && local.private_dns64_enabled ? local.private_route_table_count : 0 route_table_id = local.private_route_table_ids[count.index] - nat_gateway_id = aws_nat_gateway.default[count.index].id + nat_gateway_id = element(aws_nat_gateway.default.*.id, count.index) destination_ipv6_cidr_block = local.nat64_cidr depends_on = [aws_route_table.private] @@ -57,7 +55,7 @@ resource "aws_route" "public_nat64" { count = local.nat_gateway_enabled && local.public_dns64_enabled ? local.public_route_table_count : 0 route_table_id = local.public_route_table_ids[count.index] - nat_gateway_id = aws_nat_gateway.default[count.index].id + nat_gateway_id = element(aws_nat_gateway.default.*.id, count.index) destination_ipv6_cidr_block = local.nat64_cidr depends_on = [aws_route_table.public] diff --git a/nat-instance.tf b/nat-instance.tf index 923d4864..b9d27d04 100644 --- a/nat-instance.tf +++ b/nat-instance.tf @@ -73,7 +73,7 @@ data "aws_ami" "nat_instance" { # https://docs.aws.amazon.com/vpc/latest/userguide/VPC_NAT_Instance.html # https://dzone.com/articles/nat-instance-vs-nat-gateway resource "aws_instance" "nat_instance" { - count = local.nat_instance_enabled ? local.subnet_az_count : 0 + count = local.nat_instance_enabled ? local.nat_count : 0 ami = local.nat_instance_ami_id instance_type = var.nat_instance_type @@ -96,10 +96,6 @@ resource "aws_instance" "nat_instance" { #bridgecrew:skip=BC_AWS_LOGGING_26: Skipping requirement for detailed monitoring of NAT instance. associate_public_ip_address = true #tfsec:ignore:AWS012 - lifecycle { - create_before_destroy = true - } - metadata_options { http_endpoint = var.metadata_http_endpoint_enabled ? "enabled" : "disabled" http_put_response_hop_limit = var.metadata_http_put_response_hop_limit @@ -122,7 +118,7 @@ resource "aws_instance" "nat_instance" { } resource "aws_eip_association" "nat_instance" { - count = local.nat_instance_enabled ? local.subnet_az_count : 0 + count = local.nat_instance_enabled ? local.nat_count : 0 instance_id = aws_instance.nat_instance[count.index].id allocation_id = local.nat_eip_allocations[count.index] @@ -132,7 +128,7 @@ resource "aws_route" "nat_instance" { count = local.nat_instance_enabled ? local.private_route_table_count : 0 route_table_id = local.private_route_table_ids[count.index] - network_interface_id = aws_instance.nat_instance[count.index].primary_network_interface_id + network_interface_id = element(aws_instance.nat_instance.*.primary_network_interface_id, count.index) destination_cidr_block = "0.0.0.0/0" depends_on = [aws_route_table.private] diff --git a/outputs.tf b/outputs.tf index 9a43cd51..92884880 100644 --- a/outputs.tf +++ b/outputs.tf @@ -83,4 +83,4 @@ output "nat_ips" { output "nat_eip_allocation_ids" { description = "Elastic IP allocations in use by NAT" value = local.nat_eip_allocations -} \ No newline at end of file +} diff --git a/variables.tf b/variables.tf index b089f52c..21cdccd1 100644 --- a/variables.tf +++ b/variables.tf @@ -43,6 +43,12 @@ variable "max_subnet_count" { default = 0 } +variable "max_nats" { + type = number + description = "Maximum number of NAT Gateways or NAT instances to create" + default = 999 +} + variable "private_subnets_enabled" { type = bool description = "If false, do not create private subnets (or NAT gateways or instances)" From e5843965b370e3eb69d1762b589e795f3a5cf432 Mon Sep 17 00:00:00 2001 From: Nuru Date: Thu, 12 May 2022 11:02:08 -0700 Subject: [PATCH 09/12] Address reviewer comments, fix nat_instance_enabled=true --- README.md | 33 +++++++++++++++++++++++---------- README.yaml | 25 +++++++++++++++++++------ docs/design.md | 4 ++-- docs/migration-v1-v2.md | 8 ++++---- docs/terraform.md | 4 ++-- examples/complete/outputs.tf | 15 ++++++++++----- main.tf | 15 +++++++++++---- variables.tf | 14 ++++++++++---- 8 files changed, 81 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 5fa4cdb6..6b42725d 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,9 @@ many optional inputs to this module are specified as a `list(string)` that can h as a `string` that could be empty or `null`. The designation of an input as a `list` type does not necessarily mean that you can supply more than one value in the list, so check the input's description before supplying more than one value. -The core feature of this module is dividing up a given CIDR range so that a set of subnets each gets its own +The core function of this module is to create 2 sets of subnets, a "public" set with bidirectional access to the +public internet, and a "private" set behind a firewall with egress-only access to the public internet. This +includes dividing up a given CIDR range so that a each subnet gets its own distinct CIDR range within that range, and then creating those subnets in the appropriate availability zones. The intention is to keep this module relatively simple and easy to use for the most popular use cases. In its default configuration, this module creates 1 public subnet and 1 private subnet in each @@ -166,13 +168,15 @@ module "subnets" { name = "app" vpc_id = "vpc-XXXXXXXX" igw_id = ["igw-XXXXXXXX"] - ipv4_cidr_block = "10.0.0.0/16" + ipv4_cidr_block = "10.0.0.0/16" availability_zones = ["us-east-1a", "us-east-1b"] } ``` +Create only private subnets, route to transit gateway: + ```hcl -module "subnets_with_existing_ips" { +module "private_tgw_subnets" { source = "cloudposse/dynamic-subnets/aws" # Cloud Posse recommends pinning every module to a specific version # version = "x.x.x" @@ -181,10 +185,19 @@ module "subnets_with_existing_ips" { name = "app" vpc_id = "vpc-XXXXXXXX" igw_id = ["igw-XXXXXXXX"] - cidr_block = "10.0.0.0/16" + ipv4_cidr_block = "10.0.0.0/16" availability_zones = ["us-east-1a", "us-east-1b"] - nat_gateway_enabled = true - nat_elastic_ips = ["1.2.3.4", "1.2.3.5"] + + nat_gateway_enabled = false + public_subnets_enabled = false +} + +resource "aws_route" "private" { + count = length(module.private_tgw_subnets.private_route_table_ids) + + route_table_id = module.private_tgw_subnets.private_route_table_ids[count.index] + destination_cidr_block = "0.0.0.0/0" + transit_gateway_id = "tgw-XXXXXXXXX" } ``` @@ -216,7 +229,7 @@ cidr_count = existing_az_count * subnet_type_count 4. Calculate the number of bits needed to enumerate all the CIDRs: ``` -subnet_bits = ciel(log(cidr_count, 2)) +subnet_bits = ceil(log(cidr_count, 2)) ``` 5. Reserve CIDRs for private subnets using [`cidrsubnet`](https://www.terraform.io/language/functions/cidrsubnet): ``` @@ -231,7 +244,7 @@ public_subnet_cidrs = [ for netnumber in range(existing_az_count, existing_az_co 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 +to reserve these CIDRs, set `max_subnet_count` to the number of zones you are actually using. ## Makefile Targets ```text @@ -352,10 +365,10 @@ Available targets: | [name](#input\_name) | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a `tag`.
The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. | `string` | `null` | no | | [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no | | [nat\_elastic\_ips](#input\_nat\_elastic\_ips) | Existing Elastic IPs (not EIP IDs) to attach to the NAT Gateway(s) or Instance(s) instead of creating new ones. | `list(string)` | `[]` | no | -| [nat\_gateway\_enabled](#input\_nat\_gateway\_enabled) | Flag to enable/disable NAT Gateways to allow servers in the private subnets to access the Internet | `bool` | `true` | no | +| [nat\_gateway\_enabled](#input\_nat\_gateway\_enabled) | Set `true` to create NAT Gateways to perform IPv4 NAT and NAT64 as needed.
Defaults to `true` unless `nat_instance_enabled` is `true`. | `bool` | `null` | no | | [nat\_instance\_ami\_id](#input\_nat\_instance\_ami\_id) | A list optionally containing the ID of the AMI to use for the NAT instance.
If the list is empty (the default), the latest official AWS NAT instance AMI
will be used. NOTE: The Official NAT instance AMI is being phased out and
does not support NAT64. Use of a NAT gateway is recommended instead. | `list(string)` | `[]` | no | | [nat\_instance\_cpu\_credits\_override](#input\_nat\_instance\_cpu\_credits\_override) | NAT Instance credit option for CPU usage. Valid values are "standard" or "unlimited".
T3 and later instances are launched as unlimited by default. T2 instances are launched as standard by default. | `string` | `""` | no | -| [nat\_instance\_enabled](#input\_nat\_instance\_enabled) | Flag to enable/disable NAT Instances to allow servers in the private subnets to access the Internet | `bool` | `false` | no | +| [nat\_instance\_enabled](#input\_nat\_instance\_enabled) | Set `true` to create NAT Instances to perform IPv4 NAT.
Defaults to `false`. | `bool` | `null` | no | | [nat\_instance\_root\_block\_device\_encrypted](#input\_nat\_instance\_root\_block\_device\_encrypted) | Whether to encrypt the root block device on the created NAT instances | `bool` | `true` | no | | [nat\_instance\_type](#input\_nat\_instance\_type) | NAT Instance type | `string` | `"t3.micro"` | no | | [open\_network\_acl\_ipv4\_rule\_number](#input\_open\_network\_acl\_ipv4\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv4 traffic generated by this module | `number` | `100` | no | diff --git a/README.yaml b/README.yaml index 1623c780..bb66e4ed 100644 --- a/README.yaml +++ b/README.yaml @@ -58,7 +58,9 @@ description: |- as a `string` that could be empty or `null`. The designation of an input as a `list` type does not necessarily mean that you can supply more than one value in the list, so check the input's description before supplying more than one value. - The core feature of this module is dividing up a given CIDR range so that a set of subnets each gets its own + The core function of this module is to create 2 sets of subnets, a "public" set with bidirectional access to the + public internet, and a "private" set behind a firewall with egress-only access to the public internet. This + includes dividing up a given CIDR range so that a each subnet gets its own distinct CIDR range within that range, and then creating those subnets in the appropriate availability zones. The intention is to keep this module relatively simple and easy to use for the most popular use cases. In its default configuration, this module creates 1 public subnet and 1 private subnet in each @@ -124,13 +126,15 @@ usage: |- name = "app" vpc_id = "vpc-XXXXXXXX" igw_id = ["igw-XXXXXXXX"] - ipv4_cidr_block = "10.0.0.0/16" + ipv4_cidr_block = "10.0.0.0/16" availability_zones = ["us-east-1a", "us-east-1b"] } ``` + + Create only private subnets, route to transit gateway: ```hcl - module "subnets_with_existing_ips" { + module "private_tgw_subnets" { source = "cloudposse/dynamic-subnets/aws" # Cloud Posse recommends pinning every module to a specific version # version = "x.x.x" @@ -139,10 +143,19 @@ usage: |- name = "app" vpc_id = "vpc-XXXXXXXX" igw_id = ["igw-XXXXXXXX"] - cidr_block = "10.0.0.0/16" + ipv4_cidr_block = "10.0.0.0/16" availability_zones = ["us-east-1a", "us-east-1b"] - nat_gateway_enabled = true - nat_elastic_ips = ["1.2.3.4", "1.2.3.5"] + + nat_gateway_enabled = false + public_subnets_enabled = false + } + + resource "aws_route" "private" { + count = length(module.private_tgw_subnets.private_route_table_ids) + + route_table_id = module.private_tgw_subnets.private_route_table_ids[count.index] + destination_cidr_block = "0.0.0.0/0" + transit_gateway_id = "tgw-XXXXXXXXX" } ``` diff --git a/docs/design.md b/docs/design.md index f97abcd3..3f348a04 100644 --- a/docs/design.md +++ b/docs/design.md @@ -21,7 +21,7 @@ cidr_count = existing_az_count * subnet_type_count 4. Calculate the number of bits needed to enumerate all the CIDRs: ``` -subnet_bits = ciel(log(cidr_count, 2)) +subnet_bits = ceil(log(cidr_count, 2)) ``` 5. Reserve CIDRs for private subnets using [`cidrsubnet`](https://www.terraform.io/language/functions/cidrsubnet): ``` @@ -36,4 +36,4 @@ public_subnet_cidrs = [ for netnumber in range(existing_az_count, existing_az_co 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 +to reserve these CIDRs, set `max_subnet_count` to the number of zones you are actually using. diff --git a/docs/migration-v1-v2.md b/docs/migration-v1-v2.md index 586ef425..716a99e4 100644 --- a/docs/migration-v1-v2.md +++ b/docs/migration-v1-v2.md @@ -1,7 +1,7 @@ ## Migration Notes for Dynamic Subnets v2.0 The first version of `terraform-aws-dynamic-subnets` was written for Terraform v0.9.1, -which was so limited that it do not even have a boolean data type, and +which was so limited that it did not even have a boolean data type, and lists did not have first-class support. Cloud Posse continued to upgrade the module over time, but retained many of the awkward constructions required by those early Terraform versions. @@ -34,8 +34,8 @@ rather than a `string` that could be empty or `null`. ### `cidr_block` replaced with `ipv4_cidr_block` Previously this module required an IPv4 CIDR block input as `cidr_block`. -This value is now optional, and, Since we had to make -a breaking change in type, we took the opportunity to reduce the ambiguity +This value is now optional, and, since we had to make a breaking change in +its type anyway, we took the opportunity to reduce the ambiguity of `cidr_block` and renamed it `ipv4_cidr_block`. ***Migration***: Replace @@ -69,7 +69,7 @@ igw_id = [aws_internet_gateway.default.id] ``` Because the Internet Gateway ID is now optional, you -can create a "public" set of subnets but not have the routed directly +can create a "public" set of subnets but not have them routed directly to the internet. You could, instead, route them to a Transit Gateway, VPC Endpoint, or other egress by adding your own entry into the route table. diff --git a/docs/terraform.md b/docs/terraform.md index b7c3d5a2..c828bb93 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -106,10 +106,10 @@ | [name](#input\_name) | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a `tag`.
The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. | `string` | `null` | no | | [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no | | [nat\_elastic\_ips](#input\_nat\_elastic\_ips) | Existing Elastic IPs (not EIP IDs) to attach to the NAT Gateway(s) or Instance(s) instead of creating new ones. | `list(string)` | `[]` | no | -| [nat\_gateway\_enabled](#input\_nat\_gateway\_enabled) | Flag to enable/disable NAT Gateways to allow servers in the private subnets to access the Internet | `bool` | `true` | no | +| [nat\_gateway\_enabled](#input\_nat\_gateway\_enabled) | Set `true` to create NAT Gateways to perform IPv4 NAT and NAT64 as needed.
Defaults to `true` unless `nat_instance_enabled` is `true`. | `bool` | `null` | no | | [nat\_instance\_ami\_id](#input\_nat\_instance\_ami\_id) | A list optionally containing the ID of the AMI to use for the NAT instance.
If the list is empty (the default), the latest official AWS NAT instance AMI
will be used. NOTE: The Official NAT instance AMI is being phased out and
does not support NAT64. Use of a NAT gateway is recommended instead. | `list(string)` | `[]` | no | | [nat\_instance\_cpu\_credits\_override](#input\_nat\_instance\_cpu\_credits\_override) | NAT Instance credit option for CPU usage. Valid values are "standard" or "unlimited".
T3 and later instances are launched as unlimited by default. T2 instances are launched as standard by default. | `string` | `""` | no | -| [nat\_instance\_enabled](#input\_nat\_instance\_enabled) | Flag to enable/disable NAT Instances to allow servers in the private subnets to access the Internet | `bool` | `false` | no | +| [nat\_instance\_enabled](#input\_nat\_instance\_enabled) | Set `true` to create NAT Instances to perform IPv4 NAT.
Defaults to `false`. | `bool` | `null` | no | | [nat\_instance\_root\_block\_device\_encrypted](#input\_nat\_instance\_root\_block\_device\_encrypted) | Whether to encrypt the root block device on the created NAT instances | `bool` | `true` | no | | [nat\_instance\_type](#input\_nat\_instance\_type) | NAT Instance type | `string` | `"t3.micro"` | no | | [open\_network\_acl\_ipv4\_rule\_number](#input\_open\_network\_acl\_ipv4\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv4 traffic generated by this module | `number` | `100` | no | diff --git a/examples/complete/outputs.tf b/examples/complete/outputs.tf index 2496e57f..49b3a2ab 100644 --- a/examples/complete/outputs.tf +++ b/examples/complete/outputs.tf @@ -1,21 +1,26 @@ output "public_subnet_cidrs" { - value = module.subnets.public_subnet_cidrs + description = "IPv4 CIDRs assigned to the created public subnets" + value = module.subnets.public_subnet_cidrs } output "private_subnet_cidrs" { - value = module.subnets.private_subnet_cidrs + description = "IPv4 CIDRs assigned to the created private subnets" + value = module.subnets.private_subnet_cidrs } output "public_subnet_ipv6_cidrs" { - value = module.subnets.public_subnet_ipv6_cidrs + description = "IPv6 CIDRs assigned to the created public subnets" + value = module.subnets.public_subnet_ipv6_cidrs } output "private_subnet_ipv6_cidrs" { - value = module.subnets.private_subnet_ipv6_cidrs + description = "IPv6 CIDRs assigned to the created private subnets" + value = module.subnets.private_subnet_ipv6_cidrs } output "vpc_ipv6_cidr" { - value = module.vpc.vpc_ipv6_cidr_block + description = "Default IPv6 CIDR of the VPC" + value = module.vpc.vpc_ipv6_cidr_block } output "public_route_table_ids" { diff --git a/main.tf b/main.tf index 530952e4..b8822990 100644 --- a/main.tf +++ b/main.tf @@ -170,12 +170,19 @@ locals { nat_required = local.public_enabled && (local.private4_enabled || local.public_dns64_enabled) nat_count = min(local.subnet_az_count, var.max_nats) - nat_gateway_enabled = local.nat_required && var.nat_gateway_enabled # An AWS NAT instance does not perform NAT64, and we choose not to try to support NAT64 via NAT instances at this time. # It does not make sense to create both a NAT Gateway and a NAT instance, since they perform the same function # and occupy the same slot in a network routing table. Rather than try to create both, - # we favor the more powerful NAT Gateway over the deprecated NAT Instance and make the former suppress the latter. - nat_instance_enabled = local.public_enabled && local.private4_enabled && var.nat_instance_enabled && !local.nat_gateway_enabled + # we favor the more powerful NAT Gateway over the deprecated NAT Instance. + # However, we do not want to force people to set `nat_gateway_enabled` to `false` to enable a NAT Instance, + # so if `nat_instance_enabled` is set to true, we set the default for `nat_gateway_enabled` to `false`. + nat_gateway_setting = var.nat_instance_enabled == true ? var.nat_gateway_enabled == true : !( + var.nat_gateway_enabled == false # not true or null + ) + nat_instance_setting = local.nat_gateway_setting ? false : var.nat_instance_enabled == true # not false or null + + nat_gateway_enabled = local.nat_required && local.nat_gateway_setting + nat_instance_enabled = local.public_enabled && local.private4_enabled && local.nat_instance_setting nat_enabled = local.nat_gateway_enabled || local.nat_instance_enabled need_nat_eips = local.nat_enabled && length(var.nat_elastic_ips) == 0 need_nat_eip_data = local.nat_enabled && length(var.nat_elastic_ips) > 0 @@ -209,7 +216,7 @@ data "aws_eip" "nat" { resource "aws_eip" "default" { - count = local.need_nat_eips ? local.subnet_az_count : 0 + count = local.need_nat_eips ? local.nat_count : 0 vpc = true tags = merge( diff --git a/variables.tf b/variables.tf index 21cdccd1..2ea1938d 100644 --- a/variables.tf +++ b/variables.tf @@ -183,14 +183,20 @@ variable "availability_zone_attribute_style" { variable "nat_gateway_enabled" { type = bool - description = "Flag to enable/disable NAT Gateways to allow servers in the private subnets to access the Internet" - default = true + description = <<-EOT + Set `true` to create NAT Gateways to perform IPv4 NAT and NAT64 as needed. + Defaults to `true` unless `nat_instance_enabled` is `true`. + EOT + default = null } variable "nat_instance_enabled" { type = bool - description = "Flag to enable/disable NAT Instances to allow servers in the private subnets to access the Internet" - default = false + description = <<-EOT + Set `true` to create NAT Instances to perform IPv4 NAT. + Defaults to `false`. + EOT + default = null } variable "nat_elastic_ips" { From 898448ebc69aa56ab81b40ea58f9ba8f5abc90a8 Mon Sep 17 00:00:00 2001 From: Nuru Date: Fri, 13 May 2022 16:06:41 -0700 Subject: [PATCH 10/12] Rather than fix them, allow certain configurations to produce errors --- README.md | 6 ++--- docs/terraform.md | 6 ++--- examples/existing-ips/outputs.tf | 8 ++++--- main.tf | 24 ++++++++++++------- nat-gateway.tf | 2 +- outputs.tf | 11 +++++---- private.tf | 31 ++++++++++++++---------- public.tf | 37 +++++++++++++++------------- variables.tf | 41 +++++++++++++++++++------------- 9 files changed, 97 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index 6b42725d..de14c6bd 100644 --- a/README.md +++ b/README.md @@ -350,8 +350,8 @@ Available targets: | [ipv6\_cidrs](#input\_ipv6\_cidrs) | Lists of CIDRs to assign to subnets. Order of CIDRs in the lists must not change over time.
Lists may contain more CIDRs than needed. |
list(object({
private = list(string)
public = list(string)
}))
| `[]` | no | | [ipv6\_egress\_only\_igw\_id](#input\_ipv6\_egress\_only\_igw\_id) | The Egress Only Internet Gateway ID the private IPv6 subnets will route traffic to.
Used if `private_route_table_enabled` is `true` and `ipv6_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | | [ipv6\_enabled](#input\_ipv6\_enabled) | Set true to enable IPv6 addresses in the subnets | `bool` | `false` | no | -| [ipv6\_private\_instance\_hostnames\_enabled](#input\_ipv6\_private\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the private subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | -| [ipv6\_public\_instance\_hostnames\_enabled](#input\_ipv6\_public\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the public subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | +| [ipv6\_private\_instance\_hostnames\_enabled](#input\_ipv6\_private\_instance\_hostnames\_enabled) | If `true` (or if `ipv4_enabled` is false), DNS queries for instance hostnames in the private subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | +| [ipv6\_public\_instance\_hostnames\_enabled](#input\_ipv6\_public\_instance\_hostnames\_enabled) | If `true` (or if `ipv4_enabled` is false), DNS queries for instance hostnames in the public subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | | [label\_key\_case](#input\_label\_key\_case) | Controls the letter case of the `tags` keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper`.
Default value: `title`. | `string` | `null` | no | | [label\_order](#input\_label\_order) | The order in which the labels (ID elements) appear in the `id`.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. | `list(string)` | `null` | no | | [label\_value\_case](#input\_label\_value\_case) | Controls the letter case of ID elements (labels) as included in `id`,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper` and `none` (no transformation).
Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.
Default value: `lower`. | `string` | `null` | no | @@ -374,7 +374,7 @@ Available targets: | [open\_network\_acl\_ipv4\_rule\_number](#input\_open\_network\_acl\_ipv4\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv4 traffic generated by this module | `number` | `100` | no | | [open\_network\_acl\_ipv6\_rule\_number](#input\_open\_network\_acl\_ipv6\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv6 traffic generated by this module | `number` | `111` | no | | [private\_assign\_ipv6\_address\_on\_creation](#input\_private\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a private subnet will be assigned an IPv6 address | `bool` | `true` | no | -| [private\_dns64\_nat64\_enabled](#input\_private\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_route_table_enabled` to be `true` to be fully operational. | `bool` | `true` | no | +| [private\_dns64\_nat64\_enabled](#input\_private\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_route_table_enabled` to be `true` to be fully operational.
Defaults to `true` unless there is no public IPv4 subnet for egress, in which case it defaults to false. | `bool` | `null` | no | | [private\_label](#input\_private\_label) | The string to use in IDs and elsewhere to distinguish resources for the private subnets from resources for the public subnets | `string` | `"private"` | no | | [private\_open\_network\_acl\_enabled](#input\_private\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every private subnet, and a rule (number 100)
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | | [private\_route\_table\_enabled](#input\_private\_route\_table\_enabled) | If true, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway
will be created for each private subnet (1:1). If false, you will need to create your own route table(s) and route(s). | `bool` | `true` | no | diff --git a/docs/terraform.md b/docs/terraform.md index c828bb93..31e29c50 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -91,8 +91,8 @@ | [ipv6\_cidrs](#input\_ipv6\_cidrs) | Lists of CIDRs to assign to subnets. Order of CIDRs in the lists must not change over time.
Lists may contain more CIDRs than needed. |
list(object({
private = list(string)
public = list(string)
}))
| `[]` | no | | [ipv6\_egress\_only\_igw\_id](#input\_ipv6\_egress\_only\_igw\_id) | The Egress Only Internet Gateway ID the private IPv6 subnets will route traffic to.
Used if `private_route_table_enabled` is `true` and `ipv6_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | | [ipv6\_enabled](#input\_ipv6\_enabled) | Set true to enable IPv6 addresses in the subnets | `bool` | `false` | no | -| [ipv6\_private\_instance\_hostnames\_enabled](#input\_ipv6\_private\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the private subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | -| [ipv6\_public\_instance\_hostnames\_enabled](#input\_ipv6\_public\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the public subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | +| [ipv6\_private\_instance\_hostnames\_enabled](#input\_ipv6\_private\_instance\_hostnames\_enabled) | If `true` (or if `ipv4_enabled` is false), DNS queries for instance hostnames in the private subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | +| [ipv6\_public\_instance\_hostnames\_enabled](#input\_ipv6\_public\_instance\_hostnames\_enabled) | If `true` (or if `ipv4_enabled` is false), DNS queries for instance hostnames in the public subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | | [label\_key\_case](#input\_label\_key\_case) | Controls the letter case of the `tags` keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper`.
Default value: `title`. | `string` | `null` | no | | [label\_order](#input\_label\_order) | The order in which the labels (ID elements) appear in the `id`.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. | `list(string)` | `null` | no | | [label\_value\_case](#input\_label\_value\_case) | Controls the letter case of ID elements (labels) as included in `id`,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper` and `none` (no transformation).
Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.
Default value: `lower`. | `string` | `null` | no | @@ -115,7 +115,7 @@ | [open\_network\_acl\_ipv4\_rule\_number](#input\_open\_network\_acl\_ipv4\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv4 traffic generated by this module | `number` | `100` | no | | [open\_network\_acl\_ipv6\_rule\_number](#input\_open\_network\_acl\_ipv6\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv6 traffic generated by this module | `number` | `111` | no | | [private\_assign\_ipv6\_address\_on\_creation](#input\_private\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a private subnet will be assigned an IPv6 address | `bool` | `true` | no | -| [private\_dns64\_nat64\_enabled](#input\_private\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_route_table_enabled` to be `true` to be fully operational. | `bool` | `true` | no | +| [private\_dns64\_nat64\_enabled](#input\_private\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_route_table_enabled` to be `true` to be fully operational.
Defaults to `true` unless there is no public IPv4 subnet for egress, in which case it defaults to false. | `bool` | `null` | no | | [private\_label](#input\_private\_label) | The string to use in IDs and elsewhere to distinguish resources for the private subnets from resources for the public subnets | `string` | `"private"` | no | | [private\_open\_network\_acl\_enabled](#input\_private\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every private subnet, and a rule (number 100)
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | | [private\_route\_table\_enabled](#input\_private\_route\_table\_enabled) | If true, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway
will be created for each private subnet (1:1). If false, you will need to create your own route table(s) and route(s). | `bool` | `true` | no | diff --git a/examples/existing-ips/outputs.tf b/examples/existing-ips/outputs.tf index d929e554..3a77e208 100644 --- a/examples/existing-ips/outputs.tf +++ b/examples/existing-ips/outputs.tf @@ -1,7 +1,9 @@ output "existing_ips" { - value = aws_eip.nat_ips.*.public_ip + description = "IP Addresses created by this module for use by NAT" + value = aws_eip.nat_ips.*.public_ip } output "nat_ips" { - value = module.subnets.nat_ips -} \ No newline at end of file + description = "IP Addresses in use by NAT" + value = module.subnets.nat_ips +} diff --git a/main.tf b/main.tf index b8822990..5f084207 100644 --- a/main.tf +++ b/main.tf @@ -134,8 +134,11 @@ locals { private4_enabled = local.private_enabled && local.ipv4_enabled private6_enabled = local.private_enabled && local.ipv6_enabled - public_dns64_enabled = local.public6_enabled && var.public_dns64_nat64_enabled - private_dns64_enabled = local.private6_enabled && var.private_dns64_nat64_enabled + public_dns64_enabled = local.public6_enabled && var.public_dns64_nat64_enabled + # Set the default for private_dns64_enabled to true unless there is no IPv4 egress to enable it. + private_dns64_enabled = local.private6_enabled && ( + var.private_dns64_nat64_enabled == null ? local.public4_enabled : var.private_dns64_nat64_enabled + ) public_route_table_enabled = local.public_enabled && var.public_route_table_enabled # Use `coalesce` to pick the highest priority value (null means go to next test) @@ -165,12 +168,12 @@ locals { # Support deprecated var.private_network_acl_id private_open_network_acl_enabled = local.private_enabled && var.private_open_network_acl_enabled - # A NAT device is needed to NAT from private IPv4 to public IPv4 or to perform NAT64 for IPv6, - # but since it must be placed in a public subnet, we consider it not required if we are not creating public subnets. - nat_required = local.public_enabled && (local.private4_enabled || local.public_dns64_enabled) - nat_count = min(local.subnet_az_count, var.max_nats) - + # A NAT device is needed to NAT from private IPv4 to public IPv4 or to perform NAT64 for IPv6. # An AWS NAT instance does not perform NAT64, and we choose not to try to support NAT64 via NAT instances at this time. + nat_instance_useful = local.private4_enabled + nat_gateway_useful = local.nat_instance_useful || local.public_dns64_enabled || local.private_dns64_enabled + nat_count = min(local.subnet_az_count, var.max_nats) + # It does not make sense to create both a NAT Gateway and a NAT instance, since they perform the same function # and occupy the same slot in a network routing table. Rather than try to create both, # we favor the more powerful NAT Gateway over the deprecated NAT Instance. @@ -181,8 +184,11 @@ locals { ) nat_instance_setting = local.nat_gateway_setting ? false : var.nat_instance_enabled == true # not false or null - nat_gateway_enabled = local.nat_required && local.nat_gateway_setting - nat_instance_enabled = local.public_enabled && local.private4_enabled && local.nat_instance_setting + # We suppress creating NATs if not useful, but choose to attempt to create NATs + # when useful even if we know they will fail (e.g. due to no public subnets) + # to provide useful feedback to users. + nat_gateway_enabled = local.nat_gateway_useful && local.nat_gateway_setting + nat_instance_enabled = local.nat_instance_useful && local.nat_instance_setting nat_enabled = local.nat_gateway_enabled || local.nat_instance_enabled need_nat_eips = local.nat_enabled && length(var.nat_elastic_ips) == 0 need_nat_eip_data = local.nat_enabled && length(var.nat_elastic_ips) > 0 diff --git a/nat-gateway.tf b/nat-gateway.tf index dab4d53f..5897dda4 100644 --- a/nat-gateway.tf +++ b/nat-gateway.tf @@ -24,7 +24,7 @@ resource "aws_nat_gateway" "default" { } resource "aws_route" "nat4" { - count = local.nat_gateway_enabled && local.ipv4_enabled ? local.private_route_table_count : 0 + count = local.nat_gateway_enabled && local.private4_enabled ? local.private_route_table_count : 0 route_table_id = local.private_route_table_ids[count.index] nat_gateway_id = element(aws_nat_gateway.default.*.id, count.index) diff --git a/outputs.tf b/outputs.tf index 92884880..bde6740e 100644 --- a/outputs.tf +++ b/outputs.tf @@ -20,24 +20,27 @@ output "private_subnet_ids" { value = aws_subnet.private.*.id } +# Provide some consistency in CDIR outputs by always returning a list. +# Avoid (or at least reduce) `count` problems by toggling the return +# value via configuration rather than computing it via `compact()`. output "public_subnet_cidrs" { description = "IPv4 CIDR blocks of the created public subnets" - value = aws_subnet.public.*.cidr_block + value = local.public4_enabled ? aws_subnet.public.*.cidr_block : [] } output "public_subnet_ipv6_cidrs" { description = "IPv6 CIDR blocks of the created public subnets" - value = aws_subnet.public.*.ipv6_cidr_block + value = local.public6_enabled ? aws_subnet.public.*.ipv6_cidr_block : [] } output "private_subnet_cidrs" { description = "IPv4 CIDR blocks of the created private subnets" - value = aws_subnet.private.*.cidr_block + value = local.private4_enabled ? aws_subnet.private.*.cidr_block : [] } output "private_subnet_ipv6_cidrs" { description = "IPv6 CIDR blocks of the created private subnets" - value = aws_subnet.private.*.ipv6_cidr_block + value = local.private6_enabled ? aws_subnet.private.*.ipv6_cidr_block : [] } output "public_route_table_ids" { diff --git a/private.tf b/private.tf index 7cee71dd..0463da66 100644 --- a/private.tf +++ b/private.tf @@ -17,9 +17,9 @@ resource "aws_subnet" "private" { vpc_id = local.vpc_id availability_zone = local.subnet_availability_zones[count.index] - cidr_block = local.ipv4_enabled ? local.ipv4_private_subnet_cidrs[count.index] : null - ipv6_cidr_block = local.ipv6_enabled ? local.ipv6_private_subnet_cidrs[count.index] : null - ipv6_native = local.ipv6_enabled && !local.ipv4_enabled + cidr_block = local.private4_enabled ? local.ipv4_private_subnet_cidrs[count.index] : null + ipv6_cidr_block = local.private6_enabled ? local.ipv6_private_subnet_cidrs[count.index] : null + ipv6_native = local.private6_enabled && !local.private4_enabled tags = merge( module.private_label.tags, @@ -28,18 +28,23 @@ resource "aws_subnet" "private" { } ) - assign_ipv6_address_on_creation = local.ipv6_enabled ? var.private_assign_ipv6_address_on_creation : null - enable_dns64 = local.ipv6_enabled ? local.private_dns64_enabled : null + assign_ipv6_address_on_creation = local.private6_enabled ? var.private_assign_ipv6_address_on_creation : null + enable_dns64 = local.private6_enabled ? local.private_dns64_enabled : null - enable_resource_name_dns_a_record_on_launch = local.ipv4_enabled ? var.ipv4_private_instance_hostnames_enabled : null - enable_resource_name_dns_aaaa_record_on_launch = local.ipv6_enabled ? var.ipv6_private_instance_hostnames_enabled : null + enable_resource_name_dns_a_record_on_launch = local.private4_enabled ? var.ipv4_private_instance_hostnames_enabled : null + enable_resource_name_dns_aaaa_record_on_launch = local.private6_enabled ? var.ipv6_private_instance_hostnames_enabled || !local.private4_enabled : null - private_dns_hostname_type_on_launch = local.ipv4_enabled ? var.ipv4_private_instance_hostname_type : null + private_dns_hostname_type_on_launch = local.private4_enabled ? var.ipv4_private_instance_hostname_type : null lifecycle { # Ignore tags added by kops or kubernetes ignore_changes = [tags.kubernetes, tags.SubnetType] } + + timeouts { + create = var.subnet_create_timeout + delete = var.subnet_delete_timeout + } } resource "aws_route_table" "private" { @@ -88,7 +93,7 @@ resource "aws_network_acl" "private" { } resource "aws_network_acl_rule" "private4_ingress" { - count = local.private_open_network_acl_enabled && local.ipv4_enabled ? 1 : 0 + count = local.private_open_network_acl_enabled && local.private4_enabled ? 1 : 0 network_acl_id = aws_network_acl.private[0].id rule_action = "allow" @@ -102,7 +107,7 @@ resource "aws_network_acl_rule" "private4_ingress" { } resource "aws_network_acl_rule" "private4_egress" { - count = local.private_open_network_acl_enabled && local.ipv4_enabled ? 1 : 0 + count = local.private_open_network_acl_enabled && local.private4_enabled ? 1 : 0 network_acl_id = aws_network_acl.private[0].id rule_action = "allow" @@ -116,7 +121,7 @@ resource "aws_network_acl_rule" "private4_egress" { } resource "aws_network_acl_rule" "private6_ingress" { - count = local.private_open_network_acl_enabled && local.ipv6_enabled ? 1 : 0 + count = local.private_open_network_acl_enabled && local.private6_enabled ? 1 : 0 network_acl_id = aws_network_acl.private[0].id rule_action = "allow" @@ -130,7 +135,7 @@ resource "aws_network_acl_rule" "private6_ingress" { } resource "aws_network_acl_rule" "private6_egress" { - count = local.private_open_network_acl_enabled && local.ipv6_enabled ? 1 : 0 + count = local.private_open_network_acl_enabled && local.private6_enabled ? 1 : 0 network_acl_id = aws_network_acl.private[0].id rule_action = "allow" @@ -141,4 +146,4 @@ resource "aws_network_acl_rule" "private6_egress" { from_port = 0 to_port = 0 protocol = "-1" -} \ No newline at end of file +} diff --git a/public.tf b/public.tf index d52f1f29..d79b294d 100644 --- a/public.tf +++ b/public.tf @@ -19,20 +19,20 @@ resource "aws_subnet" "public" { # When provisioning both public and private subnets, the public subnets get the second set of CIDRs. # Use element()'s wrap-around behavior to handle the case where we are only provisioning public subnets. - cidr_block = local.ipv4_enabled ? element(local.ipv4_public_subnet_cidrs, count.index) : null - ipv6_cidr_block = local.ipv6_enabled ? element(local.ipv6_public_subnet_cidrs, count.index) : null - ipv6_native = local.ipv6_enabled && !local.ipv4_enabled + cidr_block = local.public4_enabled ? element(local.ipv4_public_subnet_cidrs, count.index) : null + ipv6_cidr_block = local.public6_enabled ? element(local.ipv6_public_subnet_cidrs, count.index) : null + ipv6_native = local.public6_enabled && !local.private4_enabled #bridgecrew:skip=BC_AWS_NETWORKING_53:Public VPCs should be allowed to default to public IPs - map_public_ip_on_launch = local.ipv4_enabled ? var.map_public_ip_on_launch : null + map_public_ip_on_launch = local.private4_enabled ? var.map_public_ip_on_launch : null - assign_ipv6_address_on_creation = local.ipv6_enabled ? var.public_assign_ipv6_address_on_creation : null - enable_dns64 = local.ipv6_enabled ? local.public_dns64_enabled : null + assign_ipv6_address_on_creation = local.public6_enabled ? var.public_assign_ipv6_address_on_creation : null + enable_dns64 = local.public6_enabled ? local.public_dns64_enabled : null - enable_resource_name_dns_a_record_on_launch = local.ipv4_enabled ? var.ipv4_public_instance_hostnames_enabled : null - enable_resource_name_dns_aaaa_record_on_launch = local.ipv6_enabled ? var.ipv6_public_instance_hostnames_enabled : null + enable_resource_name_dns_a_record_on_launch = local.private4_enabled ? var.ipv4_public_instance_hostnames_enabled : null + enable_resource_name_dns_aaaa_record_on_launch = local.public6_enabled ? var.ipv6_public_instance_hostnames_enabled || !local.private4_enabled : null - private_dns_hostname_type_on_launch = local.ipv4_enabled ? var.ipv4_public_instance_hostname_type : null + private_dns_hostname_type_on_launch = local.private4_enabled ? var.ipv4_public_instance_hostname_type : null tags = merge( @@ -45,6 +45,11 @@ resource "aws_subnet" "public" { lifecycle { ignore_changes = [tags.kubernetes, tags.SubnetType] } + + timeouts { + create = var.subnet_create_timeout + delete = var.subnet_delete_timeout + } } resource "aws_route_table" "public" { @@ -57,7 +62,7 @@ resource "aws_route_table" "public" { } resource "aws_route" "public" { - count = local.ipv4_enabled && local.igw_configured ? local.public_route_table_count : 0 + count = local.private4_enabled && local.igw_configured ? local.public_route_table_count : 0 route_table_id = local.public_route_table_ids[count.index] destination_cidr_block = "0.0.0.0/0" @@ -70,7 +75,7 @@ resource "aws_route" "public" { } resource "aws_route" "public6" { - count = local.ipv6_enabled && local.igw_configured ? local.public_route_table_count : 0 + count = local.public6_enabled && local.igw_configured ? local.public_route_table_count : 0 route_table_id = local.public_route_table_ids[count.index] destination_ipv6_cidr_block = "::/0" @@ -99,7 +104,7 @@ resource "aws_network_acl" "public" { } resource "aws_network_acl_rule" "public4_ingress" { - count = local.public_open_network_acl_enabled && local.ipv4_enabled ? 1 : 0 + count = local.public_open_network_acl_enabled && local.private4_enabled ? 1 : 0 network_acl_id = aws_network_acl.public[0].id rule_action = "allow" @@ -113,7 +118,7 @@ resource "aws_network_acl_rule" "public4_ingress" { } resource "aws_network_acl_rule" "public4_egress" { - count = local.public_open_network_acl_enabled && local.ipv4_enabled ? 1 : 0 + count = local.public_open_network_acl_enabled && local.private4_enabled ? 1 : 0 network_acl_id = aws_network_acl.public[0].id rule_action = "allow" @@ -127,7 +132,7 @@ resource "aws_network_acl_rule" "public4_egress" { } resource "aws_network_acl_rule" "public6_ingress" { - count = local.public_open_network_acl_enabled && local.ipv6_enabled ? 1 : 0 + count = local.public_open_network_acl_enabled && local.public6_enabled ? 1 : 0 network_acl_id = aws_network_acl.public[0].id rule_action = "allow" @@ -141,7 +146,7 @@ resource "aws_network_acl_rule" "public6_ingress" { } resource "aws_network_acl_rule" "public6_egress" { - count = local.public_open_network_acl_enabled && local.ipv6_enabled ? 1 : 0 + count = local.public_open_network_acl_enabled && local.public6_enabled ? 1 : 0 network_acl_id = aws_network_acl.public[0].id rule_action = "allow" @@ -152,4 +157,4 @@ resource "aws_network_acl_rule" "public6_egress" { from_port = 0 to_port = 0 protocol = "-1" -} \ No newline at end of file +} diff --git a/variables.tf b/variables.tf index 2ea1938d..4b4552fe 100644 --- a/variables.tf +++ b/variables.tf @@ -229,8 +229,9 @@ variable "private_dns64_nat64_enabled" { If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway. Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_route_table_enabled` to be `true` to be fully operational. + Defaults to `true` unless there is no public IPv4 subnet for egress, in which case it defaults to false. EOT - default = true + default = null } variable "public_dns64_nat64_enabled" { @@ -245,46 +246,50 @@ variable "public_dns64_nat64_enabled" { variable "ipv4_private_instance_hostname_type" { type = string - default = "ip-name" description = <<-EOT How to generate the DNS name for the instances in the private subnets. Either `ip-name` to generate it from the IPv4 address, or `resource-name` to generate it from the instance ID. EOT + default = "ip-name" } variable "ipv4_private_instance_hostnames_enabled" { type = bool - default = false description = "If `true`, DNS queries for instance hostnames in the private subnets will be answered with A (IPv4) records." + default = false } variable "ipv6_private_instance_hostnames_enabled" { type = bool + description = <<-EOT + If `true` (or if `ipv4_enabled` is false), DNS queries for instance hostnames in the private subnets will be answered with AAAA (IPv6) records. + EOT default = false - description = "If `true`, DNS queries for instance hostnames in the private subnets will be answered with AAAA (IPv6) records." } variable "ipv4_public_instance_hostname_type" { type = string - default = "ip-name" description = <<-EOT How to generate the DNS name for the instances in the public subnets. Either `ip-name` to generate it from the IPv4 address, or `resource-name` to generate it from the instance ID. EOT + default = "ip-name" } variable "ipv4_public_instance_hostnames_enabled" { type = bool - default = false description = "If `true`, DNS queries for instance hostnames in the public subnets will be answered with A (IPv4) records." + default = false } variable "ipv6_public_instance_hostnames_enabled" { type = bool + description = <<-EOT + If `true` (or if `ipv4_enabled` is false), DNS queries for instance hostnames in the public subnets will be answered with AAAA (IPv6) records. + EOT default = false - description = "If `true`, DNS queries for instance hostnames in the public subnets will be answered with AAAA (IPv6) records." } variable "private_open_network_acl_enabled" { @@ -366,40 +371,42 @@ variable "public_route_table_per_subnet_enabled" { variable "route_create_timeout" { type = string - default = "5m" description = "Time to wait for a network routing table entry to be created, specified as a Go Duration, e.g. `2m`" + default = "5m" } locals { route_create_timeout = var.aws_route_create_timeout == null ? var.route_create_timeout : var.aws_route_create_timeout } variable "route_delete_timeout" { type = string - default = "10m" description = "Time to wait for a network routing table entry to be deleted, specified as a Go Duration, e.g. `2m`" + default = "10m" } locals { route_delete_timeout = var.aws_route_delete_timeout == null ? var.route_delete_timeout : var.aws_route_delete_timeout } variable "subnet_create_timeout" { type = string - default = "10m" description = "Time to wait for a subnet to be created, specified as a Go Duration, e.g. `2m`" + # 10m is the AWS Provider's default value + default = "10m" } variable "subnet_delete_timeout" { type = string - default = "20m" description = "Time to wait for a subnet to be deleted, specified as a Go Duration, e.g. `5m`" + # 20m is the AWS Provider's default value + default = "20m" } variable "private_subnets_additional_tags" { type = map(string) - default = {} description = "Additional tags to be added to private subnets" + default = {} } variable "public_subnets_additional_tags" { type = map(string) - default = {} description = "Additional tags to be added to public subnets" + default = {} } ############## NAT instance configuration ################### @@ -443,25 +450,25 @@ variable "nat_instance_cpu_credits_override" { variable "metadata_http_endpoint_enabled" { type = bool - default = true description = "Whether the metadata service is available on the created NAT instances" + default = true } variable "metadata_http_put_response_hop_limit" { type = number - default = 1 description = "The desired HTTP PUT response hop limit (between 1 and 64) for instance metadata requests on the created NAT instances" + default = 1 } variable "metadata_http_tokens_required" { type = bool - default = true description = "Whether or not the metadata service requires session tokens, also referred to as Instance Metadata Service Version 2, on the created NAT instances" + default = true } variable "nat_instance_root_block_device_encrypted" { type = bool - default = true description = "Whether to encrypt the root block device on the created NAT instances" + default = true } locals { nat_instance_root_block_device_encrypted = var.root_block_device_encrypted == null ? var.nat_instance_root_block_device_encrypted : var.root_block_device_encrypted } From 0b6adef11bd5e37f2d91b72ebaad37245b085ef0 Mon Sep 17 00:00:00 2001 From: Nuru Date: Fri, 13 May 2022 17:06:57 -0700 Subject: [PATCH 11/12] Minor documentation format fix --- README.md | 4 ++-- docs/terraform.md | 4 ++-- variables.tf | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index de14c6bd..f6c8c3c4 100644 --- a/README.md +++ b/README.md @@ -350,7 +350,7 @@ Available targets: | [ipv6\_cidrs](#input\_ipv6\_cidrs) | Lists of CIDRs to assign to subnets. Order of CIDRs in the lists must not change over time.
Lists may contain more CIDRs than needed. |
list(object({
private = list(string)
public = list(string)
}))
| `[]` | no | | [ipv6\_egress\_only\_igw\_id](#input\_ipv6\_egress\_only\_igw\_id) | The Egress Only Internet Gateway ID the private IPv6 subnets will route traffic to.
Used if `private_route_table_enabled` is `true` and `ipv6_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | | [ipv6\_enabled](#input\_ipv6\_enabled) | Set true to enable IPv6 addresses in the subnets | `bool` | `false` | no | -| [ipv6\_private\_instance\_hostnames\_enabled](#input\_ipv6\_private\_instance\_hostnames\_enabled) | If `true` (or if `ipv4_enabled` is false), DNS queries for instance hostnames in the private subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | +| [ipv6\_private\_instance\_hostnames\_enabled](#input\_ipv6\_private\_instance\_hostnames\_enabled) | If `true` (or if `ipv4_enabled` is `false`), DNS queries for instance hostnames in the private subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | | [ipv6\_public\_instance\_hostnames\_enabled](#input\_ipv6\_public\_instance\_hostnames\_enabled) | If `true` (or if `ipv4_enabled` is false), DNS queries for instance hostnames in the public subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | | [label\_key\_case](#input\_label\_key\_case) | Controls the letter case of the `tags` keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper`.
Default value: `title`. | `string` | `null` | no | | [label\_order](#input\_label\_order) | The order in which the labels (ID elements) appear in the `id`.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. | `list(string)` | `null` | no | @@ -374,7 +374,7 @@ Available targets: | [open\_network\_acl\_ipv4\_rule\_number](#input\_open\_network\_acl\_ipv4\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv4 traffic generated by this module | `number` | `100` | no | | [open\_network\_acl\_ipv6\_rule\_number](#input\_open\_network\_acl\_ipv6\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv6 traffic generated by this module | `number` | `111` | no | | [private\_assign\_ipv6\_address\_on\_creation](#input\_private\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a private subnet will be assigned an IPv6 address | `bool` | `true` | no | -| [private\_dns64\_nat64\_enabled](#input\_private\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_route_table_enabled` to be `true` to be fully operational.
Defaults to `true` unless there is no public IPv4 subnet for egress, in which case it defaults to false. | `bool` | `null` | no | +| [private\_dns64\_nat64\_enabled](#input\_private\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_route_table_enabled` to be `true` to be fully operational.
Defaults to `true` unless there is no public IPv4 subnet for egress, in which case it defaults to `false`. | `bool` | `null` | no | | [private\_label](#input\_private\_label) | The string to use in IDs and elsewhere to distinguish resources for the private subnets from resources for the public subnets | `string` | `"private"` | no | | [private\_open\_network\_acl\_enabled](#input\_private\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every private subnet, and a rule (number 100)
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | | [private\_route\_table\_enabled](#input\_private\_route\_table\_enabled) | If true, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway
will be created for each private subnet (1:1). If false, you will need to create your own route table(s) and route(s). | `bool` | `true` | no | diff --git a/docs/terraform.md b/docs/terraform.md index 31e29c50..0e90304a 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -91,7 +91,7 @@ | [ipv6\_cidrs](#input\_ipv6\_cidrs) | Lists of CIDRs to assign to subnets. Order of CIDRs in the lists must not change over time.
Lists may contain more CIDRs than needed. |
list(object({
private = list(string)
public = list(string)
}))
| `[]` | no | | [ipv6\_egress\_only\_igw\_id](#input\_ipv6\_egress\_only\_igw\_id) | The Egress Only Internet Gateway ID the private IPv6 subnets will route traffic to.
Used if `private_route_table_enabled` is `true` and `ipv6_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | | [ipv6\_enabled](#input\_ipv6\_enabled) | Set true to enable IPv6 addresses in the subnets | `bool` | `false` | no | -| [ipv6\_private\_instance\_hostnames\_enabled](#input\_ipv6\_private\_instance\_hostnames\_enabled) | If `true` (or if `ipv4_enabled` is false), DNS queries for instance hostnames in the private subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | +| [ipv6\_private\_instance\_hostnames\_enabled](#input\_ipv6\_private\_instance\_hostnames\_enabled) | If `true` (or if `ipv4_enabled` is `false`), DNS queries for instance hostnames in the private subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | | [ipv6\_public\_instance\_hostnames\_enabled](#input\_ipv6\_public\_instance\_hostnames\_enabled) | If `true` (or if `ipv4_enabled` is false), DNS queries for instance hostnames in the public subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | | [label\_key\_case](#input\_label\_key\_case) | Controls the letter case of the `tags` keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper`.
Default value: `title`. | `string` | `null` | no | | [label\_order](#input\_label\_order) | The order in which the labels (ID elements) appear in the `id`.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. | `list(string)` | `null` | no | @@ -115,7 +115,7 @@ | [open\_network\_acl\_ipv4\_rule\_number](#input\_open\_network\_acl\_ipv4\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv4 traffic generated by this module | `number` | `100` | no | | [open\_network\_acl\_ipv6\_rule\_number](#input\_open\_network\_acl\_ipv6\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv6 traffic generated by this module | `number` | `111` | no | | [private\_assign\_ipv6\_address\_on\_creation](#input\_private\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a private subnet will be assigned an IPv6 address | `bool` | `true` | no | -| [private\_dns64\_nat64\_enabled](#input\_private\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_route_table_enabled` to be `true` to be fully operational.
Defaults to `true` unless there is no public IPv4 subnet for egress, in which case it defaults to false. | `bool` | `null` | no | +| [private\_dns64\_nat64\_enabled](#input\_private\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_route_table_enabled` to be `true` to be fully operational.
Defaults to `true` unless there is no public IPv4 subnet for egress, in which case it defaults to `false`. | `bool` | `null` | no | | [private\_label](#input\_private\_label) | The string to use in IDs and elsewhere to distinguish resources for the private subnets from resources for the public subnets | `string` | `"private"` | no | | [private\_open\_network\_acl\_enabled](#input\_private\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every private subnet, and a rule (number 100)
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | | [private\_route\_table\_enabled](#input\_private\_route\_table\_enabled) | If true, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway
will be created for each private subnet (1:1). If false, you will need to create your own route table(s) and route(s). | `bool` | `true` | no | diff --git a/variables.tf b/variables.tf index 4b4552fe..d3427460 100644 --- a/variables.tf +++ b/variables.tf @@ -229,7 +229,7 @@ variable "private_dns64_nat64_enabled" { If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway. Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_route_table_enabled` to be `true` to be fully operational. - Defaults to `true` unless there is no public IPv4 subnet for egress, in which case it defaults to false. + Defaults to `true` unless there is no public IPv4 subnet for egress, in which case it defaults to `false`. EOT default = null } @@ -263,7 +263,7 @@ variable "ipv4_private_instance_hostnames_enabled" { variable "ipv6_private_instance_hostnames_enabled" { type = bool description = <<-EOT - If `true` (or if `ipv4_enabled` is false), DNS queries for instance hostnames in the private subnets will be answered with AAAA (IPv6) records. + If `true` (or if `ipv4_enabled` is `false`), DNS queries for instance hostnames in the private subnets will be answered with AAAA (IPv6) records. EOT default = false } From 0e06d923e5b9e6944e8b0432d10b1bb635d0b8fd Mon Sep 17 00:00:00 2001 From: Nuru Date: Sat, 14 May 2022 17:17:54 -0700 Subject: [PATCH 12/12] Address reviewer notes --- README.md | 20 ++++++++++---------- docs/terraform.md | 20 ++++++++++---------- examples/existing-ips/outputs.tf | 2 +- outputs.tf | 6 +++--- variables.tf | 18 +++++++++--------- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index f6c8c3c4..23a99992 100644 --- a/README.md +++ b/README.md @@ -341,7 +341,7 @@ Available targets: | [igw\_id](#input\_igw\_id) | The Internet Gateway ID that the public subnets will route traffic to.
Used if `public_route_table_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | | [ipv4\_cidr\_block](#input\_ipv4\_cidr\_block) | Base IPv4 CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`). Ignored if `ipv4_cidrs` is set.
If no CIDR block is provided, the VPC's default IPv4 CIDR block will be used. | `list(string)` | `[]` | no | | [ipv4\_cidrs](#input\_ipv4\_cidrs) | Lists of CIDRs to assign to subnets. Order of CIDRs in the lists must not change over time.
Lists may contain more CIDRs than needed. |
list(object({
private = list(string)
public = list(string)
}))
| `[]` | no | -| [ipv4\_enabled](#input\_ipv4\_enabled) | Set true to enable IPv4 addresses in the subnets | `bool` | `true` | no | +| [ipv4\_enabled](#input\_ipv4\_enabled) | Set `true` to enable IPv4 addresses in the subnets | `bool` | `true` | no | | [ipv4\_private\_instance\_hostname\_type](#input\_ipv4\_private\_instance\_hostname\_type) | How to generate the DNS name for the instances in the private subnets.
Either `ip-name` to generate it from the IPv4 address, or
`resource-name` to generate it from the instance ID. | `string` | `"ip-name"` | no | | [ipv4\_private\_instance\_hostnames\_enabled](#input\_ipv4\_private\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the private subnets will be answered with A (IPv4) records. | `bool` | `false` | no | | [ipv4\_public\_instance\_hostname\_type](#input\_ipv4\_public\_instance\_hostname\_type) | How to generate the DNS name for the instances in the public subnets.
Either `ip-name` to generate it from the IPv4 address, or
`resource-name` to generate it from the instance ID. | `string` | `"ip-name"` | no | @@ -349,7 +349,7 @@ Available targets: | [ipv6\_cidr\_block](#input\_ipv6\_cidr\_block) | Base IPv6 CIDR block from which `/64` subnet CIDRs will be assigned. Must be `/56`. (e.g. `2600:1f16:c52:ab00::/56`).
Ignored if `ipv6_cidrs` is set. If no CIDR block is provided, the VPC's default IPv6 CIDR block will be used. | `list(string)` | `[]` | no | | [ipv6\_cidrs](#input\_ipv6\_cidrs) | Lists of CIDRs to assign to subnets. Order of CIDRs in the lists must not change over time.
Lists may contain more CIDRs than needed. |
list(object({
private = list(string)
public = list(string)
}))
| `[]` | no | | [ipv6\_egress\_only\_igw\_id](#input\_ipv6\_egress\_only\_igw\_id) | The Egress Only Internet Gateway ID the private IPv6 subnets will route traffic to.
Used if `private_route_table_enabled` is `true` and `ipv6_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | -| [ipv6\_enabled](#input\_ipv6\_enabled) | Set true to enable IPv6 addresses in the subnets | `bool` | `false` | no | +| [ipv6\_enabled](#input\_ipv6\_enabled) | Set `true` to enable IPv6 addresses in the subnets | `bool` | `false` | no | | [ipv6\_private\_instance\_hostnames\_enabled](#input\_ipv6\_private\_instance\_hostnames\_enabled) | If `true` (or if `ipv4_enabled` is `false`), DNS queries for instance hostnames in the private subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | | [ipv6\_public\_instance\_hostnames\_enabled](#input\_ipv6\_public\_instance\_hostnames\_enabled) | If `true` (or if `ipv4_enabled` is false), DNS queries for instance hostnames in the public subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | | [label\_key\_case](#input\_label\_key\_case) | Controls the letter case of the `tags` keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper`.
Default value: `title`. | `string` | `null` | no | @@ -375,15 +375,15 @@ Available targets: | [open\_network\_acl\_ipv6\_rule\_number](#input\_open\_network\_acl\_ipv6\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv6 traffic generated by this module | `number` | `111` | no | | [private\_assign\_ipv6\_address\_on\_creation](#input\_private\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a private subnet will be assigned an IPv6 address | `bool` | `true` | no | | [private\_dns64\_nat64\_enabled](#input\_private\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_route_table_enabled` to be `true` to be fully operational.
Defaults to `true` unless there is no public IPv4 subnet for egress, in which case it defaults to `false`. | `bool` | `null` | no | -| [private\_label](#input\_private\_label) | The string to use in IDs and elsewhere to distinguish resources for the private subnets from resources for the public subnets | `string` | `"private"` | no | -| [private\_open\_network\_acl\_enabled](#input\_private\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every private subnet, and a rule (number 100)
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | -| [private\_route\_table\_enabled](#input\_private\_route\_table\_enabled) | If true, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway
will be created for each private subnet (1:1). If false, you will need to create your own route table(s) and route(s). | `bool` | `true` | no | +| [private\_label](#input\_private\_label) | The string to use in IDs and elsewhere to identify resources for the private subnets and distinguish them from resources for the public subnets | `string` | `"private"` | no | +| [private\_open\_network\_acl\_enabled](#input\_private\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every private subnet, and a rule (number 100)
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will need to manage the network ACL outside of this module. | `bool` | `true` | no | +| [private\_route\_table\_enabled](#input\_private\_route\_table\_enabled) | If `true`, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway
will be created for each private subnet (1:1). If false, you will need to create your own route table(s) and route(s). | `bool` | `true` | no | | [private\_subnets\_additional\_tags](#input\_private\_subnets\_additional\_tags) | Additional tags to be added to private subnets | `map(string)` | `{}` | no | | [private\_subnets\_enabled](#input\_private\_subnets\_enabled) | If false, do not create private subnets (or NAT gateways or instances) | `bool` | `true` | no | | [public\_assign\_ipv6\_address\_on\_creation](#input\_public\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a public subnet will be assigned an IPv6 address | `bool` | `true` | no | | [public\_dns64\_nat64\_enabled](#input\_public\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in public subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `nat_gateway_enabled` and `public_route_table_enabled` to be `true` to be fully operational. | `bool` | `false` | no | -| [public\_label](#input\_public\_label) | The string to use in IDs and elsewhere to distinguish resources for the private subnets from resources for the public subnets | `string` | `"public"` | no | -| [public\_open\_network\_acl\_enabled](#input\_public\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every public subnet, and a rule
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | +| [public\_label](#input\_public\_label) | The string to use in IDs and elsewhere to identify resources for the public subnets and distinguish them from resources for the private subnets | `string` | `"public"` | no | +| [public\_open\_network\_acl\_enabled](#input\_public\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every public subnet, and a rule
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will need to manage the network ACL outside of this module. | `bool` | `true` | no | | [public\_route\_table\_enabled](#input\_public\_route\_table\_enabled) | If `true`, network route table(s) will be created as determined by `public_route_table_per_subnet_enabled` and
appropriate routes will be added to destinations this module knows about.
If `false`, you will need to create your own route table(s) and route(s).
Ignored if `public_route_table_ids` is non-empty. | `bool` | `true` | no | | [public\_route\_table\_ids](#input\_public\_route\_table\_ids) | List optionally containing the ID of a single route table shared by all public subnets
or exactly one route table ID for each public subnet.
If provided, it overrides `public_route_table_per_subnet_enabled`.
If omitted and `public_route_table_enabled` is `true`,
one or more network route tables will be created for the public subnets,
according to the setting of `public_route_table_per_subnet_enabled`. | `list(string)` | `[]` | no | | [public\_route\_table\_per\_subnet\_enabled](#input\_public\_route\_table\_per\_subnet\_enabled) | If `true` (and `public_route_table_enabled` is `true), a separate network route table will be created for and associated with each public subnet.
If `false` (and `public\_route\_table\_enabled` is `true), a single network route table will be created and it will be associated with every public subnet.
If not set, it will be set to the value of `public_dns64_nat64_enabled`. | `bool` | `null` | no | @@ -413,13 +413,13 @@ Available targets: | [nat\_gateway\_public\_ips](#output\_nat\_gateway\_public\_ips) | DEPRECATED: use `nat_ips` instead. Public IPv4 IP addresses in use by NAT. | | [nat\_instance\_ami\_id](#output\_nat\_instance\_ami\_id) | ID of AMI used by NAT instance | | [nat\_instance\_ids](#output\_nat\_instance\_ids) | IDs of the NAT Instances created | -| [nat\_ips](#output\_nat\_ips) | IP Addresses in use by NAT | -| [private\_network\_acl](#output\_private\_network\_acl) | ID of the Network ACL created for private subnets | +| [nat\_ips](#output\_nat\_ips) | Elastic IP Addresses in use by NAT | +| [private\_network\_acl\_id](#output\_private\_network\_acl\_id) | ID of the Network ACL created for private subnets | | [private\_route\_table\_ids](#output\_private\_route\_table\_ids) | IDs of the created private route tables | | [private\_subnet\_cidrs](#output\_private\_subnet\_cidrs) | IPv4 CIDR blocks of the created private subnets | | [private\_subnet\_ids](#output\_private\_subnet\_ids) | IDs of the created private subnets | | [private\_subnet\_ipv6\_cidrs](#output\_private\_subnet\_ipv6\_cidrs) | IPv6 CIDR blocks of the created private subnets | -| [public\_network\_acl](#output\_public\_network\_acl) | ID of the Network ACL created for public subnets | +| [public\_network\_acl\_id](#output\_public\_network\_acl\_id) | ID of the Network ACL created for public subnets | | [public\_route\_table\_ids](#output\_public\_route\_table\_ids) | IDs of the created public route tables | | [public\_subnet\_cidrs](#output\_public\_subnet\_cidrs) | IPv4 CIDR blocks of the created public subnets | | [public\_subnet\_ids](#output\_public\_subnet\_ids) | IDs of the created public subnets | diff --git a/docs/terraform.md b/docs/terraform.md index 0e90304a..91f413c5 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -82,7 +82,7 @@ | [igw\_id](#input\_igw\_id) | The Internet Gateway ID that the public subnets will route traffic to.
Used if `public_route_table_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | | [ipv4\_cidr\_block](#input\_ipv4\_cidr\_block) | Base IPv4 CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`). Ignored if `ipv4_cidrs` is set.
If no CIDR block is provided, the VPC's default IPv4 CIDR block will be used. | `list(string)` | `[]` | no | | [ipv4\_cidrs](#input\_ipv4\_cidrs) | Lists of CIDRs to assign to subnets. Order of CIDRs in the lists must not change over time.
Lists may contain more CIDRs than needed. |
list(object({
private = list(string)
public = list(string)
}))
| `[]` | no | -| [ipv4\_enabled](#input\_ipv4\_enabled) | Set true to enable IPv4 addresses in the subnets | `bool` | `true` | no | +| [ipv4\_enabled](#input\_ipv4\_enabled) | Set `true` to enable IPv4 addresses in the subnets | `bool` | `true` | no | | [ipv4\_private\_instance\_hostname\_type](#input\_ipv4\_private\_instance\_hostname\_type) | How to generate the DNS name for the instances in the private subnets.
Either `ip-name` to generate it from the IPv4 address, or
`resource-name` to generate it from the instance ID. | `string` | `"ip-name"` | no | | [ipv4\_private\_instance\_hostnames\_enabled](#input\_ipv4\_private\_instance\_hostnames\_enabled) | If `true`, DNS queries for instance hostnames in the private subnets will be answered with A (IPv4) records. | `bool` | `false` | no | | [ipv4\_public\_instance\_hostname\_type](#input\_ipv4\_public\_instance\_hostname\_type) | How to generate the DNS name for the instances in the public subnets.
Either `ip-name` to generate it from the IPv4 address, or
`resource-name` to generate it from the instance ID. | `string` | `"ip-name"` | no | @@ -90,7 +90,7 @@ | [ipv6\_cidr\_block](#input\_ipv6\_cidr\_block) | Base IPv6 CIDR block from which `/64` subnet CIDRs will be assigned. Must be `/56`. (e.g. `2600:1f16:c52:ab00::/56`).
Ignored if `ipv6_cidrs` is set. If no CIDR block is provided, the VPC's default IPv6 CIDR block will be used. | `list(string)` | `[]` | no | | [ipv6\_cidrs](#input\_ipv6\_cidrs) | Lists of CIDRs to assign to subnets. Order of CIDRs in the lists must not change over time.
Lists may contain more CIDRs than needed. |
list(object({
private = list(string)
public = list(string)
}))
| `[]` | no | | [ipv6\_egress\_only\_igw\_id](#input\_ipv6\_egress\_only\_igw\_id) | The Egress Only Internet Gateway ID the private IPv6 subnets will route traffic to.
Used if `private_route_table_enabled` is `true` and `ipv6_enabled` is `true`, ignored otherwise. | `list(string)` | `[]` | no | -| [ipv6\_enabled](#input\_ipv6\_enabled) | Set true to enable IPv6 addresses in the subnets | `bool` | `false` | no | +| [ipv6\_enabled](#input\_ipv6\_enabled) | Set `true` to enable IPv6 addresses in the subnets | `bool` | `false` | no | | [ipv6\_private\_instance\_hostnames\_enabled](#input\_ipv6\_private\_instance\_hostnames\_enabled) | If `true` (or if `ipv4_enabled` is `false`), DNS queries for instance hostnames in the private subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | | [ipv6\_public\_instance\_hostnames\_enabled](#input\_ipv6\_public\_instance\_hostnames\_enabled) | If `true` (or if `ipv4_enabled` is false), DNS queries for instance hostnames in the public subnets will be answered with AAAA (IPv6) records. | `bool` | `false` | no | | [label\_key\_case](#input\_label\_key\_case) | Controls the letter case of the `tags` keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper`.
Default value: `title`. | `string` | `null` | no | @@ -116,15 +116,15 @@ | [open\_network\_acl\_ipv6\_rule\_number](#input\_open\_network\_acl\_ipv6\_rule\_number) | The `rule_no` assigned to the network ACL rules for IPv6 traffic generated by this module | `number` | `111` | no | | [private\_assign\_ipv6\_address\_on\_creation](#input\_private\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a private subnet will be assigned an IPv6 address | `bool` | `true` | no | | [private\_dns64\_nat64\_enabled](#input\_private\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in private subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `public_subnets_enabled`, `nat_gateway_enabled`, and `private_route_table_enabled` to be `true` to be fully operational.
Defaults to `true` unless there is no public IPv4 subnet for egress, in which case it defaults to `false`. | `bool` | `null` | no | -| [private\_label](#input\_private\_label) | The string to use in IDs and elsewhere to distinguish resources for the private subnets from resources for the public subnets | `string` | `"private"` | no | -| [private\_open\_network\_acl\_enabled](#input\_private\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every private subnet, and a rule (number 100)
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | -| [private\_route\_table\_enabled](#input\_private\_route\_table\_enabled) | If true, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway
will be created for each private subnet (1:1). If false, you will need to create your own route table(s) and route(s). | `bool` | `true` | no | +| [private\_label](#input\_private\_label) | The string to use in IDs and elsewhere to identify resources for the private subnets and distinguish them from resources for the public subnets | `string` | `"private"` | no | +| [private\_open\_network\_acl\_enabled](#input\_private\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every private subnet, and a rule (number 100)
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will need to manage the network ACL outside of this module. | `bool` | `true` | no | +| [private\_route\_table\_enabled](#input\_private\_route\_table\_enabled) | If `true`, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway
will be created for each private subnet (1:1). If false, you will need to create your own route table(s) and route(s). | `bool` | `true` | no | | [private\_subnets\_additional\_tags](#input\_private\_subnets\_additional\_tags) | Additional tags to be added to private subnets | `map(string)` | `{}` | no | | [private\_subnets\_enabled](#input\_private\_subnets\_enabled) | If false, do not create private subnets (or NAT gateways or instances) | `bool` | `true` | no | | [public\_assign\_ipv6\_address\_on\_creation](#input\_public\_assign\_ipv6\_address\_on\_creation) | If `true`, network interfaces created in a public subnet will be assigned an IPv6 address | `bool` | `true` | no | | [public\_dns64\_nat64\_enabled](#input\_public\_dns64\_nat64\_enabled) | If `true` and IPv6 is enabled, DNS queries made to the Amazon-provided DNS Resolver in public subnets will return synthetic
IPv6 addresses for IPv4-only destinations, and these addresses will be routed to the NAT Gateway.
Requires `nat_gateway_enabled` and `public_route_table_enabled` to be `true` to be fully operational. | `bool` | `false` | no | -| [public\_label](#input\_public\_label) | The string to use in IDs and elsewhere to distinguish resources for the private subnets from resources for the public subnets | `string` | `"public"` | no | -| [public\_open\_network\_acl\_enabled](#input\_public\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every public subnet, and a rule
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will will need to manage the network ACL external to this module. | `bool` | `true` | no | +| [public\_label](#input\_public\_label) | The string to use in IDs and elsewhere to identify resources for the public subnets and distinguish them from resources for the private subnets | `string` | `"public"` | no | +| [public\_open\_network\_acl\_enabled](#input\_public\_open\_network\_acl\_enabled) | If `true`, a single network ACL be created and it will be associated with every public subnet, and a rule
will be created allowing all ingress and all egress. You can add additional rules to this network ACL
using the `aws_network_acl_rule` resource.
If `false`, you will need to manage the network ACL outside of this module. | `bool` | `true` | no | | [public\_route\_table\_enabled](#input\_public\_route\_table\_enabled) | If `true`, network route table(s) will be created as determined by `public_route_table_per_subnet_enabled` and
appropriate routes will be added to destinations this module knows about.
If `false`, you will need to create your own route table(s) and route(s).
Ignored if `public_route_table_ids` is non-empty. | `bool` | `true` | no | | [public\_route\_table\_ids](#input\_public\_route\_table\_ids) | List optionally containing the ID of a single route table shared by all public subnets
or exactly one route table ID for each public subnet.
If provided, it overrides `public_route_table_per_subnet_enabled`.
If omitted and `public_route_table_enabled` is `true`,
one or more network route tables will be created for the public subnets,
according to the setting of `public_route_table_per_subnet_enabled`. | `list(string)` | `[]` | no | | [public\_route\_table\_per\_subnet\_enabled](#input\_public\_route\_table\_per\_subnet\_enabled) | If `true` (and `public_route_table_enabled` is `true), a separate network route table will be created for and associated with each public subnet.
If `false` (and `public\_route\_table\_enabled` is `true), a single network route table will be created and it will be associated with every public subnet.
If not set, it will be set to the value of `public_dns64_nat64_enabled`. | `bool` | `null` | no | @@ -154,13 +154,13 @@ | [nat\_gateway\_public\_ips](#output\_nat\_gateway\_public\_ips) | DEPRECATED: use `nat_ips` instead. Public IPv4 IP addresses in use by NAT. | | [nat\_instance\_ami\_id](#output\_nat\_instance\_ami\_id) | ID of AMI used by NAT instance | | [nat\_instance\_ids](#output\_nat\_instance\_ids) | IDs of the NAT Instances created | -| [nat\_ips](#output\_nat\_ips) | IP Addresses in use by NAT | -| [private\_network\_acl](#output\_private\_network\_acl) | ID of the Network ACL created for private subnets | +| [nat\_ips](#output\_nat\_ips) | Elastic IP Addresses in use by NAT | +| [private\_network\_acl\_id](#output\_private\_network\_acl\_id) | ID of the Network ACL created for private subnets | | [private\_route\_table\_ids](#output\_private\_route\_table\_ids) | IDs of the created private route tables | | [private\_subnet\_cidrs](#output\_private\_subnet\_cidrs) | IPv4 CIDR blocks of the created private subnets | | [private\_subnet\_ids](#output\_private\_subnet\_ids) | IDs of the created private subnets | | [private\_subnet\_ipv6\_cidrs](#output\_private\_subnet\_ipv6\_cidrs) | IPv6 CIDR blocks of the created private subnets | -| [public\_network\_acl](#output\_public\_network\_acl) | ID of the Network ACL created for public subnets | +| [public\_network\_acl\_id](#output\_public\_network\_acl\_id) | ID of the Network ACL created for public subnets | | [public\_route\_table\_ids](#output\_public\_route\_table\_ids) | IDs of the created public route tables | | [public\_subnet\_cidrs](#output\_public\_subnet\_cidrs) | IPv4 CIDR blocks of the created public subnets | | [public\_subnet\_ids](#output\_public\_subnet\_ids) | IDs of the created public subnets | diff --git a/examples/existing-ips/outputs.tf b/examples/existing-ips/outputs.tf index 3a77e208..5b4aae9c 100644 --- a/examples/existing-ips/outputs.tf +++ b/examples/existing-ips/outputs.tf @@ -1,5 +1,5 @@ output "existing_ips" { - description = "IP Addresses created by this module for use by NAT" + description = "Elastic IP Addresses created by this module for use by NAT" value = aws_eip.nat_ips.*.public_ip } diff --git a/outputs.tf b/outputs.tf index bde6740e..f864626d 100644 --- a/outputs.tf +++ b/outputs.tf @@ -53,12 +53,12 @@ output "private_route_table_ids" { value = aws_route_table.private.*.id } -output "public_network_acl" { +output "public_network_acl_id" { description = "ID of the Network ACL created for public subnets" value = local.public_open_network_acl_enabled ? aws_network_acl.public[0].id : null } -output "private_network_acl" { +output "private_network_acl_id" { description = "ID of the Network ACL created for private subnets" value = local.private_open_network_acl_enabled ? aws_network_acl.private[0].id : null } @@ -79,7 +79,7 @@ output "nat_instance_ami_id" { } output "nat_ips" { - description = "IP Addresses in use by NAT" + description = "Elastic IP Addresses in use by NAT" value = local.need_nat_eip_data ? var.nat_elastic_ips : aws_eip.default.*.public_ip } diff --git a/variables.tf b/variables.tf index d3427460..56148e4c 100644 --- a/variables.tf +++ b/variables.tf @@ -46,7 +46,8 @@ variable "max_subnet_count" { variable "max_nats" { type = number description = "Maximum number of NAT Gateways or NAT instances to create" - default = 999 + # Default should be MAX_INT, but Terraform does not provide that. 999 is big enough. + default = 999 } variable "private_subnets_enabled" { @@ -68,26 +69,26 @@ variable "public_subnets_enabled" { variable "private_label" { type = string - description = "The string to use in IDs and elsewhere to distinguish resources for the private subnets from resources for the public subnets" + description = "The string to use in IDs and elsewhere to identify resources for the private subnets and distinguish them from resources for the public subnets" default = "private" } variable "public_label" { type = string - description = "The string to use in IDs and elsewhere to distinguish resources for the private subnets from resources for the public subnets" + description = "The string to use in IDs and elsewhere to identify resources for the public subnets and distinguish them from resources for the private subnets" default = "public" } variable "ipv4_enabled" { type = bool - description = "Set true to enable IPv4 addresses in the subnets" + description = "Set `true` to enable IPv4 addresses in the subnets" default = true } variable "ipv6_enabled" { type = bool - description = "Set true to enable IPv6 addresses in the subnets" + description = "Set `true` to enable IPv6 addresses in the subnets" default = false } @@ -298,7 +299,7 @@ variable "private_open_network_acl_enabled" { If `true`, a single network ACL be created and it will be associated with every private subnet, and a rule (number 100) will be created allowing all ingress and all egress. You can add additional rules to this network ACL using the `aws_network_acl_rule` resource. - If `false`, you will will need to manage the network ACL external to this module. + If `false`, you will need to manage the network ACL outside of this module. EOT default = true } @@ -309,7 +310,7 @@ variable "public_open_network_acl_enabled" { If `true`, a single network ACL be created and it will be associated with every public subnet, and a rule will be created allowing all ingress and all egress. You can add additional rules to this network ACL using the `aws_network_acl_rule` resource. - If `false`, you will will need to manage the network ACL external to this module. + If `false`, you will need to manage the network ACL outside of this module. EOT default = true } @@ -329,7 +330,7 @@ variable "open_network_acl_ipv6_rule_number" { variable "private_route_table_enabled" { type = bool description = <<-EOT - If true, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway + If `true`, a network route table and default route to the NAT gateway, NAT instance, or egress-only gateway will be created for each private subnet (1:1). If false, you will need to create your own route table(s) and route(s). EOT default = true @@ -429,7 +430,6 @@ variable "nat_instance_ami_id" { condition = length(var.nat_instance_ami_id) < 2 error_message = "Only 1 NAT Instance AMI ID can be provided." } - } variable "nat_instance_cpu_credits_override" {