2

我正在尝试将两个子网列表连接到 module.vpc.public_subnets

module "vpc" {
  source = "terraform-aws-modules/vpc/aws"
  ...
  public_subnets = [
    concat(local.ext_a_subnets,local.ext_b_subnets)
  ]
  ...
}
...
locals {
  ...
  ext_a_subnets = [
    for num in range(length(local.azs)) :
    cidrsubnet(local.cidr, 8, num + 10)
  ]
  ext_b_subnets = [
    for num in range(length(local.azs)) :
    cidrsubnet(local.cidr, 8, num)
  ]
  ...
}

当我运行时terraform validate,我收到以下错误:

Error: Invalid value for module argument

  on main.tf line 45, in module "vpc":
  45:   public_subnets = [
  46:     concat(local.ext_a_subnets,local.ext_b_subnets)
  47:   ]

The given value is not suitable for child module variable "public_subnets"
defined at .terraform/modules/vpc/variables.tf:151,1-26: element 0: string
required.

但是,将 cidrsubnet for 循环直接放在 module.vpc.public_subnets 中可以正常工作:

  public_subnets = [
    for num in range(length(local.azs)) :
    cidrsubnet(local.cidr, 8, num + 10)
  ]

concat 不是我需要的功能吗?对 terraform 来说非常新,因此感谢所有帮助。

4

1 回答 1

1

concat返回一个列表。因此,假设您concat实际工作,它应该是:

module "vpc" {
  source = "terraform-aws-modules/vpc/aws"
  ...
  public_subnets = concat(local.ext_a_subnets,local.ext_b_subnets)
  ...
}
于 2021-03-31T01:34:13.260 回答