我正在通过AWS上的Terraform创建主机集群,并尝试利用计数来避免创建3个独立的luanch配置和自动缩放组。我在使用scale.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.'
答案 0 :(得分:0)
我已经通过在hashicorp网站上阅读一些相关材料(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]}"]
}