locals { service_databases = {for key, val in var.services : key => val if val.database} } resource "random_password" "postgresql_service_passwords" { for_each = local.service_databases length = 32 special = false } resource "postgresql_role" "service_roles" { for_each = local.service_databases name = each.value.name login = true password = random_password.postgresql_service_passwords[each.key].result } resource "postgresql_database" "service_databases" { for_each = local.service_databases name = each.value.name owner = postgresql_role.service_roles[each.key].name }