betr-io / terraform-provider-mssql

Terraform provider for Microsoft SQL Server
https://registry.terraform.io/providers/betr-io/mssql/latest
MIT License
36 stars 29 forks source link

Provider doesn't handle terraform paralellism #89

Open pregress opened 5 months ago

pregress commented 5 months ago

If your create multiple mssql_users against multiple database on the same server you get a lot of errors: failures to connect, failures to sign in, timeouts.

Example errors:

context deadline exceeded

db connection failed after 30s timeout

you can bypass this by applying with parallelism set to 1, but this is not recommended. terraform apply --auto-approve -parallelism 1 https://developer.hashicorp.com/terraform/internals/graph#walking-the-graph

See example below, local.user_databases is a list with 25 values.

data "azurerm_resources" "sql_databases" {
  type = "Microsoft.Sql/servers/databases"
  resource_group_name ="resource_group"
}

locals {
  user_databases = [for db in data.azurerm_resources.sql_databases.resources : db if db.name != "servername/master"]
}

resource "mssql_user" "example" {
  for_each = { for  db in local.user_databases : db.name => db }
  server {
    host = data.azurerm_mssql_server.example.fully_qualified_domain_name
    login {
      username = "example"
      password = data.azurerm_key_vault_secret.example.value
    }
  }
  object_id = azurerm_user_assigned_identity.example.client_id
  database  = replace(each.value.name,"servername/","")
  username  = azurerm_user_assigned_identity.example.name
  roles     = ["db_reader"]
}

The problem is even worse when you connect with azuread_default_chain_auth