Contents

Terraform AWS_Route_Table Creation Error

Inappropriate value for attribute "route": set of object required.

Website Visitors:

When creating a route table, you might endup in inappropriate value for attribute error. In this tutorial, we’ll show you solution for the error.

Inappropriate value for attribute “route”: set of object required.

Issue

When trying to create a route table with a specific route using terraform aws_route_table resource, you might end up in the error, Inappropriate value for attribute “route”: set of object required. This is because the syntax for creating a route entry is changed in recent terraform versions.

Existing Syntax

If you’re using terraform version 1.2.1 or less, you would use code similar to what is shown below for creating a route table and a route entry.

1
2
3
4
5
6
7
resource "aws_route_table" "HomeLabRouteTable" {
  vpc_id = aws_vpc.HomeLabVPC.id
  route = {
    gateway_id = aws_internet_gateway.HomeLabIntGwy.vpc_id
    cidr_block = "0.0.0.0/0"
  }
}

If you use the above code in latest terraform versions, it will throw an error, as shown below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ terraform plan -out=Deploy.plan
 Error: Incorrect attribute value type
   on main.tf line 48, in resource "aws_route_table" "HomeLabRouteTable":
   48:   route = {
   49:     gateway_id = aws_internet_gateway.HomeLabIntGwy.vpc_id
   50:     cidr_block = "0.0.0.0/0"
   51:   }
     ├────────────────
      aws_internet_gateway.HomeLabIntGwy.vpc_id will be known only after apply
 Inappropriate value for attribute "route": set of object required.

This is because in the route block, gateway_id and cidr_block are changed in recent terraform versions. If you use vscode, from color coding you will easily understand aws_internet_gateway.HomeLabIntGwy.vpc_id will not be shown in color code but it will be in white color instead.

Solution

Instead of creating a route block and adding gateway_id and cidr_block in the same section, create a separate resource for route entry like shown below:

1
2
3
4
5
6
7
8
9
# Route table
resource "aws_route_table" "HomeLabRouteTable" {
  vpc_id = aws_vpc.HomeLabVPC.id
}
resource "aws_route" "HomeLabRoute" {
    gateway_id = aws_internet_gateway.HomeLabIntGwy.id
    route_table_id = aws_route_table.HomeLabRouteTable.id
    destination_cidr_block = "0.0.0.0/0"
}

This is tested in Terraform v1.2.5.

Suggested Article

If you’d like to go through Terraform definitions, check it out here or browse other articles on Terraform here.

Conclusion

In this tutorial, we’ve explained about solution for AWS Route Table error. We’ve also demonstrated examples for all the topics that are discussed.

All other DevOps categories are listed here: DevOps Tools. Have a look at the posts as per your topic of interest. Please feel free to share your thoughts about this article in the comments section below.

Your inbox needs more DevOps articles.

Subscribe to get our latest content by email.