拆分列表并根据 terraform 中的公共变量合并它

Split a list and merge it based in common variable in terraform

我想使用它的名称检索子网 ID,然后根据它们的 VPC id 将它们合并。

我正在使用数据源从要拆分的名称中检索子网 ID。检索子网 ID 后,如何为每个 VPC 制作子网列表。下面是我的代码。请检查和帮助。

resource.tf

resource "aws_ec2_transit_gateway_vpc_attachment" "this" {
 
  transit_gateway_id = var.transit_gateway_id
  vpc_id = var.vpc_id
  subnet_ids = var.subnet_ids

  dns_support                                     = "disable"
  ipv6_support                                    = "disable"
  transit_gateway_default_route_table_association = false
  transit_gateway_default_route_table_propagation = false

}

module.tf

我想获取子网 ID 列表,以便我可以提供 subnet_ids。

module "tgw" {
  source = "./aws_tgw"
  count = var.accounts[0].vpc_names != [""] ? length(var.accounts[0].vpc_names) : 0
  transit_gateway_id = aws_ec2_transit_gateway.this.id
  transit_gateway_arn = aws_ec2_transit_gateway.this.arn
  vpc_id = data.aws_vpc.vpc0[count.index].id
  vpc_name = var.accounts[0].vpc_subnets[count.index].vpc_name
  subnet_ids = data.aws_subnet.subnet0[var.accounts[0].vpc_subnets[count.index].vpc_name].id
  destination_cidr_block = var.destination_cidr_block_route
}

vars.tf

variable "accounts" {
  type = list(object({
    account_id = string
    vpc_names    = list(string)
    vpc_subnets = list(object({
        vpc_name = string
        subnet_names = list(string)
  })
  )
    asn = string
  }))
}

data.tf

data "aws_vpc" "vpc0" {
  count = length(var.accounts[0].vpc_names)
  filter {
    name = "tag:Name"
    values = [format("%s",var.accounts[0].vpc_names[count.index])]
  }
}
data "aws_subnet" "subnet0" {
  count = length(local.account0_subnet_list)
  filter {
    name = "tag:Name"
    values = [format("%s-%s",local.account0_subnet_list[count.index].vpc_name,local.account0_subnet_list[count.index].subnet)]
  }
}

我也试过这个但是它说 snet 是一个有 2 个字符串的元组

data "aws_subnet" "subnet0" {
  for_each = { for vpc in var.accounts[0].vpc_subnets : vpc.vpc_name => [ for snet in vpc.subnet_names : { vpc = vpc.vpc_name, subnet = snet } ] }
    filter {
    name = "tag:Name"
    values = [format("%s-%s",each.value.vpc,each.value.subnet)]
  }
}

locals.tf

locals {
  account0_subnet_list = flatten([ for vpc in var.accounts[0].vpc_subnets : [ for snet in vpc.subnet_names : { vpc_name = vpc.vpc_name, subnet = snet } ]] )
}

tfvars 文件

accounts = [
  {
    "account_id": "256869728636",
    "asn": "64513",
    "vpc_names": [
      "POC-Management1","POC-Management2"
    ],
    "vpc_subnets": [
      {
        "subnet_names": [
          "PrivateSubnet-1A-us-west-1b","PrivateSubnet-2B-us-west-1c"
        ],
        "vpc_name": "POC-Management1"
      },
      {
        "subnet_names": [
          "PublicSubnet-1A-us-west-1b","PublicSubnet-2A-us-west-1c"
        ],
        "vpc_name": "POC-Management2"
      }
    ]
  }

在资源级别而不是模块级别定义 data.tf,这样你就可以实现你想要的而不是拆分 subnet_ids。