【问题标题】:Terraform error `Inappropriate value for attribute "vpc_zone_identifier": element 0: string required.`Terraform 错误`属性“vpc_zone_identifier”的值不合适:元素 0:需要字符串。`
【发布时间】:2020-04-09 04:29:44
【问题描述】:

我遇到了这个错误:

Inappropriate value for attribute "vpc_zone_identifier": element 0: string required.

变量应该是字符串列表,所以元素 0 应该是字符串。

代码如下:

VPC 模块:

resource "aws_subnet" "terraform-pub-sn" {
  count                   = "${length(data.aws_availability_zones.all.names)}"
  vpc_id                  = "${aws_vpc.terraform-vpc.id}"
  cidr_block              = "${element(var.vpc_subnet_cidr, count.index)}"
   availability_zone       = "${data.aws_availability_zones.all.names[count.index]}"
    }

输出:

output "terraform_subnet_ids" {
  value = ["${aws_subnet.terraform-pub-sn.*.id}"]
} 

Main.tf:

module "auto_scaling_group" {
  source = "./modules/AutoScalingGroup"
  terraform_subnet_ids = ["${module.vpc.terraform_subnet_ids}"]
}

ASG 模块:

variable "terraform_subnet_ids"{}

resource "aws_autoscaling_group" "terraform-asg" {
  vpc_zone_identifier  = ["${var.terraform_subnet_ids}"]

  ...
}

我花了半天时间试图解决这个问题,不知道还要尝试什么以及应该如何定义它。 AFAIK 添加 [] 将使变量成为字符串列表,当它选择元素 0 并返回错误时,该元素在技术上应该是一个字符串,所以不知道问题是什么。也许有一种方法可以即时检查它是什么?

完整的错误在这里:

Error: Incorrect attribute value type

  on modules\AutoScalingGroup\asg.tf line 43, in resource "aws_autoscaling_group" "terraform-asg":
  43:   vpc_zone_identifier  = ["${var.terraform_subnet_ids}"]

Inappropriate value for attribute "vpc_zone_identifier": element 0: string
required.

【问题讨论】:

  • 这是 Terraform 0.12 吗?

标签: terraform terraform-provider-aws


【解决方案1】:

你的一个例子如下:

output "terraform_subnet_ids" {
  value = ["${aws_subnet.terraform-pub-sn.*.id}"]
}

这包括两个操作:aws_subnet.terraform-pub-sn.*.idreturns a list of ids,然后是[ ... ]constructs a list from its contents。所以这个表达式正在构造一个列表列表,看起来像这样:

[
  ["subnet-abc123", "subnet-123abc"]
]

moduleblock 中有一个类似的表达式:

  terraform_subnet_ids = ["${module.vpc.terraform_subnet_ids}"]

这也有[ ...],所以它添加了另一个级别的列表:

[
  [
    ["subnet-abc123", "subnet-123abc"]
  ]
]

最后当你在自动伸缩组配置中提到这个时,我们又多了一个[ ... ] 表达式:

  vpc_zone_identifier  = ["${var.terraform_subnet_ids}"]

所以到这里的时候,分配给这个参数的值是:

[
  [
    [
      ["subnet-abc123", "subnet-123abc"]
    ]
  ]
]

此列表的元素零是字符串列表的列表,因此 Terraform 报告类型错误。

综上所述,我认为按照您的意图完成这项工作的方法是从这些表达式的所有中删除[ ... ]列表构造括号:

output "terraform_subnet_ids" {
  # A list of subnet ids
  value = aws_subnet.terraform-pub-sn.*.id
}
module "auto_scaling_group" {
  source = "./modules/AutoScalingGroup"

  # still a list of subject ids
  terraform_subnet_ids = module.vpc.terraform_subnet_ids
}
variable "terraform_subnet_ids" {
  # Setting an explicit type for your variable can be helpful to
  # catch this sort of problem at the caller, rather than in
  # the usage below. I used set(string) rather than list(string)
  # here because vpc_zone_identifier is an unordered set of subnet
  # ids; list(string) would work too, since Terraform will convert
  # to a set just in time to assign to vpc_zone_identifier.
  type = set(string)
}

resource "aws_autoscaling_group" "terraform-asg" {
  # because of the type declaration above, this is now a set
  # of strings, which is the type this argument is expecting.
  vpc_zone_identifier  = var.terraform_subnet_ids
}

【讨论】:

  • 感谢您的详尽解释,我删除了所有方括号,它完美运行。
猜你喜欢
  • 2021-07-17
  • 1970-01-01
  • 2021-06-23
  • 2021-11-27
  • 2020-08-09
  • 2021-03-26
  • 1970-01-01
  • 2019-12-28
  • 2021-02-09
相关资源
最近更新 更多