Skip to content

Commit

Permalink
fix: Update routes resource for rvpc route table
Browse files Browse the repository at this point in the history
  • Loading branch information
13archit committed Sep 4, 2023
1 parent 2c01c2e commit c52e4a0
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 152 deletions.
140 changes: 116 additions & 24 deletions _example/different-account/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,146 @@ provider "aws" {
region = "eu-west-2"
}

provider "aws" {
alias = "test"
assume_role {
role_arn = ""
}
region = "eu-west-2"
}
locals {
name = "app"
environment = "test"
other_name = "app_1"
other_environment = "test_1"
}

##------------------------------------------------------------------------------
# VPC module call.
## VPC module call.
##------------------------------------------------------------------------------
module "vpc" {
source = "clouddrove/vpc/aws"
version = "2.0.0"
name = "vpc"
environment = "test"
cidr_block = "172.16.0.0/16"
name = local.name
environment = local.environment
cidr_block = "10.10.0.0/16"
}

##------------------------------------------------------------------------------
# Subnet module call.
## Subnet module call.
##------------------------------------------------------------------------------
module "subnets" {
source = "clouddrove/subnet/aws"
version = "1.3.0"

name = "subnets"
environment = "test"
source = "clouddrove/subnet/aws"
version = "2.0.0"
name = local.name
environment = local.environment
availability_zones = ["eu-west-2a", "eu-west-2b"]
vpc_id = module.vpc.vpc_id
type = "public"
igw_id = module.vpc.igw_id
nat_gateway_enabled = false
cidr_block = module.vpc.vpc_cidr_block
ipv6_cidr_block = module.vpc.ipv6_cidr_block
}

##------------------------------------------------------------------------------
## Other-vpc module call.
##------------------------------------------------------------------------------
module "vpc_other" {
source = "clouddrove/vpc/aws"
version = "2.0.0"
name = local.other_name
environment = local.other_environment
cidr_block = "192.168.0.0/16"
}

##------------------------------------------------------------------------------
# transit-gateway module call.
## Other-subnet module call.
##------------------------------------------------------------------------------
module "transit-gateway" {
source = "./../../"
name = "transit-gateway"
environment = "test"
module "subnets_other" {
source = "clouddrove/subnet/aws"
version = "2.0.0"
name = local.other_name
environment = local.other_environment
availability_zones = ["eu-west-2a", "eu-west-2b"]
vpc_id = module.vpc_other.vpc_id
type = "public"
igw_id = module.vpc_other.igw_id
nat_gateway_enabled = false
cidr_block = module.vpc_other.vpc_cidr_block

#Transit gateway invitation accepter
aws_ram_resource_share_accepter = false
resource_share_arn = "arn:aws:ram:eu-west-2:"
}

##------------------------------------------------------------------------------
## transit-gateway module call.
##------------------------------------------------------------------------------
module "transit-gateway" {
depends_on = [module.vpc, module.subnets]
source = "./../../"
name = local.name
environment = local.environment
tgw_create = true
amazon_side_asn = 64512
auto_accept_shared_attachments = "enable"
default_route_table_propagation = "enable"
description = "This transit Gateway create for testing purpose"
#TGW Share
resource_share_enable = true
resource_share_allow_external_principals = true
resource_share_account_ids = [""]
# VPC Attachements
vpc_attachement_create = false # Enable After once create the subnets
vpc_id = module.vpc.vpc_id
use_existing_transit_gateway_id = true
transit_gateway_id = "tgw-xxxxxxxx"
destination_cidr_block = ["192.168.0.0/16", "172.16.0.0/12"]
subnet_ids = module.subnets.public_subnet_id
vpc_attachments = {
vpc1 = {
vpc_id = module.vpc.vpc_id
subnet_ids = module.subnets.public_subnet_id
transit_gateway_default_route_table_association = false
transit_gateway_default_route_table_propagation = false
vpc_route_table_ids = module.subnets.public_route_tables_id
destination_cidr = ["30.0.0.0/16", "50.0.0.0/16"]
},
vpc2 = {
vpc_id = module.vpc_other.vpc_id
subnet_ids = module.subnets_other.public_subnet_id
transit_gateway_default_route_table_association = false
transit_gateway_default_route_table_propagation = false
vpc_route_table_ids = module.subnets_other.public_route_tables_id
destination_cidr = ["31.0.0.0/16", "53.0.0.0/16"]
} }
}

##------------------------------------------------------------------------------
## Transit-gateway module call for diff account.
##------------------------------------------------------------------------------
module "transit-gateway" {
depends_on = [module.vpc, module.subnets]
source = "./../../"
name = local.name
environment = local.environment
tgw_create = false
amazon_side_asn = 64512
auto_accept_shared_attachments = "enable"
default_route_table_propagation = "enable"
description = "This transit Gateway create for testing purpose"
#TGW Share
resource_share_enable = true
resource_share_allow_external_principals = true
resource_share_account_ids = [""]
# VPC Attachements
vpc_attachments = {
vpc1 = {
vpc_id = module.vpc.vpc_id
subnet_ids = module.subnets.public_subnet_id
transit_gateway_default_route_table_association = false
transit_gateway_default_route_table_propagation = false
vpc_route_table_ids = module.subnets.public_route_tables_id
destination_cidr = ["30.0.0.0/16", "50.0.0.0/16"]
},
vpc2 = {
vpc_id = module.vpc_other.vpc_id
subnet_ids = module.subnets_other.public_subnet_id
transit_gateway_default_route_table_association = false
transit_gateway_default_route_table_propagation = false
vpc_route_table_ids = module.subnets_other.public_route_tables_id
destination_cidr = ["31.0.0.0/16", "53.0.0.0/16"]
} }
}
108 changes: 53 additions & 55 deletions _example/single-account/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,32 @@ provider "aws" {
region = "eu-west-2"
}

locals {
name = "app"
environment = "test"
other_name = "app_1"
other_environment = "test_1"
}

##------------------------------------------------------------------------------
# VPC module call.
##------------------------------------------------------------------------------
module "vpc" {
source = "clouddrove/vpc/aws"
version = "2.0.0"

name = "vpc"
environment = "test"
source = "clouddrove/vpc/aws"
version = "2.0.0"
name = local.name
environment = local.environment
cidr_block = "10.10.0.0/16"
}

##------------------------------------------------------------------------------
# Subnets module call.
# Subnet module call.
##------------------------------------------------------------------------------
module "subnets" {
source = "clouddrove/subnet/aws"
version = "2.0.0"

name = "subnet"
environment = "test"
source = "clouddrove/subnet/aws"
version = "2.0.0"
name = local.name
environment = local.environment
availability_zones = ["eu-west-2a", "eu-west-2b"]
vpc_id = module.vpc.vpc_id
type = "public"
Expand All @@ -33,72 +38,65 @@ module "subnets" {
}

##------------------------------------------------------------------------------
# other-vpc module call.
## Other-vpc module call.
##------------------------------------------------------------------------------
module "vpc-other" {
source = "clouddrove/vpc/aws"
version = "2.0.0"

name = "vpc"
environment = "test"

cidr_block = "192.168.0.0/16"
module "vpc_other" {
source = "clouddrove/vpc/aws"
version = "2.0.0"
name = local.other_name
environment = local.other_environment
cidr_block = "192.168.0.0/16"
}

##------------------------------------------------------------------------------
# other-subnets module call.
# Other-subnet module call.
##------------------------------------------------------------------------------
module "subnets-other" {
source = "clouddrove/subnet/aws"
version = "2.0.0"

name = "subnets"
environment = "test"
module "subnets_other" {
source = "clouddrove/subnet/aws"
version = "2.0.0"
name = local.other_name
environment = local.other_environment
availability_zones = ["eu-west-2a", "eu-west-2b"]
vpc_id = module.vpc-other.vpc_id
vpc_id = module.vpc_other.vpc_id
type = "public"
igw_id = module.vpc-other.igw_id
igw_id = module.vpc_other.igw_id
nat_gateway_enabled = false
cidr_block = module.vpc-other.vpc_cidr_block
ipv6_cidr_block = module.vpc-other.ipv6_cidr_block
cidr_block = module.vpc_other.vpc_cidr_block

}

##------------------------------------------------------------------------------
## transit-gateway module call.
##------------------------------------------------------------------------------
module "transit-gateway" {
depends_on = [module.vpc, module.subnets]
source = "./../../"
name = "transit-gateway"
environment = "test"
name = local.name
environment = local.environment
tgw_create = true
amazon_side_asn = 64512
auto_accept_shared_attachments = "enable"
auto_accept_shared_attachments = "disable"
default_route_table_propagation = "enable"
description = "This transit Gateway create for testing purpose"
#TGW Share
resource_share_enable = false
resource_share_allow_external_principals = true
resource_share_account_ids = ["xxxxxxxxx"]
# VPC Attachements
vpc_attachement_create = false # Enable After once create the subnets
vpc_id = module.vpc.vpc_id
subnet_ids = module.subnets.public_subnet_id
destination_cidr_block = ["192.168.0.0/16", "172.16.0.0/12"]
}

#------------------------------------------------------------------------------
# vpc-attachement module call.
#------------------------------------------------------------------------------
module "vpc-attachement-2" {
source = "./../../"
name = "transit-gateway"
environment = "test"
# VPC Attachements
vpc_id = module.vpc-other.vpc_id
destination_cidr_block = ["10.20.0.0/16"]
vpc_attachement_create = false # Enable After once create the subnets
use_existing_transit_gateway_id = true
transit_gateway_id = module.transit-gateway.transit_gateway_id
subnet_ids = module.subnets-other.public_subnet_id
vpc_attachments = {
vpc1 = {
vpc_id = module.vpc.vpc_id
subnet_ids = module.subnets.public_subnet_id
transit_gateway_default_route_table_association = false
transit_gateway_default_route_table_propagation = false
vpc_route_table_ids = module.subnets.public_route_tables_id
destination_cidr = ["30.0.0.0/16", "50.0.0.0/16"]
},
vpc2 = {
vpc_id = module.vpc_other.vpc_id
subnet_ids = module.subnets_other.public_subnet_id
transit_gateway_default_route_table_association = false
transit_gateway_default_route_table_propagation = false
vpc_route_table_ids = module.subnets_other.public_route_tables_id
destination_cidr = ["31.0.0.0/16", "53.0.0.0/16"]
} }
}
29 changes: 15 additions & 14 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ resource "aws_ec2_transit_gateway" "main" {
## Get information on an EC2 Transit Gateway VPC Attachment.
##------------------------------------------------------------------------------
resource "aws_ec2_transit_gateway_vpc_attachment" "main" {
for_each = var.enable ? { for attach in var.vpc_attachments : attach.vpc_id => attach } : {}
transit_gateway_id = coalesce(each.value.transit_gateway_id, aws_ec2_transit_gateway.main[0].id)
for_each = var.enable ? var.vpc_attachments : {}
transit_gateway_id = var.transit_gateway_id != null ? var.transit_gateway_id : aws_ec2_transit_gateway.main[0].id
subnet_ids = each.value.subnet_ids
vpc_id = each.value.vpc_id
dns_support = lookup(each.value, "dns_support", "enable")
Expand Down Expand Up @@ -83,24 +83,25 @@ resource "aws_ram_resource_association" "main" {
resource_share_arn = aws_ram_resource_share.main[0].arn
}

data "aws_route_tables" "main" {
for_each = var.enable ? { for attach in var.vpc_attachments : attach.vpc_id => attach } : {}
vpc_id = each.value.vpc_id
locals {
vpc_route_table = flatten([
for k, v in var.vpc_attachments : [
for rtb_id in try(v.vpc_route_table_ids, []) : [for cidr in try(v.destination_cidr) : {
rtb_id = rtb_id
cidr = cidr
}
]]
])
}

##------------------------------------------------------------------------------
## Provides a resource to create a routing table entry (a route) in a VPC routing table.
##------------------------------------------------------------------------------
resource "aws_route" "main" {
# count = var.enable && var.vpc_attachement_create ? length(distinct(sort(data.aws_route_tables.main[*].ids)), ) * length(var.destination_cidr_block) : 0
for_each = var.enable ? { for attach in var.vpc_attachments : attach.vpc_id => attach } : {}
route_table_id = element(distinct(sort(data.aws_route_tables.main[0].ids)), count.index)
destination_cidr_block = element(distinct(sort(var.destination_cidr_block)), ceil(count.index / length(var.destination_cidr_block), ), )
transit_gateway_id = var.use_existing_transit_gateway_id == false ? join("", aws_ec2_transit_gateway.main[*].id) : var.transit_gateway_id
depends_on = [
data.aws_route_tables.main,
aws_ec2_transit_gateway_vpc_attachment.main,
]
count = var.enable ? length(local.vpc_route_table) : 0
route_table_id = local.vpc_route_table[count.index].rtb_id
destination_cidr_block = local.vpc_route_table[count.index].cidr
transit_gateway_id = var.transit_gateway_id != null ? var.transit_gateway_id : aws_ec2_transit_gateway.main[0].id
}

##------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit c52e4a0

Please sign in to comment.