Skip to content

Commit ef781be

Browse files
authored
Merge pull request #56 from aws-ia/tgw-association
include tgw rt assocation, re-organize rsc
2 parents 26f0553 + 07304e1 commit ef781be

File tree

3 files changed

+94
-81
lines changed

3 files changed

+94
-81
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ Please see our [developer documentation](https://github.com/aws-ia/terraform-aws
187187
| [awscc_ec2_route_table.tgw](https://registry.terraform.io/providers/hashicorp/awscc/latest/docs/resources/ec2_route_table) | resource |
188188
| [awscc_ec2_subnet_route_table_association.private](https://registry.terraform.io/providers/hashicorp/awscc/latest/docs/resources/ec2_subnet_route_table_association) | resource |
189189
| [awscc_ec2_subnet_route_table_association.public](https://registry.terraform.io/providers/hashicorp/awscc/latest/docs/resources/ec2_subnet_route_table_association) | resource |
190+
| [awscc_ec2_subnet_route_table_association.tgw](https://registry.terraform.io/providers/hashicorp/awscc/latest/docs/resources/ec2_subnet_route_table_association) | resource |
190191
| [aws_availability_zones.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
191192
| [aws_vpc_ipam_preview_next_cidr.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc_ipam_preview_next_cidr) | data source |
192193
| [awscc_ec2_vpc.main](https://registry.terraform.io/providers/hashicorp/awscc/latest/docs/data-sources/ec2_vpc) | data source |

examples/transit_gateway/main.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,3 @@ module "vpc" {
3232
}
3333
}
3434
}
35-

main.tf

Lines changed: 93 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,7 @@ resource "aws_vpc_ipv4_cidr_block_association" "secondary" {
3030
ipv4_ipam_pool_id = var.vpc_ipv4_ipam_pool_id
3131
}
3232

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
4934

5035
resource "aws_subnet" "public" {
5136
for_each = try(local.subnets.public, {})
@@ -59,41 +44,6 @@ resource "aws_subnet" "public" {
5944
module.tags.tags_aws)
6045
}
6146

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-
9747
resource "awscc_ec2_route_table" "public" {
9848
for_each = try(local.subnets.public, {})
9949

@@ -105,13 +55,6 @@ resource "awscc_ec2_route_table" "public" {
10555
)
10656
}
10757

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-
11558
resource "awscc_ec2_subnet_route_table_association" "public" {
11659
for_each = try(local.subnets.public, {})
11760

@@ -152,27 +95,71 @@ resource "aws_internet_gateway" "main" {
15295
module.tags.tags_aws)
15396
}
15497

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, {})
158100

159-
route_table_id = awscc_ec2_route_table.private[each.key].id
101+
route_table_id = awscc_ec2_route_table.public[each.key].id
160102
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
163104
}
164105

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" {
166154
# 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, {}) : {}
168156

169-
route_table_id = awscc_ec2_route_table.tgw[each.key].id
157+
route_table_id = awscc_ec2_route_table.private[each.key].id
170158
destination_cidr_block = "0.0.0.0/0"
171159
# try to get nat for AZ, else use singular nat
172160
nat_gateway_id = try(aws_nat_gateway.main[each.key].id, aws_nat_gateway.main[local.nat_configuration[0]].id)
173161
}
174162

175-
176163
resource "aws_route" "private_to_tgw" {
177164
# TODO: move logic to locals once `route_to_transit_gateway` can accept more than 1 list item
178165
for_each = try(var.subnets.private.route_to_transit_gateway, []) != [] ? toset([
@@ -184,22 +171,46 @@ resource "aws_route" "private_to_tgw" {
184171
transit_gateway_id = var.subnets.transit_gateway.transit_gateway_id
185172
}
186173

187-
resource "aws_route" "public_to_igw" {
188-
for_each = try(local.subnets.public, {})
174+
# Transit Gateway Subnets
189175

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)
193186
}
194187

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, {})
199190

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)
203214
}
204215

205216
resource "aws_ec2_transit_gateway_vpc_attachment" "tgw" {
@@ -220,6 +231,8 @@ resource "aws_ec2_transit_gateway_route_table_association" "tgw" {
220231
transit_gateway_route_table_id = var.subnets.transit_gateway.transit_gateway_route_table_id
221232
}
222233

234+
# Flow Logs
235+
223236
module "flow_logs" {
224237
count = local.create_flow_logs ? 1 : 0
225238

0 commit comments

Comments
 (0)