提取子网列表会导致错误 - 元组包含 1 个元素

Jai*_*e S 4 amazon-web-services terraform terraform-provider-aws

我有一个模块,可以创建具有公共和私有子网的 VPC

module "vpc" {
  count              = var.vpc_enabled ? 1 : 0
  source             = "./vpc"
}
Run Code Online (Sandbox Code Playgroud)

作为该模块的输出,我正在提取私有子网

output "private_subnets" {
  value = aws_subnet.private.*.id
}
Run Code Online (Sandbox Code Playgroud)

然后我想使用该子网列表作为另一个模块的输入:

module "eks" {
  source          = "./eks"
  name            = var.name
  private_subnets = var.vpc_enabled ? module.vpc.private_subnets : var.private_subnets_id
}
Run Code Online (Sandbox Code Playgroud)

基本上我想要实现的是,用户可以选择是否要创建新的 VPC 或使用现有 VPC 的子网列表作为输入。

我现在遇到的问题是我在 terraform plan 中遇到以下错误:

  on main.tf line 32, in module "eks":
  32:   private_subnets = var.vpc_enabled ? module.vpc.private_subnets : var.private_subnets_id
    |----------------
    | module.vpc is tuple with 1 element

This value does not have any attributes.
Run Code Online (Sandbox Code Playgroud)

有谁知道如何解决这个问题?

Mar*_*cin 6

您正在使用 定义您的vpc模块count。因此,您需要引用该模块的各个实例,即使您只有 1 个实例。

private_subnets = var.vpc_enabled ? module.vpc[0].private_subnets : var.private_subnets_id
Run Code Online (Sandbox Code Playgroud)