Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

amazon web services - Terraform: 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 . . .


locals{
   route_table_ipv4  = {
     private = [
       {
         nat = module.gateways.nat_ids 
         cidrblock = "0.0.0.0/0"  
       }  
     ]
   }
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've found a solution

1 - I've changed the typo of variable to any 2 - I used join to output of nat gateways, than in resource I just splitted and counted indexes


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...