Add routes to multiple routing tables?

Dear Colleagues, I’m trying to create a peering connection and mutual routes between two existing VPCs. Adding mutual routes to the main routing table of each VPC is easy (showing only one direction for brevity):

resource "aws_route" "vpc1vpc2" {
  route_table_id            = data.aws_vpc.test1.main_route_table_id
  destination_cidr_block    = data.aws_vpc.test2.cidr_block
  vpc_peering_connection_id = aws_vpc_peering_connection.foo.id
}

the problem is that each VPC has several route tables associated with different subnets, and they are not managed by Terraform. Can you please help me with an example of how to iterate over all existing route tables adding a route to the other VPC to each of them?

What you wrote looks good. You just need to create aws_route resources with the correct details. You can use the aws_route_tables data source to find route tables by tag and then use for_each to loop through them.

Thank you a lot @stuart-c for the hint about the aws_route_tables data source, this was the key thing.

I’ve come up with this code and it works:

resource "aws_route" "vpc1vpc2" {
  for_each                  = toset(data.aws_route_tables.vpc1.ids)
  route_table_id            = each.value
  destination_cidr_block    = data.aws_vpc.test2.cidr_block
  vpc_peering_connection_id = aws_vpc_peering_connection.foo.id
}

resource "aws_route" "vpc2vpc1" {
  for_each                  = toset(data.aws_route_tables.vpc2.ids)
  route_table_id            = each.value
  destination_cidr_block    = data.aws_vpc.test1.cidr_block
  vpc_peering_connection_id = aws_vpc_peering_connection.foo.id
}

@stuart-c sorry to trouble you, a quick question. I cannot find a syntax description of the filter clause, so what would be the syntax to say that the tag KubernetesCluster should be present with any value?

data "aws_route_tables" "test2" {
  vpc_id = data.aws_vpc.test2.id
  filter {
    name   = "tag:KubernetesCluster"
    values = ????????
  }
}


The filter docs can be found here: DescribeRouteTables - Amazon Elastic Compute Cloud

For a tag to exist without caring for the value I think it would be name = "tag-KubernetesCluster"

The filter docs can be found here: DescribeRouteTables - Amazon Elastic Compute Cloud

Ah, these are actually AWS CLI filters, not Terraform filters? No wonder I could not find their description in Terraform docs. Thank you!

Nope, as it turns out.

This code:

data "aws_route_tables" "test2" {
  vpc_id = data.aws_vpc.test2.id
  filter {
    name   = "tag-KubernetesCluster"
  }
}

produces the error

╷
│ Error: Missing required argument
│ 
│   on data.tf line 15, in data "aws_route_tables" "test2":
│   15:   filter {
│ 
│ The argument "values" is required, but no definition was found.
╵

Just put values = []

Nope:

Error: reading EC2 Route Tables: InvalidParameterValue: The filter ‘tag-KubernetesCluster’ is invalid

Code:

data "aws_route_tables" "prod2" {
  vpc_id = data.aws_vpc.prod2.id
  filter {
    name   = "tag-KubernetesCluster"
    values = []
  }
}

Maybe the Terraform filter does not use AWS syntax after all?