1

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

halfer
  • 19,824
  • 17
  • 99
  • 186
CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • Your command is correct. Please double check if the VPC actually has non-default RTs. Maybe you provided wrong VPC id? – Marcin Dec 08 '20 at 23:33
  • @Marcin The VPC id is correct. Also, we are able to filter by tag to get the same route table. Therefore, the route table is discoverable in addition to the VPC ID being correct. But we need to be able to filter for not main. – CodeMed Dec 08 '20 at 23:58
  • Are the RTs associated with any subnets? [Docs](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-route-tables.html) write: " Route tables that do not have an association ID are not returned in the response." and also try with `aws_route_tables`, rather then `aws_route_table`. – Marcin Dec 09 '20 at 00:18

1 Answers1

2

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 
Marcin
  • 215,873
  • 14
  • 235
  • 294