1

我正在通过 terraform on aws 创建一个主机集群,并尝试利用 count 来避免创建 3 个单独的 luanch 配置和自动缩放组。我没有成功使用 count.index 引用单个启动配置的自动缩放组部分。

这就是我认为这会起作用的方式

resource "aws_launch_configuration" "cluster-lc" {
  count                       = 3
  associate_public_ip_address = true
  image_id                    = "${data.aws_ami.ami.id}"
  instance_type               = "${var.instance-type}"
  security_groups             = ["${aws_security_group.sg.id}"]
  key_name                    = "kp"
  user_data                   = "${data.template_file.user_data.rendered}"
  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_autoscaling_group" "asg" {
  count                = 3
  desired_capacity     = 1
  launch_configuration = "${aws_launch_configuration.cluster-lc[count.index].name}"
  max_size             = 1
  min_size             = 1
  name                 = "asg-${count.index}"
  vpc_zone_identifier  = ["${var.subnets.[count.index]}"]
}

尝试上述类似变体时出现以下错误。

Error: Error loading /test.tf: Error reading config for aws_autoscaling_group[asg]: parse error at 1:47: expected "}" but found "."

"${aws_launch_configuration.cluster-lc.[count.index].name}" "${aws_launch_configuration.cluster-lc.[count.index]name}"

如果我尝试“${aws_launch_configuration.cluster-lc.name.[count.index]}”,我会收到以下错误。

Error: Error running plan: 1 error(s) occurred:

* aws_autoscaling_group.zoo-asg: 3 error(s) occurred:

* aws_autoscaling_group.asg[2]: Resource 'aws_launch_configuration.cluster-lc' not found for variable 'aws_launch_configuration.cluster-lc.name.'
* aws_autoscaling_group.asg[1]: Resource 'aws_launch_configuration.cluster-lc' not found for variable 'aws_launch_configuration.cluster-lc.name.'
* aws_autoscaling_group.asg[0]: Resource 'aws_launch_configuration.cluster-lc' not found for variable 'aws_launch_configuration.cluster-lc.name.'
4

1 回答 1

3

我通过阅读hashcorp 网站上的一些相关材料( https://www.terraform.io/docs/configuration/interpolation.html#using-templates-with-count )找到了答案。

以下对我有用。

resource "aws_autoscaling_group" "asg" {
  count                = 3
  desired_capacity     = 1
  launch_configuration = "${aws_launch_configuration.cluster-lc.*.name[count.index]}"
  max_size             = 1
  min_size             = 1
  name                 = "asg-${count.index}"
  vpc_zone_identifier  = ["${var.subnets.[count.index]}"]
}
于 2018-09-24T13:23:34.313 回答