### Simplified Module # Mandatory vpc input variable "vpc_id" { type = string } # Optional subnet input variable "subnets_ids" { type = list(string) default = [] } # Get route tables from vpc data "aws_route_tables" "vpc_rts" { vpc_id = var.vpc_id } # If provided get route tables from subnets data "aws_route_table" "subnet_rts" { count = length(var.subnets_ids) subnet_id = var.subnets_ids[count.index] } # Generate local values locals { vpc_rts_ids = data.aws_route_tables.vpc_rts.ids subnet_rts_ids = data.aws_route_table.subnet_rts[*].route_table_id rts_ids = var.subnets_ids == [] ? local.vpc_rts_ids : local.subnet_rts_ids } ### Outputs for troubleshooting # inputs output "VAR_vpc_id" { value = var.vpc_id } output "VAR_subnets_ids" { value = var.subnets_ids } # data output "DATA_vpc_rts" { value = data.aws_route_tables.vpc_rts } output "DATA_subnet_rts" { value = data.aws_route_table.subnet_rts } # locals output "LOCAL_vpc_rts_ids" { value = local.vpc_rts_ids } output "LOCAL_subnet_rts_ids" { value = local.subnet_rts_ids } # Written in full capital letters since it is the most interesting value output "LOCAL_RTS_IDS" { value = local.rts_ids }