Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random failures when creating a user soon after the database has been created #28

Open
Fresa opened this issue Feb 16, 2022 · 3 comments

Comments

@Fresa
Copy link

Fresa commented Feb 16, 2022

I'm trying to create contained database users right after a database has been created using the azurerm_mssql_database resourrce. However the mssql_user create request fails for various reasons:
Error: unable to read user [my-database-name].[my-user]: login error: mssql: Login failed for user 'admin'.
Another error:
Error: unable to create user [my-database-name].[my-user]: SQL Server had internal error

When retrying a moment later by planning and applying only the failed user terraform succeeds.

There seems to be some sort of timing issue where the database has been created but is not ready to receive any commands yet. I've seen this problem before when provisioning a local containerized instance of SQL Server and trying to connect and execute scripts right after using sqlcmd. A delay in-between seems to sort it out, but it's of course a hacky, brittle solution.

@Nopesound
Copy link

Does anyone have a solution? this problem is driving me crazy.

@Nopesound
Copy link

I'm trying to create contained database users right after a database has been created using the azurerm_mssql_database resourrce. However the mssql_user create request fails for various reasons: Error: unable to read user [my-database-name].[my-user]: login error: mssql: Login failed for user 'admin'. Another error: Error: unable to create user [my-database-name].[my-user]: SQL Server had internal error

When retrying a moment later by planning and applying only the failed user terraform succeeds.

There seems to be some sort of timing issue where the database has been created but is not ready to receive any commands yet. I've seen this problem before when provisioning a local containerized instance of SQL Server and trying to connect and execute scripts right after using sqlcmd. A delay in-between seems to sort it out, but it's of course a hacky, brittle solution.

How did you manage to create a delay? Knowing this would help me until I find a solution. I tried with the create_duration parameter but it doesn't seem to be compatible.

@Fresa
Copy link
Author

Fresa commented May 10, 2022

resource "time_sleep" "wait_for_sqldb" {
  depends_on = [azurerm_mssql_database.sqldb]

  create_duration = "30s"
}

Then you add depends_on = [time_sleep.wait_for_sqldb] to your user resources.

https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/sleep

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants