routesterraformterraform-provider-awstransit-gateway

Store the values created by two modules in an array and use those values Terraform


I am creating two transit gateway vpc-attachments. I am trying to store the attachment IDs in one variable and call them to create multiple routes in Route Table.

Error:

Error: Invalid value for module argument

  , in module "routes":
 199:   transit_gateway_attachment_id = "${local.ec2_transit_gateway_vpc_attachment_id[count.index]}"

The given value is not suitable for child module variable
"transit_gateway_attachment_id" defined at
../modules/routes/variables.tf:25,1-41: string required.
locals {
  ec2_transit_gateway_vpc_attachment_id = [concat(module.tgw.ec2_transit_gateway_vpc_attachment_ids, module.tgw_peer.ec2_transit_gateway_vpc_attachment_ids)]
}


module "routes" {
  source   = "../modules/routes"
  count = length(local.ec2_transit_gateway_vpc_attachment_id)
  blackhole              = false
  destination_cidr_block = var.destination_cidr_block_route[count.index]

  transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.route.id

  transit_gateway_attachment_id = "${local.ec2_transit_gateway_vpc_attachment_id[count.index]}"
}

Solution

  • This worked. It was mostly syntax error.

    locals {
      ec2_transit_gateway_vpc_attachment_id = concat(module.tgw.ec2_transit_gateway_vpc_attachment_ids, module.tgw_peer.ec2_transit_gateway_vpc_attachment_ids)
    }
    
    
    module "routes" {
      source   = "../modules/routes"
     count = length(local.ec2_transit_gateway_vpc_attachment_id)
      blackhole              = false
     destination_cidr_block = var.destination_cidr_block_route[count.index]
      transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.route.id
    
     transit_gateway_attachment_id = element(local.ec2_transit_gateway_vpc_attachment_id, count.index) 
    }