3

I'm struggling to understand the difference between the following Terraform Resources:

Example 1:

resource "aws_route_table" "public_1" {
  vpc_id = aws_vpc.test.id
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.main.id
  }
  tags = {
    Name = "test-route"
  }
}
resource "aws_route_table_association" "test_pub_route" {
  subnet_id      = aws_subnet.test_pub.id
  route_table_id = aws_route_table.public_1.id
}

Correct me if I'm wrong but in this example I'm just creating a route and a route table with public_1 resource.
In the second part I'm just associating the public_1 route table subnet called test_pub.

Question 1: I don't understand what does the block route do in public_1 The documentation here doesn't make much sense to me.

Example 2:

resource "aws_route_table" "public" {
  vpc_id = aws_vpc.main.id
}
resource "aws_route" "public" {
  route_table_id         = aws_route_table.public.id
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = aws_internet_gateway.main.id
}
resource "aws_route_table_association" "public" {
  subnet_id      = aws_subnet.test_pub.id
  route_table_id = aws_route_table.public.id
}

Question 2(main) I'm trying to understand if these two examples are actually the same. And that the only difference is that in the first example we have route block which is equivalent to resource aws_route in the second example.
Even the required variables are the same just name is little bit different.

1
  • 1
    Through testing i have confirmed that resutls are the same.
    – Shaddar
    Jul 8, 2022 at 11:48

1 Answer 1

2

These are two ways of accomplishing the same thing with limitations.

Note that you cannot use both methods for the same table. In other words if you create routes in the aws_route_table you cannot associate routes created with aws_route

Terraform currently provides both a standalone Route resource and a Route Table resource with routes defined in-line. At this time you cannot use a Route Table with in-line routes in conjunction with any Route resources. Doing so will cause a conflict of rule settings and will overwrite rules.

Q1. The route block in aws_route_table is creating a route inside the routing table without the need to create a separate route using aws_route.

Q2. Yes, they accomplish the same result.

1
  • That answers my question! Thank you @kenlukas
    – Shaddar
    Jul 8, 2022 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.