Introduction
Migrating my "azurerm_sql_server" to the "azurerm_mssql_server" resource without destroying the infrastructure.
Use Case
Procedure
The existing resource in Azure needs to be imported into the new resource definition in Terraform. Then the existing resource state in Terraform needs to be removed. The following walk-through is provided to guide you on modifying whatever additional parameters you need.
First, build the azurerm_sql_database resource:
# cat .\main.tf
provider "azurerm" {
version = "~>2.19.0"
features {}
}
resource "azurerm_resource_group" "example" {
name = "example-resources"
location = "East US"
}
resource "azurerm_sql_server" "example" {
name = "hashiexamplesqlserver"
resource_group_name = azurerm_resource_group.example.name
location = "East US"
version = "12.0"
administrator_login = "username"
administrator_login_password = "password"
}
resource "azurerm_sql_database" "example" {
name = "hashiexamplesqldatabase"
resource_group_name = azurerm_resource_group.example.name
location = "East US"
server_name = azurerm_sql_server.example.name
}
Terraform Apply -- Assuming a clean creation
Change the resource to azurerm_mssql_database and update the parameters
cat .\main.tf
provider "azurerm" {
version = "~>2.19.0"
features {}
}
resource "azurerm_resource_group" "example" {
name = "example-resources"
location = "East US"
}
resource "azurerm_sql_server" "example" {
name = "hashiexamplesqlserver"
resource_group_name = azurerm_resource_group.example.name
location = "East US"
version = "12.0"
administrator_login = "username"
administrator_login_password = "password"
}
resource "azurerm_mssql_database" "example" {
name = "hashiexamplesqldatabase"
server_id = azurerm_sql_server.example.id
}
Terraform Apply -- will show deletion. DISAPPROVE THE APPLY
# terraform apply
azurerm_resource_group.example: Refreshing state...
[id=/subscriptions/redacted/resourceGroups/example-resources]
azurerm_sql_database.example: Refreshing state...
[id=/subscriptions/redacted/resourceGroups/
example-resources/providers/Microsoft.Sql/servers/
hashexamplesqlserver/databases/hashexamplesqldatabase]
azurerm_sql_server.example: Refreshing state...
[id=/subscriptions/redacted/resourceGroups/example-resources/
providers/Microsoft.Sql/servers/hashexamplesqlserver]
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
- destroy
Terraform will perform the following actions:
# azurerm_mssql_database.example will be created
+ resource "azurerm_mssql_database" "example" {
+ auto_pause_delay_in_minutes = (known after apply)
+ collation = (known after apply)
+ create_mode = (known after apply)
+ creation_source_database_id = (known after apply)
+ id = (known after apply)
+ license_type = (known after apply)
+ max_size_gb = (known after apply)
+ min_capacity = (known after apply)
+ name = "hashexamplesqldatabase"
+ read_replica_count = (known after apply)
+ read_scale = (known after apply)
+ restore_point_in_time = (known after apply)
+ sample_name = (known after apply)
+ server_id = "/subscriptions/redacted/
resourceGroups/example-resources/providers/Microsoft.Sql/servers/hashexamplesqlserver"
+ sku_name = (known after apply)
+ zone_redundant = (known after apply)
+ threat_detection_policy {
+ disabled_alerts = (known after apply)
+ email_account_admins = (known after apply)
+ email_addresses = (known after apply)
+ retention_days = (known after apply)
+ state = (known after apply)
+ storage_account_access_key = (sensitive value)
+ storage_endpoint = (known after apply)
+ use_server_default = (known after apply)
}
}
# azurerm_sql_database.example will be destroyed
- resource "azurerm_sql_database" "example" {
- collation = "SQL_Latin1_General_CP1_CI_AS" -> null
- create_mode = "Default" -> null
- creation_date = "2020-07-31T17:54:48.453Z" -> null
- default_secondary_location = "West US" -> null
- edition = "GeneralPurpose" -> null
- id = "/subscriptions/redacted/
resourceGroups/example-resources/providers/Microsoft.Sql/servers/hashexamplesqlserver/
databases/hashexamplesqldatabase" -> null
- location = "eastus" -> null
- max_size_bytes = "34359738368" -> null
- name = "hashexamplesqldatabase" -> null
- read_scale = false -> null
- requested_service_objective_id = "f21733ad-9b9b-4d4e-a4fa-94a133c41718" ->null
- requested_service_objective_name = "GP_Gen5_2" -> null
- resource_group_name = "example-resources" -> null
- server_name = "hashexamplesqlserver" -> null
- tags = {} -> null
- zone_redundant = false -> null
- threat_detection_policy {
- disabled_alerts = [] -> null
- email_account_admins = "Disabled" -> null
- email_addresses = [] -> null
- retention_days = 0 -> null
- state = "Disabled" -> null
- use_server_default = "Disabled" -> null
}
}
Plan: 1 to add, 0 to change, 1 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value:
Apply canceled
Terraform Import -- Import the resource
# terraform import azurerm_mssql_database.example /subscriptions/redacted/
resourceGroups/example-resources/providers/Microsoft.Sql/servers/hashexamplesqlserver/
databases/hashexamplesqldatabase
azurerm_mssql_database.example: Importing from ID "/subscriptions/redacted/
resourceGroups/example-resources/providers/Microsoft.Sql/servers/hashexamplesqlserver/
databases/hashexamplesqldatabase"...
azurerm_mssql_database.example: Import prepared!
Prepared azurerm_mssql_database for import
azurerm_mssql_database.example: Refreshing state... [id=/subscriptions/redacted/
resourceGroups/example-resources/providers/Microsoft.Sql/servers/hashexamplesqlserver/
databases/hashexamplesqldatabase]
Import successful!
The imported resources are shown above. These resources are now in
your Terraform state and will subsequently be managed by Terraform.
Terraform State Remove -- Remove the old state
terraform state rm azurerm_sql_database.example
Removed azurerm_sql_database.example
Successfully removed 1 resource instance(s)
Terraform Apply - Clean
# terraform apply
azurerm_resource_group.example: Refreshing state... [id=/subscriptions/redacted/
resourceGroups/example-resources]
azurerm_sql_server.example: Refreshing state... [id=/subscriptions/redacted/
resourceGroups/example-resources/providers/Microsoft.Sql/servers/hashexamplesqlserver]
azurerm_mssql_database.example: Refreshing state... [id=/subscriptions/redacted/
resourceGroups/example-resources/providers/Microsoft.Sql/servers/hashexamplesqlserver/
databases/hashexamplesqldatabase]
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Additional Information
-
https://support.hashicorp.com/hc/en-us/articles/360061289934-How-to-Import-Resources-into-a-Remote-State-Managed-by-Terraform-Cloud