Skip to content

Commit 1e1a827

Browse files
Arnel Jan SarmientoASPactores
authored andcommitted
feat: Add support for optional Transit Gateway subnets in AWS VPC module, including new variables, resources, and outputs for subnet and route table IDs
1 parent 366ff89 commit 1e1a827

5 files changed

Lines changed: 55 additions & 1 deletion

File tree

.github/dependabot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
version: 2
22
updates:
33
- package-ecosystem: "terraform"
4-
directory: "/"
4+
directory: "/"
55
schedule:
66
interval: daily
77
time: "13:00"

modules/aws/vpc/main.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ locals {
1616
nat_az_keys = var.enable_nat_gateway ? (
1717
var.single_nat_gateway ? { (var.availability_zones[0]) = 0 } : local.az_keys
1818
) : {}
19+
20+
tgw_az_keys = length(var.transit_gateway_subnet_cidrs) > 0 ? local.az_keys : {}
1921
}
2022

2123
# ------------------------------------------------------------------------------
@@ -115,6 +117,39 @@ resource "aws_route_table_association" "private" {
115117
route_table_id = aws_route_table.private[each.key].id
116118
}
117119

120+
# ------------------------------------------------------------------------------
121+
# Transit Gateway Subnets (optional)
122+
# ------------------------------------------------------------------------------
123+
124+
resource "aws_subnet" "transit_gateway" {
125+
for_each = local.tgw_az_keys
126+
127+
vpc_id = aws_vpc.this.id
128+
cidr_block = var.transit_gateway_subnet_cidrs[each.value]
129+
availability_zone = each.key
130+
131+
tags = {
132+
Name = "${var.name}-tgw-${each.key}"
133+
}
134+
}
135+
136+
resource "aws_route_table" "transit_gateway" {
137+
for_each = local.tgw_az_keys
138+
139+
vpc_id = aws_vpc.this.id
140+
141+
tags = {
142+
Name = "${var.name}-tgw-${each.key}"
143+
}
144+
}
145+
146+
resource "aws_route_table_association" "transit_gateway" {
147+
for_each = aws_subnet.transit_gateway
148+
149+
subnet_id = each.value.id
150+
route_table_id = aws_route_table.transit_gateway[each.key].id
151+
}
152+
118153
# ------------------------------------------------------------------------------
119154
# NAT Gateway (optional)
120155
# ------------------------------------------------------------------------------

modules/aws/vpc/outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ output "private_route_table_ids" {
4848
value = [for rt in aws_route_table.private : rt.id]
4949
}
5050

51+
output "transit_gateway_subnet_ids" {
52+
description = "List of transit gateway subnet IDs. Empty when transit_gateway_subnet_cidrs is not provided."
53+
value = [for s in aws_subnet.transit_gateway : s.id]
54+
}
55+
56+
output "transit_gateway_route_table_ids" {
57+
description = "List of transit gateway route table IDs. Empty when transit_gateway_subnet_cidrs is not provided."
58+
value = [for rt in aws_route_table.transit_gateway : rt.id]
59+
}
60+
5161
output "flow_log_id" {
5262
description = "ID of the VPC flow log."
5363
value = aws_flow_log.this.id

modules/aws/vpc/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,9 @@ variable "flow_log_retention_days" {
5252
type = number
5353
default = 30
5454
}
55+
56+
variable "transit_gateway_subnet_cidrs" {
57+
description = "CIDR blocks for transit gateway subnets, one per availability zone. Leave empty to skip creation."
58+
type = list(string)
59+
default = []
60+
}

workspaces/root/organizations/main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ resource "aws_organizations_organization" "this" {
3333
enabled_policy_types = ["SERVICE_CONTROL_POLICY"]
3434

3535
aws_service_access_principals = [
36+
"ram.amazonaws.com",
3637
"sso.amazonaws.com",
3738
]
3839
}
3940

41+
resource "aws_ram_sharing_with_organization" "this" {}
42+
4043
resource "aws_organizations_account" "accounts" {
4144
for_each = local.member_accounts
4245

0 commit comments

Comments
 (0)