2

When you create an AWS VPC in terraform, it will be assigned a default route table that will route traffic just within the CIDR block of the VPC.

I want to add a default route to this to send all other traffic to the Internet.

David Purdue
  • 250
  • 4
  • 9

2 Answers2

5

This can be done by using the aws_route to add the default route to the existing VPC route table. For example:

resource "aws_vpc" "vpc" {
  cidr_block       = "${var.classb}.0.0/16"
}

resource "aws_intenet_gateway" "ig" {
  vpc_id = "${aws_vpc.vpc.id}"
}

resource "aws_route" "simulation_default_route" {
  route_table_id         = "${aws_vpc.vpc.default_route_table_id}"
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = "${aws_internet_gateway.ig.id}"
}
David Purdue
  • 250
  • 4
  • 9
1

This should also work if you prefer to use route table way.

resource "aws_default_route_table" "rtb-default" {
  default_route_table_id = aws_vpc.main.default_route_table_id
  route {
    cidr_block = "0.0.0.0/0"
    nat_gateway_id = aws_nat_gateway.ngw-main.id
  }
}
Chirag Vyas
  • 101
  • 1
  • 3
  • 1
    Hello! While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Brian61354270 Jun 25 '20 at 18:40