Managing list of resources, ex: all databases in an elasticpool to put them in a sql_failover_group


What is the correct way of managing a list of resources?

I mean can I put resource id in a list and loop over it?
Can I set some data source, to query about some sort of list?

Here I’m facing the case that some databases are created by terraform other are created dynamically by the code.

And I want to put everything in the failover group?

resource "azurerm_sql_failover_group" "example" {
  name                = "example-failover-group"
  resource_group_name = azurerm_sql_server.primary.resource_group_name
  server_name         =
  databases           = []
  partner_servers {
    id =

  read_write_endpoint_failover_policy {
    mode          = "Automatic"
    grace_minutes = 60

How could I say?

resource "azurerm_sql_failover_group" "example" {
  # ...
  databases           = [ all databases in the elastic pool ]