diff --git a/README.md b/README.md index 5fdcd6e..3863096 100644 --- a/README.md +++ b/README.md @@ -372,9 +372,10 @@ Please see our [developer documentation](https://github.com/aws-ia/terraform-aws | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| -| [az\_count](#input\_az\_count) | Searches region for # of AZs to use and takes a slice based on count. Assume slice is sorted a-z. | `number` | n/a | yes | | [name](#input\_name) | Name to give VPC. Note: does not effect subnet names, which get assigned name based on name\_prefix. | `string` | n/a | yes | | [subnets](#input\_subnets) | Configuration of subnets to build in VPC. 1 Subnet per AZ is created. Subnet types are defined as maps with the available keys: "private", "public", "transit\_gateway", "core\_network". Each Subnet type offers its own set of available arguments detailed below.

**Attributes shared across subnet types:**
- `cidrs` = (Optional\|list(string)) **Cannot set if `netmask` is set.** List of IPv4 CIDRs to set to subnets. Count of CIDRs defined must match quantity of azs in `az_count`.
- `netmask` = (Optional\|Int) **Cannot set if `cidrs` is set.** Netmask of the `var.cidr_block` to calculate for each subnet.
- `assign_ipv6_cidr` = (Optional\|bool) **Cannot set if `ipv6_cidrs` is set.** If true, it will calculate a /64 block from the IPv6 VPC CIDR to set in the subnets.
- `ipv6_cidrs` = (Optional\|list(string)) **Cannot set if `assign_ipv6_cidr` is set.** List of IPv6 CIDRs to set to subnets. The subnet size must use a /64 prefix length. Count of CIDRs defined must match quantity of azs in `az_count`.
- `name_prefix` = (Optional\|String) A string prefix to use for the name of your subnet and associated resources. Subnet type key name is used if omitted (aka private, public, transit\_gateway). Example `name_prefix = "private"` for `var.subnets.private` is redundant.
- `tags` = (Optional\|map(string)) Tags to set on the subnet and associated resources.

**Any private subnet type options:**
- All shared keys above
- `connect_to_public_natgw` = (Optional\|bool) Determines if routes to NAT Gateways should be created. Must also set `var.subnets.public.nat_gateway_configuration` in public subnets.
- `ipv6_native` = (Optional\|bool) Indicates whether to create an IPv6-ony subnet. Either `var.assign_ipv6_cidr` or `var.ipv6_cidrs` should be defined to allocate an IPv6 CIDR block.
- `connect_to_eigw` = (Optional\|bool) Determines if routes to the Egress-only Internet gateway should be created. Must also set `var.vpc_egress_only_internet_gateway`.

**public subnet type options:**
- All shared keys above
- `nat_gateway_configuration` = (Optional\|string) Determines if NAT Gateways should be created and in how many AZs. Valid values = `"none"`, `"single_az"`, `"all_azs"`. Default = "none". Must also set `var.subnets.private.connect_to_public_natgw = true`.
- `connect_to_igw` = (Optional\|bool) Determines if the default route (0.0.0.0/0 or ::/0) is created in the public subnets with destination the Internet gateway. Defaults to `true`.
- `ipv6_native` = (Optional\|bool) Indicates whether to create an IPv6-ony subnet. Either `var.assign_ipv6_cidr` or `var.ipv6_cidrs` should be defined to allocate an IPv6 CIDR block.

**transit\_gateway subnet type options:**
- All shared keys above
- `connect_to_public_natgw` = (Optional\|string) Determines if routes to NAT Gateways should be created. Specify the CIDR range or a prefix-list-id that you want routed to nat gateway. Usually `0.0.0.0/0`. Must also set `var.subnets.public.nat_gateway_configuration`.
- `transit_gateway_default_route_table_association` = (Optional\|bool) Boolean whether the VPC Attachment should be associated with the EC2 Transit Gateway association default route table. This cannot be configured or perform drift detection with Resource Access Manager shared EC2 Transit Gateways.
- `transit_gateway_default_route_table_propagation` = (Optional\|bool) Boolean whether the VPC Attachment should propagate routes with the EC2 Transit Gateway propagation default route table. This cannot be configured or perform drift detection with Resource Access Manager shared EC2 Transit Gateways.
- `transit_gateway_appliance_mode_support` = (Optional\|string) Whether Appliance Mode is enabled. If enabled, a traffic flow between a source and a destination uses the same Availability Zone for the VPC attachment for the lifetime of that flow. Valid values: `disable` (default) and `enable`.
- `transit_gateway_dns_support` = (Optional\|string) DNS Support is used if you need the VPC to resolve public IPv4 DNS host names to private IPv4 addresses when queried from instances in another VPC attached to the transit gateway. Valid values: `enable` (default) and `disable`.

**core\_network subnet type options:**
- All shared keys abovce
- `connect_to_public_natgw` = (Optional\|string) Determines if routes to NAT Gateways should be created. Specify the CIDR range or a prefix-list-id that you want routed to nat gateway. Usually `0.0.0.0/0`. Must also set `var.subnets.public.nat_gateway_configuration`.
- `appliance_mode_support` = (Optional\|bool) Indicates whether appliance mode is supported. If enabled, traffic flow between a source and destination use the same Availability Zone for the VPC attachment for the lifetime of that flow. Defaults to `false`.
- `require_acceptance` = (Optional\|bool) Boolean whether the core network VPC attachment to create requires acceptance or not. Defaults to `false`.
- `accept_attachment` = (Optional\|bool) Boolean whether the core network VPC attachment is accepted or not in the segment. Only valid if `require_acceptance` is set to `true`. Defaults to `true`.

Example:
subnets = {
# Dual-stack subnet
public = {
netmask = 24
assign_ipv6_cidr = true
nat_gateway_configuration = "single_az"
}
# IPv4 only subnet
private = {
netmask = 24
connect_to_public_natgw = true
}
# IPv6 only subnet
ipv6 = {
ipv6_native = true
assign_ipv6_cidr = true
connect_to_eigw = true
}
# Transit gateway subnets (dual-stack)
transit_gateway = {
netmask = 24
assign_ipv6_cidr = true
connect_to_public_natgw = true
transit_gateway_default_route_table_association = true
transit_gateway_default_route_table_propagation = true
}
# Core Network subnets (dual-stack)
core_network = {
netmask = 24
assign_ipv6_cidr = true
connect_to_public_natgw = true
appliance_mode_support = true
require_acceptance = true
accept_attachment = true
}
}
| `any` | n/a | yes | +| [az\_count](#input\_az\_count) | Searches region for # of AZs to use and takes a slice based on count. Assume slice is sorted a-z. Required if `azs` is not provided. | `number` | `null` | no | +| [azs](#input\_azs) | (Optional) A list of AZs to use. e.g. `azs = ["us-east-1a","us-east-1c"]` Incompatible with `az_count` | `list(string)` | `null` | no | | [cidr\_block](#input\_cidr\_block) | IPv4 CIDR range to assign to VPC if creating VPC or to associate as a secondary IPv6 CIDR. Overridden by var.vpc\_id output from data.aws\_vpc. | `string` | `null` | no | | [core\_network](#input\_core\_network) | AWS Cloud WAN's core network information - to create a VPC attachment. Required when `cloud_wan` subnet is defined. Two attributes are required: the `id` and `arn` of the resource. |
object({
id = string
arn = string
})
|
{
"arn": null,
"id": null
}
| no | | [core\_network\_ipv6\_routes](#input\_core\_network\_ipv6\_routes) | Configuration of IPv6 route(s) to AWS Cloud WAN's core network.
For each `public` and/or `private` subnets named in the `subnets` variable, optionally create routes from the subnet to the core network.
You can specify either a CIDR range or a prefix-list-id that you want routed to the core network.
Example:
core_network_ivp6_routes = {
public = "::/0"
private = "pl-123"
}
| `any` | `{}` | no | diff --git a/data.tf b/data.tf index 7cf5efe..80bbacb 100644 --- a/data.tf +++ b/data.tf @@ -1,5 +1,6 @@ locals { - azs = slice(data.aws_availability_zones.current.names, 0, var.az_count) + # az_count has been provided slice based on az_count otherwise return those provided + azs = var.az_count != null ? slice(data.aws_availability_zones.current.names, 0, var.az_count) : data.aws_availability_zones.current.names # references to module.calculate_subnets output calculated_subnets = module.calculate_subnets.subnets_by_type @@ -123,6 +124,34 @@ data "aws_availability_zones" "current" { name = "opt-in-status" values = ["opt-in-not-required"] } + + dynamic "filter" { + for_each = var.azs != null ? [1] : [] + content { + name = "zone-name" + values = var.azs + } + } + + lifecycle { + # Check at least one variable `var.azs` or `var.az_count` has been provided + precondition { + condition = (var.azs != null && var.az_count != null) ? false : true + error_message = "Both `var.azs` and `var.az_count` provided. You must provide a value for `var.azs` or `var.az_count` but not both." + } + + # Check that only `var.azs` or `var.az_count` was provided (and not both) + precondition { + condition = (var.azs == null && var.az_count == null) ? false : true + error_message = "You must provide a value for the variable `var.az_count` or `var.azs`." + } + + # Check if `var.azs` was provided that the filter returns the correct number of AZs (i.e. all exist in the current partition/region) + postcondition { + condition = (var.azs == null) ? true : (length(self.names) == length(var.azs) ? true : false) + error_message = "One or more of the Availability Zones provided in `var.azs` does not exist. Please check the availability zones are available in the current region." + } + } } # search for existing vpc with var.vpc_id if not creating diff --git a/examples/provide_specific_azs/.header.md b/examples/provide_specific_azs/.header.md new file mode 100644 index 0000000..884674e --- /dev/null +++ b/examples/provide_specific_azs/.header.md @@ -0,0 +1,6 @@ +# Deploy VPC subnets to a specific set of Availability Zones + +This example shows how you can use this module to deploy a VPC defining a list of specific Availability Zones where subnets should be deployed. This example creates the following: + +* VPC with IPv4 CIDR block named "specific-azs" +* Private subnets deployed in each of the Availability Zones defined in the input variable `azs` with the name prefix "vpc-specific-azs-private" and a /18 netmask diff --git a/examples/provide_specific_azs/README.md b/examples/provide_specific_azs/README.md new file mode 100644 index 0000000..acb3cce --- /dev/null +++ b/examples/provide_specific_azs/README.md @@ -0,0 +1,37 @@ + +# Deploy VPC subnets to a specific set of Availability Zones + +This example shows how you can use this module to deploy a VPC defining a list of specific Availability Zones where subnets should be deployed. This example creates the following: + +* VPC with IPv4 CIDR block named "specific-azs" +* Private subnets deployed in each of the Availability Zones defined in the input variable `azs` with the name prefix "vpc-specific-azs-private" and a /18 netmask + +## Requirements + +No requirements. + +## Providers + +No providers. + +## Modules + +| Name | Source | Version | +|------|--------|---------| +| [vpc\_specific\_azs](#module\_vpc\_specific\_azs) | ../.. | n/a | + +## Resources + +No resources. + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [azs](#input\_azs) | A list of AZs to use. e.g. `azs = ["us-east-1a","us-east-1c"]` Incompatible with `az_count` | `list(string)` |
[
"us-east-1a",
"us-east-1c"
]
| no | +| [cidr\_block](#input\_cidr\_block) | IPv4 CIDR range to assign to VPC if creating VPC or to associate as a secondary IPv6 CIDR. Overridden by var.vpc\_id output from data.aws\_vpc. | `string` | `"10.0.0.0/16"` | no | + +## Outputs + +No outputs. + \ No newline at end of file diff --git a/examples/provide_specific_azs/main.tf b/examples/provide_specific_azs/main.tf new file mode 100644 index 0000000..cb7aa6d --- /dev/null +++ b/examples/provide_specific_azs/main.tf @@ -0,0 +1,17 @@ +module "vpc_specific_azs" { + # source = "aws-ia/vpc/aws" + # version = ">= 4.3.1" + source = "../.." + + name = "specific-azs" + cidr_block = var.cidr_block + azs = var.azs + + + subnets = { + private = { + name_prefix = "vpc-specific-azs-private" + netmask = 18 + } + } +} diff --git a/examples/provide_specific_azs/outputs.tf b/examples/provide_specific_azs/outputs.tf new file mode 100644 index 0000000..e69de29 diff --git a/examples/provide_specific_azs/variables.tf b/examples/provide_specific_azs/variables.tf new file mode 100644 index 0000000..00cae4e --- /dev/null +++ b/examples/provide_specific_azs/variables.tf @@ -0,0 +1,15 @@ +variable "azs" { + type = list(string) + description = "A list of AZs to use. e.g. `azs = [\"us-east-1a\",\"us-east-1c\"]` Incompatible with `az_count`" + default = [ + "us-east-1a", + "us-east-1c", + ] +} + +variable "cidr_block" { + type = string + description = "IPv4 CIDR range to assign to VPC if creating VPC or to associate as a secondary IPv6 CIDR. Overridden by var.vpc_id output from data.aws_vpc." + default = "10.0.0.0/16" +} + diff --git a/test/examples_provide_specific_azs_test.go b/test/examples_provide_specific_azs_test.go new file mode 100644 index 0000000..7bfc476 --- /dev/null +++ b/test/examples_provide_specific_azs_test.go @@ -0,0 +1,17 @@ +package test + +import ( + "testing" + + "github.com/gruntwork-io/terratest/modules/terraform" +) + +func TestExamplesSpecificAzs(t *testing.T) { + + terraformOptions := &terraform.Options{ + TerraformDir: "../examples/provide_specific_azs", + } + + defer terraform.Destroy(t, terraformOptions) + terraform.InitAndApply(t, terraformOptions) +} \ No newline at end of file diff --git a/variables.tf b/variables.tf index 95c9936..0d809d1 100644 --- a/variables.tf +++ b/variables.tf @@ -17,7 +17,14 @@ variable "vpc_id" { variable "az_count" { type = number - description = "Searches region for # of AZs to use and takes a slice based on count. Assume slice is sorted a-z." + description = "Searches region for # of AZs to use and takes a slice based on count. Assume slice is sorted a-z. Required if `azs` is not provided." + default = null +} + +variable "azs" { + type = list(string) + description = "(Optional) A list of AZs to use. e.g. `azs = [\"us-east-1a\",\"us-east-1c\"]` Incompatible with `az_count`" + default = null } variable "vpc_enable_dns_hostnames" {