reference launch config by count.index in an autoscale group

1.7k views Asked by At

I'm creating a cluster of hosts via terraform on aws and trying to utilize count to avoid creating 3 separate luanch configs & auto scale groups. I'm not having success with the auto-scale group section referencing individual launch-configs using count.index though.

Here's how I thought this would work

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]}"]
}

i get the following error when trying similar variations of the above.

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}"

If i try "${aws_launch_configuration.cluster-lc.name.[count.index]}" I get the following error.

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.'
1

There are 1 answers

0
Ben A On

I've figured it out through reading some related material (https://www.terraform.io/docs/configuration/interpolation.html#using-templates-with-count) on the hashicorp site.

The following worked for me.

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]}"]
}