amazon-web-services - 通过自动缩放组中的 count.index 引用启动配置

标签 amazon-web-services count terraform autoscaling launch-configuration

我正在 aws 上通过 terraform 创建一个主机集群,并尝试利用 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]名称}”

如果我尝试“${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.'

最佳答案

我通过阅读 hashcorp 网站上的一些相关 Material ( 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]}"]
}

关于amazon-web-services - 通过自动缩放组中的 count.index 引用启动配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52472028/

相关文章:

c++ - 使用链表的摘要报告代码中的错误

php - MySQL选择记录,统计重复项

powershell - 如何通过Powershell检索IAM实体限制和当前使用情况

java - 如何将图像流式传输到 Amazon S3

amazon-web-services - 如何查询 AWS Athena 分区表

amazon-web-services - EMR 的实例配置文件角色出错?

Terraform - 嵌入式引号

amazon-web-services - AWS Amplify CLI CloudFront S3 限制

c - 显示分数以及每个分数获得的次数

azure - 使用多级 map 在 terraform 中循环