Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add azure SQL user with terraform

is there a possibility to add a sql user to the azure sql via terraform? https://www.mssqltips.com/sqlservertip/5242/adding-users-to-azure-sql-databases/

Or is there a better suggestions how to create a SQL user?

Thanks

like image 305
MelleD Avatar asked Jan 02 '23 09:01

MelleD


1 Answers

Yes you can do it from Terraform if that is what you want to happen. I would use a null resource provider in Terraform to execute the commands from the box that is running Terraform. You could use PowerShell, CMD, etc. to connect to the database after it is created and create your user account. Here is an example of how to use the null resource provider.

I would image it would look something like this, in this example I am using the SqlServer PowerShell module.

resource "null_resource" "create-sql-user" {

  provisioner "local-exec" {
    command = "Add-SqlLogin -LoginName ${var.loginName} -LoginType ${var.loginType}"
    interpreter = ["PowerShell", "-Command"]
  }

  depends_on = ["azurerm_sql_database.demo"]
}

You could of course do it with our CLI tools, but this would guarantee it is part of your Terraform deployment.

like image 115
Jamie Avatar answered Jan 09 '23 08:01

Jamie