Error: Invalid value for module argument The given value is not suitable for child module variable

Error: Invalid value for module argument The given value is not suitable for child module variable

我收到以下错误:

The given value is not suitable for child module variable "subnets" defined at modules/efs/variables.tf:28,1-19: list of string required.

这就是我调用 EFS 模块的方式:

module "efs_media" {
  source          = "./modules/efs"
  namespace       = "eg"
  stage           = "test"
  name            = var.efs_names[1]
  region          = var.region
  vpc_id          = module.vpc.config.vpc_id
  subnets         = module.vpc.config.private_subnet_ids
  security_groups = [module.cluster.config.node_security_group]
}

下面是 VPC 输出文件:

output "config" {
  value = {
    vpc_id             = aws_vpc.network.id
    public_subnet_ids  = { for az, subnet in aws_subnet.public : az => subnet.id }
    private_subnet_ids = { for az, subnet in aws_subnet.private : az => subnet.id }
  }
}

这是主要的输出文件:

output "vpc_config" {
  value = module.vpc.config
}

output "iam_config" {
  value = module.iam.config
}

output "cluster_config" {
  value = module.cluster.config
}

output "odic_config" {
  value = module.cluster.odic_config
}

在您的模块输出中 private_subnet_idsaz 的映射:subnet.id。您的 efs_media 模块只需要一个子网 ID 列表,它要求一个字符串列表,如问题中的错误消息所示。

您可以使用 values function 将地图转换为其值列表。因此,在您的情况下,您可以这样调用 efs_media 模块:

module "efs_media" {
  source          = "./modules/efs"
  namespace       = "eg"
  stage           = "test"
  name            = var.efs_names[1]
  region          = var.region
  vpc_id          = module.vpc.config.vpc_id
  subnets         = values(module.vpc.config.private_subnet_ids)
  security_groups = [module.cluster.config.node_security_group]
}