For_Each Append Name with unique number value

Is it possible to do the below using for_each?

Previously when creating an EC2 we would construct the Name tag value and append it with a number. Such as TESTSERVER01

When using count this allowed us to create multiple EC2’s with the number increasing by 1 each time using
var.integer_suffix + count.index
So if we defined var.interger as 01 you could construct names like the below with a count of 3:
TESTSERVER01
TESTSERVER02
TESTSERVER03

I am now looking at trying to do something similar when creating the EC2’s using for_each but have not found a way yet. Is this possible?

Hi @jprouten,

The keys for any for_each expression must be strings, and you can use those within the resource via the each value within the configuration block. Can you show what you tried and didn’t work?

For example, if the resource has a name argument, the configuration might looks something like

resource "aws_resource" "x" {
  for_each = var.objects
  name = "TESTSERVER${each.key}"
...