|
1 | | -# Create a single subnet for the NAT Gateway to live in |
2 | | -# routed to the outside world |
| 1 | +# Create subnets for use by the LoadBalancer for ingress |
| 2 | +# And use the first of these subnets for the NAT Gateway |
| 3 | + |
| 4 | +data "aws_availability_zones" "available" { |
| 5 | + exclude_names = var.exclude_availability_zones |
| 6 | +} |
3 | 7 |
|
4 | 8 | data "aws_vpc" "current" { |
5 | 9 | id = var.vpc_id |
6 | 10 | } |
7 | 11 |
|
8 | | -# Create a subnet in us-east-1a in the |
9 | | -# CIDR block specified by the inputs |
10 | | -# So that the CIDR block is different than |
11 | | -# others in this VPC |
12 | 12 | resource "aws_subnet" "mod" { |
13 | | - count = var.uses_nat_gateway ? 1 : 0 |
14 | | - availability_zone = var.availability_zone |
| 13 | + count = var.uses_nat_gateway ? length(data.aws_availability_zones.available.names) : 0 |
| 14 | + availability_zone = element(data.aws_availability_zones.available.names, count.index) |
15 | 15 | cidr_block = cidrsubnet( |
16 | 16 | data.aws_vpc.current.cidr_block, |
17 | 17 | var.subnet_cidr_newbits, |
18 | | - var.subnet_cidr_netnum_offset + 1, |
| 18 | + var.subnet_cidr_netnum_offset + count.index + 1, |
19 | 19 | ) |
20 | 20 | map_public_ip_on_launch = true |
21 | 21 | tags = var.tags |
22 | 22 | vpc_id = var.vpc_id |
23 | 23 | } |
24 | 24 |
|
25 | | - |
26 | 25 | # ElasticIP address for use with the NAT Gateway |
27 | 26 | resource "aws_eip" "nat-gw-eip" { |
28 | 27 | count = var.uses_nat_gateway ? 1 : 0 |
29 | 28 | vpc = true |
30 | 29 | tags = var.tags |
31 | 30 | } |
32 | 31 |
|
33 | | -# NAT Gateway in the first (only) subnet |
| 32 | +# NAT Gateway in the first subnet |
34 | 33 | resource "aws_nat_gateway" "gw" { |
35 | 34 | count = var.uses_nat_gateway ? 1 : 0 |
36 | 35 | allocation_id = aws_eip.nat-gw-eip[0].id |
@@ -59,7 +58,7 @@ resource "aws_route" "mod" { |
59 | 58 | } |
60 | 59 |
|
61 | 60 | resource "aws_route_table_association" "mod" { |
62 | | - count = var.uses_nat_gateway ? 1 : 0 |
| 61 | + count = var.uses_nat_gateway ? length(data.aws_availability_zones.available.names) : 0 |
63 | 62 | route_table_id = aws_route_table.mod[0].id |
64 | | - subnet_id = aws_subnet.mod[0].id |
| 63 | + subnet_id = element(aws_subnet.mod[*].id, count.index) |
65 | 64 | } |
0 commit comments