Search code examples
azureterraformazure-sql-database

Provision new SQL Azure database into existing, non-terraform managed SQL Server instance


I am starting to provision a set of new services on Azure and am using Terraform for IaC. Normally, I'd provision everything top to bottom in Terraform in nice neat modules. However, for this job I've an externally imposed restriction of deploying to a non-terraform managed SQL server managed instance. The config file will throw an error during plan of the form:

parsing "" as an Server ID: parsing Azure ID: parse "": invalid URI for request

All the docs reference a SQL server provisioned (usually directly beforehand) in the same script as the DB, I'm wondering if there's any string literal I can put there that will ref an existing server

I have tried IDs of the following format:

  • server-name
  • /server-name
  • resource-group/server-name
  • /resource-group/server-name
  • The azure object ID (guid)

Existing code for declaring the DB:

resource "azurerm_mssql_database" "xxxxxxxxxxx-dev" {
  name         = "xxxxxxxxxxx-dev"
  server_id    = "?????????"
  collation    = "SQL_Latin1_General_CP1_CI_AS"
  license_type = "LicenseIncluded"
  max_size_gb  = 2
  sku_name     = "S1"
  zone_redundant = false
  storage_account_type = "Local"
  
  # entra admin is on the server level only

  tags = {
    servicename = "xxx"
  }

  # prevent the possibility of accidental data loss
  lifecycle {
    prevent_destroy = true
  }
}

Solution

  • I'm a bit confused - you're mentioning a "non-terraform managed SQL server managed instance" but you're using the azurerm_mssql_database resource, which manages a MS SQL Database - which one do you really need?

    You can find some sample code for both below.

    Managing an Azure SQL Database and referencing a SQL Server

    You can use the fully qualified resource Id for the SQL Server, which can be retrieved from the Azure portal:

    resource "azurerm_mssql_database" "xxxxxxxxxxx-dev" {
      name         = "xxxxxxxxxxx-dev"
      server_id    = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroup/providers/Microsoft.Sql/servers/myserver"
    
      # ... other properties here
    }
    

    Or, as an alternative, use the azurerm_mssql_server data source to reference the existing SQL Server:

    data "azurerm_mssql_server" "example" {
      name                = "existingMsSqlServer"
      resource_group_name = "existingResGroup"
    }
    
    resource "azurerm_mssql_database" "xxxxxxxxxxx-dev" {
      name         = "xxxxxxxxxxx-dev"
      server_id    = data.azurerm_mssql_server.example.id
    
      # ... other properties here
    }
    

    Managing an Azure SQL Managed Database and referencing a SQL Managed Instance

    You can use the fully qualified resource Id for the SQL Managed Instance, which can be retrieved from the Azure portal:

    resource "azurerm_mssql_managed_database" "example" {
      name                = "example"
      managed_instance_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroup/providers/Microsoft.Sql/managedInstances/myserver"
    
      # other properties here
    }
    

    Or, as an alternative, use the azurerm_mssql_managed_instance data source to reference the existing SQL Managed Instance:

    data "azurerm_mssql_managed_instance" "example" {
      name                = "managedsqlinstance"
      resource_group_name = "existingResGroup"
    }
    
    resource "azurerm_mssql_managed_database" "example" {
      name                = "example"
      managed_instance_id = azurerm_mssql_managed_instance.example.id
    
      # other properties here
    }