hhh0505
hhh0505

Reputation: 357

Terraform using count.index in a resource name

using terraform i'm trying to include the count in the name of my resource using count.index, but unable to get the count to display. I'm basically looking to add the count to the resource name so that the resource can be found, otherwise the resource is unknown.

count = 3    
autoscaling_group_name = "${aws_autoscaling_group.exampleautoscaling-("count.index")-example.name}"

ERROR

resource variables must be three parts: TYPE.NAME.ATTR in:

expected is : exampleautoscaling-1-example.name,exampleautoscaling-2-example.name,exampleautoscaling-3-example.name

Upvotes: 7

Views: 26641

Answers (2)

Jamie
Jamie

Reputation: 3347

Your syntax is incorrect. You are trying to insert count into the middle of a resource name. You need to change it to be the following:

count = 3    
autoscaling_group_name = "${aws_autoscaling_group.exampleautoscaling.name}-${count.index}"

Upvotes: 2

Sorabh Mendiratta
Sorabh Mendiratta

Reputation: 951

My suggestion will be to add tags and use name_prefix arguments. But specific to your question

Here are some snippets from the documentation what you can try

"${var.hostnames[count.index]}"

OR

resource "aws_instance" "web" {
  # ...

  count = "${var.count}"

  # Tag the instance with a counter starting at 1, ie. web-001
  tags {
    Name = "${format("web-%03d", count.index + 1)}"
  }
}

Providing the link here. Look under the Math section.

Upvotes: 11

Related Questions