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
How to use Terraform Count. We use count to deploy multiple resources. The count meta-argument generates numerous resources of a module. You use the count argument within the block and assign a whole number (could by variable or expression), and Terraform generates the number of resources.
When count is set, Terraform distinguishes between the block itself and the multiple resource or module instances associated with it. Instances are identified by an index number, starting with 0 . <TYPE>. <NAME> or module.
What is count in Terraform? When you define a resource block in Terraform, by default, this specifies one resource that will be created. To manage several of the same resources, you can use either count or for_each , which removes the need to write a separate block of code for each one.
you can use terraform built-in function length() to get the count.
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.
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}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With