Setting conditional value for resource parameter

I’m sure this has been answered somewhere, but I’m having trouble understanding/finding a solution.
I am iterating through a map that has hostname, ip_address and availability_set.
I want to use availability_set (which does not exist for all entries) to map to the availability set created. I am trying:
availability_set_id = each.value.availability_set != “” ? azurerm_availability_set.${each.value.availability_set}.id : null

But that is not good because of the $ after the dot. (Error: Invalid attribute name)
It tries if I quote things out, but then it doesn’t like the URI. (Error: parsing Azure ID: parse “azurerm_availability_set.test-as.id”: invalid URI for request)
If I put in azurerm_availability_set.test-as.id as the value without quoting, it can work, but then I am hard-coding a single availability set rather than mapping appropriately.

Is it possible to do such a mapping?

Thanks in advance for any insight,
Brian

I don’t think it’s possible to have a dynamic reference to a resource block.

A dynamic reference to a specific item of a for_each collection for a statically defined resource block, yes - but the name of the resource block itself needs to be static, so Terraform can build the dependency graph between resources, I believe.