Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions data.tf
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ data "aws_subnets" "this" {

# get route tables associated with subnets
data "aws_route_tables" "this_associated_route_tables" {
for_each = { for subnet in data.aws_subnets.this.ids : subnet => subnet }
for_each = { for subnet in var.this_subnets_ids : subnet => subnet }
provider = aws.this
vpc_id = var.this_vpc_id
filter {
Expand All @@ -67,6 +67,10 @@ data "aws_route_tables" "this_associated_route_tables" {
}
}

data "aws_route_tables" "this_all_route_tables" {
provider = aws.this
vpc_id = var.this_vpc_id
}

# Get subnets and route tables from peer

Expand All @@ -91,7 +95,7 @@ data "aws_subnets" "peer" {

# get route tables associated with subnets
data "aws_route_tables" "peer_associated_route_tables" {
for_each = { for subnet in data.aws_subnets.peer.ids : subnet => subnet }
for_each = { for subnet in var.peer_subnets_ids : subnet => subnet }
provider = aws.peer
vpc_id = var.peer_vpc_id
filter {
Expand All @@ -100,3 +104,7 @@ data "aws_route_tables" "peer_associated_route_tables" {
}
}

data "aws_route_tables" "peer_all_route_tables" {
provider = aws.peer
vpc_id = var.peer_vpc_id
}
20 changes: 10 additions & 10 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,38 @@ locals {
this_subnet_route_table_map = {
for subnet in data.aws_subnets.this.ids :
subnet => concat(
data.aws_route_tables.this_associated_route_tables[subnet].ids,
try(data.aws_route_tables.this_associated_route_tables[subnet].ids, []),
[data.aws_route_table.this_main_route_table.id]
)[0]
}

peer_subnet_route_table_map = {
for subnet in data.aws_subnets.peer.ids :
subnet => concat(
data.aws_route_tables.peer_associated_route_tables[subnet].ids,
try(data.aws_route_tables.peer_associated_route_tables[subnet].ids, []),
[data.aws_route_table.peer_main_route_table.id]
)[0]
}

this_rts_ids = length(var.this_subnets_ids) == 0 ? distinct(values(local.this_subnet_route_table_map)) : distinct([
this_rts_ids = length(var.this_subnets_ids) == 0 ? data.aws_route_tables.this_all_route_tables.ids : distinct([
for subnet_id in var.this_subnets_ids : local.this_subnet_route_table_map[subnet_id]
])

peer_rts_ids = length(var.peer_subnets_ids) == 0 ? distinct(values(local.peer_subnet_route_table_map)) : distinct([
peer_rts_ids = length(var.peer_subnets_ids) == 0 ? data.aws_route_tables.peer_all_route_tables.ids : distinct([
for subnet_id in var.peer_subnets_ids : local.peer_subnet_route_table_map[subnet_id]
])

# `this_dest_cidrs` represent CIDR of peer VPC, therefore a destination CIDR for this_vpc
# `peer_dest_cidrs` represent CIDR of this VPC, therefore a destination CIDR for peer_vpc
# Destination cidrs for this are in peer and vice versa
this_dest_ipv4_cidrs = toset(compact(length(var.peer_subnets_ids) == 0 ? [data.aws_vpc.peer_vpc.cidr_block] : data.aws_subnet.peer[*].cidr_block))
this_dest_ipv6_cidrs = toset(compact(length(var.peer_subnets_ids) == 0 && var.use_ipv6 ? [data.aws_vpc.peer_vpc.ipv6_cidr_block] : data.aws_subnet.peer[*].ipv6_cidr_block))
peer_dest_ipv4_cidrs = toset(compact(length(var.this_subnets_ids) == 0 ? [data.aws_vpc.this_vpc.cidr_block] : data.aws_subnet.this[*].cidr_block))
peer_dest_ipv6_cidrs = toset(compact(length(var.this_subnets_ids) == 0 && var.use_ipv6 ? [data.aws_vpc.this_vpc.ipv6_cidr_block] : data.aws_subnet.this[*].ipv6_cidr_block))
this_dest_ipv4_cidrs = toset(length(var.peer_subnets_ids) == 0 ? [data.aws_vpc.peer_vpc.cidr_block] : data.aws_subnet.peer[*].cidr_block)
this_dest_ipv6_cidrs = toset(length(var.peer_subnets_ids) == 0 && var.use_ipv6 ? [data.aws_vpc.peer_vpc.ipv6_cidr_block] : data.aws_subnet.peer[*].ipv6_cidr_block)
peer_dest_ipv4_cidrs = toset(length(var.this_subnets_ids) == 0 ? [data.aws_vpc.this_vpc.cidr_block] : data.aws_subnet.this[*].cidr_block)
peer_dest_ipv6_cidrs = toset(length(var.this_subnets_ids) == 0 && var.use_ipv6 ? [data.aws_vpc.this_vpc.ipv6_cidr_block] : data.aws_subnet.this[*].ipv6_cidr_block)

# Get associated CIDR blocks
this_associated_dest_cidrs = toset(compact([for k, v in data.aws_vpc.peer_vpc.cidr_block_associations : v.cidr_block]))
peer_associated_dest_cidrs = toset(compact([for k, v in data.aws_vpc.this_vpc.cidr_block_associations : v.cidr_block]))
this_associated_dest_cidrs = toset([for k, v in data.aws_vpc.peer_vpc.cidr_block_associations : v.cidr_block])
peer_associated_dest_cidrs = toset([for k, v in data.aws_vpc.this_vpc.cidr_block_associations : v.cidr_block])

# Allow specifying route tables explicitly
this_rts_ids_hack = length(var.this_rts_ids) == 0 ? local.this_rts_ids : var.this_rts_ids
Expand Down
2 changes: 1 addition & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ variable "peer_rts_ids" {
variable "use_ipv6" {
description = "If ipv6 should be used"
type = bool
default = true
default = false
}
Loading