Terraform,将几个子网传递给自动缩放组

Terraform, passing few subnets to autosecaling group

定义子网的代码如下:

resource "aws_subnet" "public" {
  count = length(data.aws_availability_zones.available.names)

  vpc_id                  = aws_vpc.main_vpc.id
  cidr_block              = cidrsubnet(var.vpc_cidr, 8, count.index)
  availability_zone       = element(data.aws_availability_zones.available.names, count.index)
  map_public_ip_on_launch = true

  tags = {
    Name = "${var.environment}-public-${element(data.aws_availability_zones.available.names, count.index)}"
  }
}

稍后有定义自动缩放组的代码:

resource "aws_autoscaling_group" "ec2_testing" {
  availability_zones  = [data.aws_availability_zones.available.names[0]]
  vpc_zone_identifier = [element(aws_subnet.public.*id, count.index),]
  desired_capacity    = 1
  max_size            = 1
  min_size            = 1

  launch_template {
    id      = aws_launch_template.ec2_testing.id
    version = "$Latest"
  }
}

我遇到错误:

A comma is required to separate each function argument from the next.

编辑:只需要分配一个子网。

vpc_zone_identifier应该如何纠正?请帮忙

就像 ydaetskcoR 提到的那样,应该采取两种行动:

aws_subnet.public.*id should be aws_subnet.public.*.id. Also you shouldn't be specifying the availability_zones parameter if you are also specifying the vpc_zone_identifier as it's unnecessary and will complicate logic (your current example will fail because those subnets aren't all in the same AZ)

And you either need count on that aws_autoscaling_group to be able to use count.index or you need to just use aws_subnet.public.*.id[0] similarly to how you are using the first AZ in the list of zones in the above parameter.

为了可见性添加这个。