Dynamic route table routes, could not retrieve elements from list of string

I’ve faced an issue while I was converting route to dynamic route.
By default I’ve 3 Availability zones, and each zone has own nat gateway, when I’m trying to use those nat gateways I received a different type of errors.

Here is the output of nat.tf (Returns > list[string])

output "nat_ids" {
   description = "The id of created nat gateway"
   value       = aws_nat_gateway.nat.*.id

Here is the block of aws_route_table

resource "aws_route_table" "route"   {
   count                             = length(var.subnets)
   vpc_id                            = var.vpc_id

   # Route Table For IPv4
   dynamic "route" {
       for_each                      = var.route_table_ipv4
       content {
         cidr_block                  = lookup(route.value, "cidrblock", "" )
         gateway_id                  = lookup(route.value, "igw",  "" )
         instance_id                 = lookup(route.value, "instance",  "" )
         nat_gateway_id              = compact(split(",", lookup(route.value, "nat",   "")))
         vpc_endpoint_id             = lookup(route.value, "vpc_endpoint",  "" )
         local_gateway_id            = lookup(route.value, "local_gateway", "" )
         transit_gateway_id          = lookup(route.value, "transit_gateway", "" )
         network_interface_id        = lookup(route.value, "network_interface", "" )
         vpc_peering_connection_id   = lookup(route.value, "vpc_peering", "" )
   tags = var.map_tags

Here is the error message:

Error: Invalid value for module argument

  on main.tf line 211, in module "private_routing":
 211:    route_table_ipv4              = local.route_table_ipv4.private

The given value is not suitable for child module variable "route_table_ipv4"
defined at ../Resources/Network/Routings/variables.tf:107,1-28: element 0:
element "nat": string required.

Error: Incorrect attribute value type

  on ../Resources/Network/Routings/main.tf line 28, in resource "aws_route_table" "route":
  28:          nat_gateway_id              = compact(split(",", lookup(route.value,   "nat",   "")))

Inappropriate value for attribute "nat_gateway_id": string required.

What is my problem? What I’m doing wrong?
I’ve tried many options like

element(concat(lookup(route.value, "nat", "" ), [""]), count.index)

element(lookup(route.value, "nat"), count.index)

compact(split(",", lookup(route.value, "nat", "")))

And here is the variable, and declaration way in main module

variable "route_table_ipv4"    {
   description = "The list of routes for IPv4 'CIDR' block(s)"
   type        = list(map(string))
   default     = null

. . . other modules . . .

route_table_ipv4 = local.route_table_ipv4.private

. . . other modules . . .

   route_table_ipv4  = {
     private = [
         nat = module.gateways.nat_ids 
         cidrblock = ""  

I hope my post will be helpfull also to others, who have faced the same issue, and I hope it is not too long, I tried my best to minimize the code. Looking forward for your comments and answers. There is no issue with internet gateway in case if you wander that I made a typo mistake

try type = any and convert it if tf cannot gather the type.

1 Like

Hello, @tbugfinder :blush:, I’m really appreciate your help, I owe you one. Let me try, I hope the lookup will not complain about it will need only identical types


At least different error

| count.index is 2
| route.value is object with 2 attributes

Call to function "element" failed: cannot read elements from string.

Unfortunately I could not manage it, it seems i’m missing some syntax hack

I’ve used this options

element(concat(lookup(route.value, "nat", "" ), [""]), count.index)

element(lookup(route.value, "nat"), count.index)

compact(split(",", lookup(route.value, "nat", "")))

@tbugfinder, I found a solution, thank you for your support

Here how I did:

For module
join(",", module.gateways.nat_ids)

For problematic area
element(concat(compact(split(",", lookup(route.value, "nat", ""))), [""]), count.index)