Skip to content

Commit 0639b34

Browse files
authored
Merge pull request #45 from aws-ia/tgw
Tgw
2 parents 16c01c1 + a977fd7 commit 0639b34

File tree

11 files changed

+99
-8
lines changed

11 files changed

+99
-8
lines changed

.tflint.hcl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,12 @@ rule "terraform_standard_module_structure" {
6464
rule "terraform_workspace_remote" {
6565
enabled = true
6666
}
67+
68+
# seems to be a bug when a resource is not created
69+
rule "aws_route_not_specified_target" {
70+
enabled = false
71+
}
72+
73+
rule "aws_route_specified_multiple_targets" {
74+
enabled = false
75+
}

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,18 @@ The above example will cause only creating 2 new subnets in az `c` of the region
113113
| [aws_internet_gateway.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/internet_gateway) | resource |
114114
| [aws_nat_gateway.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/nat_gateway) | resource |
115115
| [aws_route.private_to_nat](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource |
116+
| [aws_route.private_to_tgw](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource |
116117
| [aws_route.public_to_igw](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource |
118+
| [aws_route.public_to_tgw](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource |
119+
| [aws_route.tgw_to_nat](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource |
117120
| [aws_subnet.private](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet) | resource |
118121
| [aws_subnet.public](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet) | resource |
119122
| [aws_subnet.tgw](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet) | resource |
120123
| [aws_vpc.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc) | resource |
121124
| [aws_vpc_ipv4_cidr_block_association.secondary](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc_ipv4_cidr_block_association) | resource |
122125
| [awscc_ec2_route_table.private](https://registry.terraform.io/providers/hashicorp/awscc/latest/docs/resources/ec2_route_table) | resource |
123126
| [awscc_ec2_route_table.public](https://registry.terraform.io/providers/hashicorp/awscc/latest/docs/resources/ec2_route_table) | resource |
127+
| [awscc_ec2_route_table.tgw](https://registry.terraform.io/providers/hashicorp/awscc/latest/docs/resources/ec2_route_table) | resource |
124128
| [awscc_ec2_subnet_route_table_association.private](https://registry.terraform.io/providers/hashicorp/awscc/latest/docs/resources/ec2_subnet_route_table_association) | resource |
125129
| [awscc_ec2_subnet_route_table_association.public](https://registry.terraform.io/providers/hashicorp/awscc/latest/docs/resources/ec2_subnet_route_table_association) | resource |
126130
| [aws_availability_zones.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
@@ -149,6 +153,7 @@ The above example will cause only creating 2 new subnets in az `c` of the region
149153

150154
| Name | Description |
151155
|------|-------------|
156+
| <a name="output_nat_gateways_by_az"></a> [nat\_gateways\_by\_az](#output\_nat\_gateways\_by\_az) | Map of nat gateway resource attributes by AZ. |
152157
| <a name="output_private_subnet_attributes_by_az"></a> [private\_subnet\_attributes\_by\_az](#output\_private\_subnet\_attributes\_by\_az) | Map of all private subnets containing their attributes. |
153158
| <a name="output_public_subnet_attributes_by_az"></a> [public\_subnet\_attributes\_by\_az](#output\_public\_subnet\_attributes\_by\_az) | Map of all public subnets containing their attributes. |
154159
| <a name="output_route_table_by_subnet_type"></a> [route\_table\_by\_subnet\_type](#output\_route\_table\_by\_subnet\_type) | Map of route tables by type => az => route table attributes. Example usage: module.vpc.route\_table\_by\_subnet\_type.private.id |

examples/ipam/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ No resources.
3232
| Name | Description |
3333
|------|-------------|
3434
| <a name="output_subnets"></a> [subnets](#output\_subnets) | Map of subnet types with key/value az = cidr. |
35-
<!-- END_TF_DOCS -->
35+
<!-- END_TF_DOCS -->

examples/private_only/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ No inputs.
3131
|------|-------------|
3232
| <a name="output_subnet_attributes"></a> [subnet\_attributes](#output\_subnet\_attributes) | n/a |
3333
| <a name="output_subnets"></a> [subnets](#output\_subnets) | Map of subnet types with key/value az = cidr. |
34-
<!-- END_TF_DOCS -->
34+
<!-- END_TF_DOCS -->

examples/public_only/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ No inputs.
3030
| Name | Description |
3131
|------|-------------|
3232
| <a name="output_subnets"></a> [subnets](#output\_subnets) | Map of subnet types with key/value az = cidr. |
33-
<!-- END_TF_DOCS -->
33+
<!-- END_TF_DOCS -->

examples/public_private_flow_logs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ No resources.
3434
| Name | Description |
3535
|------|-------------|
3636
| <a name="output_subnets"></a> [subnets](#output\_subnets) | Map of subnet types with key/value az = cidr. |
37-
<!-- END_TF_DOCS -->
37+
<!-- END_TF_DOCS -->

examples/secondary_cidr/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ No inputs.
3636
| Name | Description |
3737
|------|-------------|
3838
| <a name="output_secondary_subnets"></a> [secondary\_subnets](#output\_secondary\_subnets) | Map of subnet types with key/value az = cidr. |
39-
<!-- END_TF_DOCS -->
39+
<!-- END_TF_DOCS -->

examples/transit_gateway/main.tf

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,24 @@ module "vpc" {
1111
az_count = 2
1212

1313
subnets = {
14+
public = {
15+
netmask = 24
16+
nat_gateway_configuration = "single_az"
17+
route_to_transit_gateway = ["10.1.0.0/16"]
18+
}
19+
20+
private = {
21+
netmask = 24
22+
route_to_nat = true
23+
route_to_transit_gateway = ["10.1.0.0/16"]
24+
}
25+
1426
transit_gateway = {
15-
netmask = 24
16-
transit_gateway_id = aws_ec2_transit_gateway.example.id
27+
netmask = 24
28+
transit_gateway_id = aws_ec2_transit_gateway.example.id
29+
route_to_nat = false
30+
transit_gateway_default_route_table_association = true
31+
transit_gateway_default_route_table_propagation = true
1732
}
1833
}
1934
}

main.tf

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ resource "awscc_ec2_route_table" "private" {
8282
)
8383
}
8484

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+
8597
resource "awscc_ec2_route_table" "public" {
8698
for_each = try(local.subnets.public, {})
8799

@@ -150,6 +162,28 @@ resource "aws_route" "private_to_nat" {
150162
nat_gateway_id = try(aws_nat_gateway.main[each.key].id, aws_nat_gateway.main[local.nat_configuration[0]].id)
151163
}
152164

165+
resource "aws_route" "tgw_to_nat" {
166+
# 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, {}) : {}
168+
169+
route_table_id = awscc_ec2_route_table.tgw[each.key].id
170+
destination_cidr_block = "0.0.0.0/0"
171+
# try to get nat for AZ, else use singular nat
172+
nat_gateway_id = try(aws_nat_gateway.main[each.key].id, aws_nat_gateway.main[local.nat_configuration[0]].id)
173+
}
174+
175+
176+
resource "aws_route" "private_to_tgw" {
177+
# TODO: move logic to locals once `route_to_transit_gateway` can accept more than 1 list item
178+
for_each = try(var.subnets.private.route_to_transit_gateway, []) != [] ? toset([
179+
for _, key in keys(local.subnets.private) : "${key}:${var.subnets.private.route_to_transit_gateway[0]}"
180+
]) : toset([])
181+
182+
route_table_id = awscc_ec2_route_table.private[split(":", each.key)[0]].id
183+
destination_cidr_block = var.subnets.private.route_to_transit_gateway[0]
184+
transit_gateway_id = var.subnets.transit_gateway.transit_gateway_id
185+
}
186+
153187
resource "aws_route" "public_to_igw" {
154188
for_each = try(local.subnets.public, {})
155189

@@ -158,6 +192,16 @@ resource "aws_route" "public_to_igw" {
158192
gateway_id = aws_internet_gateway.main[0].id
159193
}
160194

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([])
199+
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
203+
}
204+
161205
resource "aws_ec2_transit_gateway_vpc_attachment" "tgw" {
162206
count = contains(keys(local.subnets), "transit_gateway") ? 1 : 0
163207

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,8 @@ output "route_table_by_subnet_type" {
5050
"public" = awscc_ec2_route_table.public
5151
}
5252
}
53+
54+
output "nat_gateways_by_az" {
55+
description = "Map of nat gateway resource attributes by AZ."
56+
value = try(aws_nat_gateway.main, null)
57+
}

0 commit comments

Comments
 (0)