地形错误“属性“ vpc_zone_identifier”的值不合适:元素0:需要字符串。

时间:2019-12-16 17:03:36

标签: terraform terraform-provider-aws

我遇到了这个错误:

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.

1 个答案:

答案 0 :(得分:2)

您的一个示例如下:

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

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

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

module块中有一个类似的表达式:

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

它也有[ ...],因此它添加了另一级列表:

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

最后,当您在自动缩放组配置中引用此名称时,我们还有一个[ ... ]表达式:

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

因此,当到达此位置时,分配给该参数的值为:

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

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

话虽如此,我认为按照您的意图进行此工作的方法是从这些表达式的 all 中删除[ ... ]列表构造括号:

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
}