Resource: aws_route_table

Provides a resource to create a VPC routing table.

Example Usage

Basic example

resource "aws_route_table" "example" {
  vpc_id = aws_vpc.example.id

  route {
    cidr_block = "10.0.1.0/24"
    gateway_id = aws_internet_gateway.example.id
  }

  route {
    ipv6_cidr_block        = "::/0"
    egress_only_gateway_id = aws_egress_only_internet_gateway.example.id
  }

  tags = {
    Name = "example"
  }
}

To subsequently remove all managed routes:

resource "aws_route_table" "example" {
  vpc_id = aws_vpc.example.id

  route = []

  tags = {
    Name = "example"
  }
}

Adopting an existing local route

AWS creates certain routes that the AWS provider mostly ignores. You can manage them by importing or adopting them. See Import below for information on importing. This example shows adopting a route and then updating its target.

First, adopt an existing AWS-created route:

resource "aws_vpc" "test" {
  cidr_block = "10.1.0.0/16"
}

resource "aws_route_table" "test" {
  vpc_id = aws_vpc.test.id

  # since this is exactly the route AWS will create, the route will be adopted
  route {
    cidr_block = "10.1.0.0/16"
    gateway_id = "local"
  }
}

Next, update the target of the route:

resource "aws_vpc" "test" {
  cidr_block = "10.1.0.0/16"
}

resource "aws_route_table" "test" {
  vpc_id = aws_vpc.test.id

  route {
    cidr_block           = aws_vpc.test.cidr_block
    network_interface_id = aws_network_interface.test.id
  }
}

resource "aws_subnet" "test" {
  cidr_block = "10.1.1.0/24"
  vpc_id     = aws_vpc.test.id
}

resource "aws_network_interface" "test" {
  subnet_id = aws_subnet.test.id
}

The target could then be updated again back to local.

Argument Reference

This resource supports the following arguments:

route Argument Reference

This argument is processed in attribute-as-blocks mode.

One of the following destination arguments must be supplied:

One of the following target arguments must be supplied:

Note that the default route, mapping the VPC's CIDR block to "local", is created implicitly and cannot be specified.

Attribute Reference

This resource exports the following attributes in addition to the arguments above:

Timeouts

Configuration options:

Import

In Terraform v1.5.0 and later, use an import block to import Route Tables using the route table id. For example:

import {
  to = aws_route_table.public_rt
  id = "rtb-4e616f6d69"
}

Using terraform import, import Route Tables using the route table id. For example:

% terraform import aws_route_table.public_rt rtb-4e616f6d69