Having trouble adding additional 0.0.0.0/0 route to aws_route_table

When creating a new vpc, How can you associate the additional route destination 0.0.0.0/0 and internet gateway?

1 Like

@rmattier
Below is a snippet of associating an IGW to your VPC

resource "aws_internet_gateway" "gw" {
  vpc_id = "${aws_vpc.main.id}"

  tags = {
    Name = "main"
  }
}

For the route I’d encourage you to use the aws_route resource that Terraform provides. Now it’s possible to create route tables and add routes all at the same time, building them in pieces gives you greater flexibility. An example of this is below. Hope this helps and as always let us know if you’re still requiring some assistance.

resource "aws_route" "default_route" {
  route_table_id         = "${aws_route_table.default_rtb.id}"
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = "${aws_internet_gateway.gw.id}"
  depends_on             = ["aws_route_table.your_route_table"]
}

I did as you said, When I check the route table in aws it shows the vpc cidr, but it doesn’t seem to create the additional destination cidr block 0.0.0.0/0 entry.

1 Like

Could you include a snippet of the resource you’re creating? Maybe even a little screengrab from your VPC in AWS.

resource “aws_vpc” “tf-vpc” {
cidr_block = “172.16.0.0/16”

tags = {
Name = “tf-vpc-1”
}
}

resource “aws_subnet” “tf-subnet” {
vpc_id = aws_vpc.tf-vpc.id
cidr_block = “172.16.25.0/24”

tags = {
Name = “tf-subnet-1”
}
}

resource “aws_internet_gateway” “tf-internet-gateway” {
vpc_id = aws_vpc.tf-vpc.id

tags = {
Name = “tf-internet-gw”
}
}

resource “aws_route_table” “tf-route-table” {
vpc_id = aws_vpc.tf-vpc.id
}

resource “aws_route” “tf-route” {
route_table_id = aws_route_table.tf-route-table.id
destination_cidr_block = “0.0.0.0/0”
gateway_id = aws_internet_gateway.tf-internet-gateway.id

depends_on = [ aws_route_table.tf-route-table ]
}

1 Like

Looks to me as if you’re missing the aws_route_table_association which ties the routing table to your subnet

1 Like

You are correct sir! Thank you very much! Once I added the aws_route_table_association resource, that did the trick. Thanks again!

2 Likes