Variable reference in resource id

I’m using a for_each statement in a resource like:
resource "my_resource_type" "my_resource_name"
that creates several resources with names formatted like:
my_resource.my_resource_name["${each.value}-2"]
…resulting with values like:

resourceA-1
resourceA-2
resourceA-3
resourceB-1
resourceB-2
resourceB-3

I want to use another loop in another resource to reference these resource ids.

ids = [
resourceA-1.id
resourceA-2.id
resourceA-3.id
]

It seems as simple as:

my_resource_type.my_resource_name.${each.value}-1.id
my_resource_type.my_resource_name.${each.value}-2.id
my_resource_type.my_resource_name.${each.value}-3.id

But many variations on this with quoting don’t seem to pass my linter or otherwise fail at plan time with some syntax error.

Is there a way to reference the resource like this?

I found my answer… since the resources created are in list format, I can do exactly as I was expecting:

  monitor_ids = [
    my_resource.my_resource_name["${each.value.name}-1"].id,
    my_resource.my_resource_name["${each.value.name}-2"].id,
    my_resource.my_resource_name["${each.value.name}-3"].id,
  ]

Hi @mlindes,

If you want to generalize that to any number of resource instances, you can alternatively write this as a for expression:

  monitor_ids = [
    for v in y_resource.my_resource_name : v.id
  ]

This is a generalized version of what you shared which will work for any number of elements in my_resource.my_resource_name.