@@ -30,22 +30,7 @@ resource "aws_vpc_ipv4_cidr_block_association" "secondary" {
30
30
ipv4_ipam_pool_id = var. vpc_ipv4_ipam_pool_id
31
31
}
32
32
33
- resource "aws_subnet" "private" {
34
- for_each = try (local. subnets . private , {})
35
-
36
- availability_zone = each. key
37
- vpc_id = local. vpc . id
38
- cidr_block = each. value
39
- map_public_ip_on_launch = false
40
-
41
- tags = merge ({
42
- Name = " ${ local . subnet_names [" private" ]} -${ each . key } " },
43
- module. tags . tags_aws )
44
-
45
- depends_on = [
46
- aws_vpc_ipv4_cidr_block_association . secondary
47
- ]
48
- }
33
+ # Public Subnets
49
34
50
35
resource "aws_subnet" "public" {
51
36
for_each = try (local. subnets . public , {})
@@ -59,41 +44,6 @@ resource "aws_subnet" "public" {
59
44
module. tags . tags_aws )
60
45
}
61
46
62
- resource "aws_subnet" "tgw" {
63
- for_each = try (local. subnets . transit_gateway , {})
64
-
65
- availability_zone = each. key
66
- vpc_id = local. vpc . id
67
- cidr_block = each. value
68
-
69
- tags = merge ({
70
- Name = " ${ local . subnet_names [" transit_gateway" ]} -${ each . key } " },
71
- module. tags . tags_aws )
72
- }
73
-
74
- resource "awscc_ec2_route_table" "private" {
75
- for_each = try (local. subnets . private , {})
76
-
77
- vpc_id = local. vpc . id
78
-
79
- tags = concat (
80
- [{ " key" = " Name" , " value" = " ${ local . subnet_names [" private" ]} -${ each . key } " }],
81
- module. tags . tags
82
- )
83
- }
84
-
85
- resource "awscc_ec2_route_table" "tgw" {
86
- for_each = try (local. subnets . transit_gateway , {})
87
-
88
- vpc_id = local. vpc . id
89
-
90
- tags = concat (
91
- [{ " key" = " Name" , " value" = " ${ local . subnet_names [" transit_gateway" ]} -${ each . key } " }],
92
- module. tags . tags
93
- )
94
- }
95
-
96
-
97
47
resource "awscc_ec2_route_table" "public" {
98
48
for_each = try (local. subnets . public , {})
99
49
@@ -105,13 +55,6 @@ resource "awscc_ec2_route_table" "public" {
105
55
)
106
56
}
107
57
108
- resource "awscc_ec2_subnet_route_table_association" "private" {
109
- for_each = try (local. subnets . private , {})
110
-
111
- subnet_id = aws_subnet. private [each . key ]. id
112
- route_table_id = awscc_ec2_route_table. private [each . key ]. id
113
- }
114
-
115
58
resource "awscc_ec2_subnet_route_table_association" "public" {
116
59
for_each = try (local. subnets . public , {})
117
60
@@ -152,27 +95,71 @@ resource "aws_internet_gateway" "main" {
152
95
module. tags . tags_aws )
153
96
}
154
97
155
- resource "aws_route" "private_to_nat" {
156
- # if `route_to_nat` exists & `true` apply to private subnets per az, else do not apply
157
- for_each = try (var. subnets . private . route_to_nat , false ) ? try (local. subnets . public , {}) : {}
98
+ resource "aws_route" "public_to_igw" {
99
+ for_each = try (local. subnets . public , {})
158
100
159
- route_table_id = awscc_ec2_route_table. private [each . key ]. id
101
+ route_table_id = awscc_ec2_route_table. public [each . key ]. id
160
102
destination_cidr_block = " 0.0.0.0/0"
161
- # try to get nat for AZ, else use singular nat
162
- nat_gateway_id = try (aws_nat_gateway. main [each . key ]. id , aws_nat_gateway. main [local . nat_configuration [0 ]]. id )
103
+ gateway_id = aws_internet_gateway. main [0 ]. id
163
104
}
164
105
165
- resource "aws_route" "tgw_to_nat" {
106
+ resource "aws_route" "public_to_tgw" {
107
+ for_each = try (var. subnets . public . route_to_transit_gateway , []) != [] ? toset ([
108
+ for _, key in keys (local. subnets . public ) : " ${ key } :${ var . subnets . public . route_to_transit_gateway [0 ]} "
109
+ ]) : toset ([])
110
+
111
+ route_table_id = awscc_ec2_route_table. public [split (" :" , each. key )[0 ]]. id
112
+ destination_cidr_block = var. subnets . public . route_to_transit_gateway [0 ]
113
+ transit_gateway_id = var. subnets . transit_gateway . transit_gateway_id
114
+ }
115
+
116
+ # Private Subnets
117
+
118
+ resource "aws_subnet" "private" {
119
+ for_each = try (local. subnets . private , {})
120
+
121
+ availability_zone = each. key
122
+ vpc_id = local. vpc . id
123
+ cidr_block = each. value
124
+ map_public_ip_on_launch = false
125
+
126
+ tags = merge ({
127
+ Name = " ${ local . subnet_names [" private" ]} -${ each . key } " },
128
+ module. tags . tags_aws )
129
+
130
+ depends_on = [
131
+ aws_vpc_ipv4_cidr_block_association . secondary
132
+ ]
133
+ }
134
+
135
+ resource "awscc_ec2_route_table" "private" {
136
+ for_each = try (local. subnets . private , {})
137
+
138
+ vpc_id = local. vpc . id
139
+
140
+ tags = concat (
141
+ [{ " key" = " Name" , " value" = " ${ local . subnet_names [" private" ]} -${ each . key } " }],
142
+ module. tags . tags
143
+ )
144
+ }
145
+
146
+ resource "awscc_ec2_subnet_route_table_association" "private" {
147
+ for_each = try (local. subnets . private , {})
148
+
149
+ subnet_id = aws_subnet. private [each . key ]. id
150
+ route_table_id = awscc_ec2_route_table. private [each . key ]. id
151
+ }
152
+
153
+ resource "aws_route" "private_to_nat" {
166
154
# if `route_to_nat` exists & `true` apply to private subnets per az, else do not apply
167
- for_each = try (var. subnets . transit_gateway . route_to_nat , false ) ? try (local. subnets . public , {}) : {}
155
+ for_each = try (var. subnets . private . route_to_nat , false ) ? try (local. subnets . public , {}) : {}
168
156
169
- route_table_id = awscc_ec2_route_table. tgw [each . key ]. id
157
+ route_table_id = awscc_ec2_route_table. private [each . key ]. id
170
158
destination_cidr_block = " 0.0.0.0/0"
171
159
# try to get nat for AZ, else use singular nat
172
160
nat_gateway_id = try (aws_nat_gateway. main [each . key ]. id , aws_nat_gateway. main [local . nat_configuration [0 ]]. id )
173
161
}
174
162
175
-
176
163
resource "aws_route" "private_to_tgw" {
177
164
# TODO: move logic to locals once `route_to_transit_gateway` can accept more than 1 list item
178
165
for_each = try (var. subnets . private . route_to_transit_gateway , []) != [] ? toset ([
@@ -184,22 +171,46 @@ resource "aws_route" "private_to_tgw" {
184
171
transit_gateway_id = var. subnets . transit_gateway . transit_gateway_id
185
172
}
186
173
187
- resource "aws_route" "public_to_igw" {
188
- for_each = try (local. subnets . public , {})
174
+ # Transit Gateway Subnets
189
175
190
- route_table_id = awscc_ec2_route_table. public [each . key ]. id
191
- destination_cidr_block = " 0.0.0.0/0"
192
- gateway_id = aws_internet_gateway. main [0 ]. id
176
+ resource "aws_subnet" "tgw" {
177
+ for_each = try (local. subnets . transit_gateway , {})
178
+
179
+ availability_zone = each. key
180
+ vpc_id = local. vpc . id
181
+ cidr_block = each. value
182
+
183
+ tags = merge ({
184
+ Name = " ${ local . subnet_names [" transit_gateway" ]} -${ each . key } " },
185
+ module. tags . tags_aws )
193
186
}
194
187
195
- resource "aws_route" "public_to_tgw" {
196
- for_each = try (var. subnets . public . route_to_transit_gateway , []) != [] ? toset ([
197
- for _, key in keys (local. subnets . public ) : " ${ key } :${ var . subnets . public . route_to_transit_gateway [0 ]} "
198
- ]) : toset ([])
188
+ resource "awscc_ec2_route_table" "tgw" {
189
+ for_each = try (local. subnets . transit_gateway , {})
199
190
200
- route_table_id = awscc_ec2_route_table. public [split (" :" , each. key )[0 ]]. id
201
- destination_cidr_block = var. subnets . public . route_to_transit_gateway [0 ]
202
- transit_gateway_id = var. subnets . transit_gateway . transit_gateway_id
191
+ vpc_id = local. vpc . id
192
+
193
+ tags = concat (
194
+ [{ " key" = " Name" , " value" = " ${ local . subnet_names [" transit_gateway" ]} -${ each . key } " }],
195
+ module. tags . tags
196
+ )
197
+ }
198
+
199
+ resource "awscc_ec2_subnet_route_table_association" "tgw" {
200
+ for_each = try (local. subnets . transit_gateway , {})
201
+
202
+ subnet_id = aws_subnet. tgw [each . key ]. id
203
+ route_table_id = awscc_ec2_route_table. tgw [each . key ]. id
204
+ }
205
+
206
+ resource "aws_route" "tgw_to_nat" {
207
+ # if `route_to_nat` exists & `true` apply to private subnets per az, else do not apply
208
+ for_each = try (var. subnets . transit_gateway . route_to_nat , false ) ? try (local. subnets . public , {}) : {}
209
+
210
+ route_table_id = awscc_ec2_route_table. tgw [each . key ]. id
211
+ destination_cidr_block = " 0.0.0.0/0"
212
+ # try to get nat for AZ, else use singular nat
213
+ nat_gateway_id = try (aws_nat_gateway. main [each . key ]. id , aws_nat_gateway. main [local . nat_configuration [0 ]]. id )
203
214
}
204
215
205
216
resource "aws_ec2_transit_gateway_vpc_attachment" "tgw" {
@@ -220,6 +231,8 @@ resource "aws_ec2_transit_gateway_route_table_association" "tgw" {
220
231
transit_gateway_route_table_id = var. subnets . transit_gateway . transit_gateway_route_table_id
221
232
}
222
233
234
+ # Flow Logs
235
+
223
236
module "flow_logs" {
224
237
count = local. create_flow_logs ? 1 : 0
225
238
0 commit comments