I just been learning terraform , i have trouble using for_each for creating instances as it been recommended over using count.
If i want 10 instances with For-each do i have to write set of variables(instance_type,ami,sg) 10 times and if i alternatively use a range(10) then if i delete a instance say web-2 wont the name go void and have trouble tracking the instance count.someone recommended me to use random name generator but not sure how practical it would in a prod env.
what are recommended practise to use, do you guys separate module for each type of instances?
resource "aws_instance" "example"
{
for_each = toset(range(var.instance_count))
instance_type = var.instance_type
ami = var.ami
tags = { Name = "${var.name_prefix}-${each.value}"
}
}
variable "instance_count"
{
description = "The number of instances to create"
type = number
}
`