1-
21locals {
3- enable_private_routing = var. create_private_route_table
4- route_to_igw = length (var. public_subnet ) > 0 && var. use_created_igw_for_public_routing
2+ route_to_igw = length (var. public_subnets ) > 0 && var. use_created_igw_for_public_routing
53}
64
75resource "aws_route_table" "private" {
8- count = local . enable_private_routing ? 1 : 0
6+ count = var . create_private_route_table ? 1 : 0
97
108 vpc_id = aws_vpc. this . id
119 tags = merge (var. tags ,
@@ -15,7 +13,7 @@ resource "aws_route_table" "private" {
1513}
1614
1715resource "aws_route_table" "public" {
18- count = length (var. public_subnet ) > 0 ? 1 : 0
16+ count = length (var. public_subnets ) > 0 ? 1 : 0
1917
2018 vpc_id = aws_vpc. this . id
2119 tags = merge (var. tags ,
@@ -25,7 +23,7 @@ resource "aws_route_table" "public" {
2523}
2624
2725resource "aws_route" "private" {
28- for_each = local . enable_private_routing ? var. private_routes : {}
26+ for_each = var . create_private_route_table ? var. private_subnet_routes : {}
2927
3028 route_table_id = aws_route_table. private [0 ]. id
3129 destination_cidr_block = each. value . destination_cidr_block
@@ -39,25 +37,35 @@ resource "aws_route" "private" {
3937 vpc_peering_connection_id = each. value . vpc_peering_connection_id
4038}
4139
40+ resource "aws_route" "public_subnet_default_to_igw" {
41+ count = local. route_to_igw ? 1 : 0
42+
43+ route_table_id = aws_route_table. public [0 ]. id
44+ destination_cidr_block = " 0.0.0.0/0"
45+ gateway_id = aws_internet_gateway. this [0 ]. id
46+ }
47+
4248resource "aws_route" "public" {
43- for_each = length (var. public_subnet ) > 0 ? var. public_routes : {}
49+ for_each = length (var. public_subnets ) > 0 ? var. public_subnet_routes : {}
4450
4551 route_table_id = aws_route_table. public [0 ]. id
4652 destination_cidr_block = each. value . destination_cidr_block
4753 destination_prefix_list_id = each. value . destination_prefix_list_id
48- gateway_id = local. route_to_igw ? aws_internet_gateway. this [0 ]. id : each. value . gateway_id
49- egress_only_gateway_id = each. value . egress_only_gateway_id
54+ network_interface_id = each. value . network_interface_id
55+ vpc_peering_connection_id = each. value . vpc_peering_connection_id
56+ transit_gateway_id = each. value . transit_gateway_id
57+ vpc_endpoint_id = each. value . vpc_endpoint_id
58+ carrier_gateway_id = each. value . carrier_gateway_id
5059}
5160
5261resource "aws_route_table_association" "private" {
53- for_each = { for subnet in var . private_subnet : subnet . name => subnet if local . enable_private_routing }
62+ for_each = { for subnet in var . private_subnets : subnet . name => subnet if var . create_private_route_table }
5463 subnet_id = aws_subnet. private_subnet [(each. value . name )]. id
5564 route_table_id = aws_route_table. private [0 ]. id
5665}
5766
5867resource "aws_route_table_association" "public" {
59- for_each = { for subnet in var . public_subnet : subnet . name => subnet if (length (var. public_subnet ) > 0 ) }
68+ for_each = { for subnet in var . public_subnets : subnet . name => subnet if (length (var. public_subnets ) > 0 ) }
6069 subnet_id = aws_subnet. public_subnet [(each. value . name )]. id
6170 route_table_id = aws_route_table. public [0 ]. id
6271}
63-
0 commit comments