Filter to find the non-main route table in AWS VPC with Terraform

1.4k views Asked by At

What specific syntax must be used in order for the Terraform aws_route_table data source below to successfully return the route table that is NOT designated the main route table in the VPC?

data "aws_route_table" "rt" {
  vpc_id = var.vpcId

  filter {
    name   = "association.main"
    values = [false]
  }

}

There is only one non-main route table in the VPC. Therefore, filtering for main=false should identify it if the filter syntax is correct.

The error currently produced by the above code is:

Error: Your query returned no results. Please change your search criteria and try again

1

There are 1 answers

0
Marcin On BEST ANSWER

I did some tests on my own, and here are some of my findings. aws_route_tables rather then aws_route_table should be used to return multiple RTs:

data "aws_route_tables" "rt" {
  vpc_id = var.vpcId

  filter {
    name   = "association.main"
    values = [false]
  }
}

However, there are few things to know about:

  1. RTs that are not associated with any subnet will not be returned.
  2. If Main route table is associated with a subnet, it will be returned nevertheless. Basicily if a main route table is associated with a subnet it will be considered as both main and not main at the same time.

So basically, the usefulness of the above filter highly depends on how your VPC and RTs are organized.

Below is AWS CLI that I also used to double check some of these findings:

aws ec2 describe-route-tables --filters Name=vpc-id,Values=vpc-0a347b77b8c0109b6 Name=association.main,Values=false