How to fix Error: Error creating SQL Database: ServerNotFound in Terraform
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.
Quick fix
Fixes ServerNotFound
# 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
}- Use server_id reference instead of hardcoded server_name
- Add explicit depends_on if needed
- Verify the server exists and is in the correct resource group
- Refresh state and retry if it's a timing issue
# 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}Error: Error creating SQL Database: ServerNotFound
What this error means
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.
Typical symptoms
- Terraform apply fails with 'ServerNotFound' when creating or importing SQL databases
- Error appears after creating new SQL Server resources
- Re-running terraform apply works on the second attempt
- Database creation fails even though server exists in Azure Portal
- Cross-resource-group or cross-subscription database creation fails
- Hardcoded server names don't resolve, but references work intermittently
Common causes
- Missing or incorrect depends_on relationship between server and database resources
- Hardcoded server name that doesn't exist or is misspelled
- SQL Server hasn't finished provisioning when database creation begins (timing issue)
- Database resource references deleted or renamed server
- Server exists in different resource group than specified in database config
- Server exists in different subscription than Terraform provider context
- Race condition in parallel Terraform operations
- Stale Terraform state referencing non-existent server
How to fix "Error: Error creating SQL Database: ServerNotFound"
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 planAdvanced notes
The 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).
Frequently asked questions
- What does "ServerNotFound" mean?
- 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.
- How do I fix "ServerNotFound"?
- 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.
- What causes "ServerNotFound"?
- Missing or incorrect depends_on relationship between server and database resources Hardcoded server name that doesn't exist or is misspelled SQL Server hasn't finished provisioning when database creation begins (timing issue)
- Is the "ServerNotFound" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.