将两个模块创建的值存储在一个数组中并使用这些值 Terraform

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

我正在创建两个中转网关 vpc 附件。我试图将附件 ID 存储在一个变量中,并调用它们在 Route Table.

中创建多条路由

错误:

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]}"
}

这成功了。主要是语法错误。

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) 
}