This error occurs when Terraform attempts to create an Azure SQL Database but cannot find the referenced SQL Server. It typically results from resource dependency issues, incorrect server references, or timing delays in Azure resource propagation.
When creating an azurerm_mssql_database resource, Terraform needs to reference an existing azurerm_mssql_server. The ServerNotFound error indicates that either the SQL Server doesn't exist yet, has been deleted, or the server reference in your configuration is incorrect. This is often a resource ordering problem where Terraform tries to create the database before the server is fully provisioned.
Change your azurerm_mssql_database resource to reference the server using its ID instead of a hardcoded name:
# Before (incorrect)
resource "azurerm_mssql_database" "example" {
name = "mydb"
server_name = "myserver"
# This may fail if server_name is wrong or not yet created
}
# After (correct)
resource "azurerm_mssql_database" "example" {
name = "mydb"
server_id = azurerm_mssql_server.example.id
# This ensures proper dependency and uses the actual server ID
}This automatically creates an implicit dependency and uses the resource's actual ID.
If you must use server_name (not recommended), add an explicit depends_on to ensure the server is created first:
resource "azurerm_mssql_database" "example" {
name = "mydb"
server_name = azurerm_mssql_server.example.name
server_id = azurerm_mssql_server.example.id
depends_on = [
azurerm_mssql_server.example,
]
}Note: Using server_id is always preferable as it implicitly creates this dependency.
Confirm that:
1. The SQL Server resource is defined in your Terraform configuration
2. The server's resource_group_name matches the database's resource_group_name
3. Both resources are in the same Azure subscription
Check your azurerm_mssql_server resource:
resource "azurerm_mssql_server" "example" {
name = "myserver"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
version = "12.0"
administrator_login = "sqladmin"
administrator_login_password = var.sql_admin_password
}If the server was just created, Azure may need time to propagate it. Try:
terraform refresh
terraform applyIf the error persists after refresh, the server truly doesn't exist or there's a reference problem. If it succeeds on the second apply, you've identified a timing issue—consider adding a small depends_on or using timeouts.
If the database creation previously succeeded but now fails, the server may have been deleted outside Terraform:
terraform state list
terraform state show 'azurerm_mssql_server.example'If the server doesn't appear in state but you expect it to exist, either:
- Re-create the server resource
- Import the existing server: terraform import azurerm_mssql_server.example /subscriptions/{id}/resourceGroups/{group}/providers/Microsoft.Sql/servers/{server}
Ensure you're running a recent azurerm provider version (3.0+). Check your required_providers block:
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.100"
}
}
}Run terraform init to upgrade, then retry:
terraform init -upgrade
terraform planThe ServerNotFound error can sometimes mask deeper issues. If you're importing existing Azure resources, always use terraform import to link them to state before referencing them in new resources. When deploying across subscriptions, ensure your provider is configured for the correct subscription context. If using multiple workspaces, verify they're not referencing different Azure subscriptions. In CI/CD pipelines, race conditions can occur if terraform apply runs in parallel without locking; use remote state with locking to prevent this. Some users report ServerNotFound when the server name contains characters Azure rejects or when using deprecated resource types—always check that azurerm_mssql_server and azurerm_mssql_database are being used (not the legacy azurerm_sql_server or azurerm_sql_database).
Error: Error installing helm release: cannot re-use a name that is still in use
How to fix "release name in use" error in Terraform with Helm
Error: Error creating GKE Cluster: BadRequest
BadRequest error creating GKE cluster in Terraform
Error: External program failed to produce valid JSON
External program failed to produce valid JSON
Error: Unsupported argument in child module call
How to fix "Unsupported argument in child module call" in Terraform
Error: network is unreachable
How to fix "network is unreachable" in Terraform