0

I'm unable to fetch the default route table id associated with the private subnet.

I get the following error. This seems to be reported on github. I'm not sure how to get the route table ID for private subnets.

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

One workaround I tried was to produce a list of VPC subnets using the aws_subnet_ids Data Source, and then discover all route table ids by using the aws_route_table Data Source with count and subnet_id as a parameter for each iteration, but this does not always work, because if there are one or more subnets that are (non-explictly) associated with the Main route table, TF will error as follows:

data.tf:

data "aws_route_table" "private_subnet_RT" { depends_on = [ aws_subnet.private_subnet ] count = length(var.availability_zones) # subnet_id = "${element(aws_subnet.private_subnet.*.id, count.index)}" subnet_id = "subnet-01bae78f452ca0000" } 

subnets.tf

# Create Private Subnets resource "aws_subnet" "private_subnet" { count = length(var.availability_zones) vpc_id = var.vpc_id cidr_block = var.private_subnet_cidr[count.index] availability_zone = "${element(var.availability_zones, count.index)}" tags = merge({ Name = "Private_subnet_${count.index} - ${var.environment}" }, var.private_subnet_tags) } 

enter image description here

Though the route table doesn't show that its associated with a subnet..

enter image description here

2 Answers 2

1

I have a feeling that a filter should do well for you:

data "aws_route_table" "selected" { vpc_id = var.vpc_id filter { name = "association.main" values = ["true"] } } 

If you interested in seeing more filters please navigate to official aws resource -> https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeRouteTables.html

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

Comments

1

If this could help anyone.

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

this would only return route table id of subnet which has explicit association with the route table. Meaning if the subnet not associated with any route table and ends up being a part of the Main route table, will not be returned by this until you make that association explicit by associating that subnet with Main route table explicitly

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.