amazon-web-servicesterraformterraform-provider-awsroutetable

terraform aws - how to add default route to vpc default route table?


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.


Solution

  • 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}"
    }