Search code examples
terraformazure-sql-databaseinfrastructure-as-code

Why can't I use a Terraform `move` block to move an Azure SQL Database to a different resource name?


I've been able to move SQL Alerts between resource names in my terraform, for example:

moved {
  from = module.myproj_sqldbs.azurerm_monitor_metric_alert.alert_storage_percentage_exceeds_80["mydb"]
  to   = azurerm_monitor_metric_alert.alert_storage_percentage_exceeds_80    
}

The above results in a 'replace' when running a terraform plan

However, an equivalent move for my SQL DB:

moved {
  from = module.myproj_sqldbs.azurerm_mssql_database.sql_database["mydb"]
  to   = module.sqldb.azurerm_mssql_database.sql_database
}

Yields the following error:

╷
│ Error: Unsupported `moved` across resource types
│
│   on imports.tf line 10:
│   10: moved {
│
│ The provider "terraformregistry.mycompany.com/hashicorp/azurerm" does not support moved operations across resource types and providers.
╵

I thought maybe it was treating them as different resource, types, but I don't think that's the case. I.e. Without this, the plan indicates the following:

# module.sqldb.azurerm_mssql_database.sql_database will be created
......
# module.myproj_sqldbs.azurerm_mssql_database.sql_database["mydb"] will be destroyed

I've also noticed, that it's relying on the 'mycompany' registry. Could that be a thing?


Solution

  • Terraform move block to move an Azure SQL Database to a different resource name

    In general, the move is used to move the resource within the configuration from one resource to another. However, this block has some limitations, especially when dealing with custom providers or registries.

    In this case the moving operation between two SQL dBs is treating the resources are different in types and providers. This is because of using custom domain.

    • While use of custom domain there should not be any difference in the way we define the configuration and structure here in this case the monitor configuration for both SQL dBs should be on point any difference leads to the blockers.
    • Another walk around would be to creating the monitoring in the portal and simple importing the same can also be achieved.

    the file structure look something like this

    ├── main.tf
    ├── variables.tf
    ├── modules/
    │   ├── sqldb/
    │   │   ├── main.tf
    │   │   ├── variables.tf
    │   ├── myproj_sqldbs/
    │   │   ├── main.tf
    │   │   ├── variables.tf
    

    Refer:

    https://developer.hashicorp.com/terraform/language/moved

    https://developer.hashicorp.com/terraform/language/modules/develop/refactoring

    https://developer.hashicorp.com/terraform/cli/import

    azurerm_monitor_metric_alert | Resources | hashicorp/azurerm | Terraform | Terraform Registry