Search code examples
amazon-web-servicesterraformterraform-provider-aws

Terraform how to fix attribute "route": set of object required


I have this terraform module route_table.tf I need to use. It looks like below:

resource "aws_route_table" "aws_route_table" {

  # route {
  #   cidr_block = "0.0.0.0/0"
  #   gateway_id = var.GATEWAY_ID
  # }

  route = var.ROUTE

  tags = var.ROUTE_TABLE_TAGS

  vpc_id = var.VPC_ID
}

and I have defined the variable ROUTE as below in the inputs.tf:

variable "ROUTE" {
  type = object({ cidr_block=string, gateway_id=string })
}

And I am passing those values in the main.tf as below:

module "route_tables_public" {
  source = "./modules/route_tables"

  ROUTE =    {
    cidr_block = "0.0.0.0/0"
    gateway_id = var.GATEWAY_ID
  }

  ROUTE_TABLE_TAGS = { "Name" : "mlb-rt-public" , "Project" : "mlb"}
  VPC_ID           = module.ecs_vpc.vpc_id
}

But I am getting this error:

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

Can someone help me on this?


Solution

  • Your var.ROUTE is a single object, but it should be list of objects. So you can try:

    variable "ROUTE" {
      type = list(object({ cidr_block=string, gateway_id=string }))
    }
    

    and then

    module "route_tables_public" {
      source = "./modules/route_tables"
    
      ROUTE =    [{
        cidr_block = "0.0.0.0/0"
        gateway_id = var.GATEWAY_ID
      }]
    
      ROUTE_TABLE_TAGS = { "Name" : "mlb-rt-public" , "Project" : "mlb"}
      VPC_ID           = module.ecs_vpc.vpc_id
    }
    
    

    UPDATE

    Your aws_route_table should be:

    resource "aws_route_table" "aws_route_table" {
    
     dynamic "route" {
     
       for_each = var.ROUTE
       
       content {
          cidr_block = route.value.cidr_block
          gateway_id = route.value.gateway_id
        }
      }
    
      tags = var.ROUTE_TABLE_TAGS
    
      vpc_id = var.VPC_ID
    }