Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core 2711 #120

Closed
wants to merge 14 commits into from
72 changes: 64 additions & 8 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ locals {

vpc_route_table_destination_cidr = flatten([
for k, v in var.vpc_attachments : [
for rtb_id in try(v.vpc_route_table_ids, []) : {
rtb_id = rtb_id
cidr = v.tgw_destination_cidr
}
for rtb_id in try(v.vpc_route_table_ids, []) : [
for tgw_route in try(v.tgw_routes, []) : {
vpc_attachment_id = k
rtb_id = rtb_id
cidr = tgw_route.destination_cidr_block
tgw_id = v.tgw_id
}
]
]
])
}
Expand Down Expand Up @@ -110,11 +114,11 @@ resource "aws_ec2_transit_gateway_route" "this" {
}

resource "aws_route" "this" {
for_each = { for x in local.vpc_route_table_destination_cidr : x.rtb_id => x.cidr }
for_each = { for index, x in local.vpc_route_table_destination_cidr : "${x.tgw_id}-${x.rtb_id}-${x.cidr}" => { "rtb_id" : x.rtb_id, "cidr" : x.cidr, "tgw_id" : x.tgw_id } }

route_table_id = each.key
destination_cidr_block = each.value
transit_gateway_id = aws_ec2_transit_gateway.this[0].id
route_table_id = each.value.rtb_id
destination_cidr_block = each.value.cidr
transit_gateway_id = var.create_tgw ? aws_ec2_transit_gateway.this[0].id : each.value.tgw_id
}

resource "aws_ec2_transit_gateway_route_table_association" "this" {
Expand Down Expand Up @@ -173,3 +177,55 @@ resource "aws_ram_resource_share_accepter" "this" {

share_arn = var.ram_resource_share_arn
}

# Transit Gateway Peering Attachment
resource "aws_ec2_transit_gateway_peering_attachment" "this" {
for_each = var.tgw_peering_attachments

transit_gateway_id = aws_ec2_transit_gateway.this[0].id
peer_transit_gateway_id = each.value.peer_transit_gateway_id
peer_region = each.value.peer_region
peer_account_id = each.value.peer_account_id

tags = merge(
var.tags,
{ Name = "${var.name}-peering-${each.key}" }
)

lifecycle {
create_before_destroy = true
}
}

# Accepting Peering Attachment
resource "aws_ec2_transit_gateway_peering_attachment_accepter" "this" {
count = length([for k, v in var.tgw_peering_attachments : v if v.request_accepter])

transit_gateway_attachment_id = tolist([for attachment in aws_ec2_transit_gateway_peering_attachment.this : attachment.id])[count.index]

tags = merge(
var.tags,
{ Name = "${var.name}-peering-accepter-${count.index}" }
)
}

# Transit Gateway Peering Route Table
resource "aws_ec2_transit_gateway_route_table" "peering" {
count = length(var.tgw_peering_attachments) > 0 ? 1 : 0

transit_gateway_id = aws_ec2_transit_gateway.this[0].id

tags = merge(
var.tags,
{ Name = "${var.name}-tgw-peering-route-table" }
)
}

# Routes for Peering Attachments
resource "aws_ec2_transit_gateway_route" "peering" {
for_each = { for r in var.tgw_peering_route_table_routes : "${r.peering_attachment_key}-${r.destination_cidr_block}" => r }

destination_cidr_block = each.value.destination_cidr_block
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.peering[0].id
transit_gateway_attachment_id = aws_ec2_transit_gateway_peering_attachment.this[each.value.peering_attachment_key].id
}
24 changes: 24 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,27 @@ variable "ram_tags" {
type = map(string)
default = {}
}

################################################################################
# TGW Peering Settings
################################################################################
variable "tgw_peering_attachments" {
description = "A map of transit gateway peering attachments"
type = map(object({
peer_transit_gateway_id = string
peer_region = string
peer_account_id = string
request_accepter = bool
}))
default = {}
}

variable "tgw_peering_route_table_routes" {
description = "A list of routes for the Transit Gateway Peering Route Table"
type = list(object({
destination_cidr_block = string
peering_attachment_key = string
}))
default = []
}

Loading