1

I'm trying to get the route table associated with a subnet. It seems like the AWS provider is not correctly finding the subnet? I attempted to follow the documentation, but I get this error:

│ Error: query returned no results. Please change your search criteria and try again │ │ with data.aws_route_table.selected, │ on main.tf line 1, in data "aws_route_table" "selected": │ 1: data "aws_route_table" "selected" { │ 

Here is the relevant documentation I'm following:

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_table

Here is the example I used as a reference:

data "aws_route_table" "selected" { subnet_id = var.subnet_id } 

Here's what I wrote:

data "aws_route_table" "selected" { subnet_id = "subnet-1234567890abcdefg" } 

If I use the route table ID, then it works:

data "aws_route_table" "selected" { route_table_id = "rtb-1234567890abcdefg" } 

I noticed that if I do a terraform state show data.aws_route_table.selected on the working example, there is no subnet_id listed as an attribute.

3
  • 1
    Does your subnet actually have a RT associated with it? Commented Mar 19, 2022 at 4:40
  • @Marcin It did not have an explicit subnet association, but I assumed that it would still return the main route table. It appears that is not the case, however. Commented Mar 19, 2022 at 4:52
  • Yes, it must be associated. Commented Mar 19, 2022 at 5:20

2 Answers 2

1

It appears that the aws_route_table and aws_route_tables resources will only return route tables explicitly associated with the subnet. If the subnet uses the main route table, then terraform won't return that.

In my example, there was no explicit subnet association - the subnet was using the main route table. As soon as I added an explicit subnet association, the data block started working as expected.

Sign up to request clarification or add additional context in comments.

Comments

0

aws_route_table can only fetch the details in which the subnets are explicitly associated. default route table will not have explicitly associated subnets.

Screenshot of default route table

Assuming you intend to retrieve the default route table created during VPC creation, you can obtain the route table by using the VPC ID

data "aws_route_table" "default_routetable" { vpc_id = vpc-05f7cb5d09ec15853 } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.