使用azurerm_mssql_virtual_machine部署SQL VM遇CRPNotAllowedOperation错误
使用azurerm_mssql_virtual_machine资源,基于预配置SQL Server 2016的自定义镜像构建SQL Server虚拟机,执行以下Terraform代码时出现错误:
resource "azurerm_mssql_virtual_machine" "mssql_vm" { provider = azurerm.spoke-subscription virtual_machine_id = azurerm_windows_virtual_machine.sql_server.id sql_license_type = "PAYG" sql_connectivity_port = "49535" sql_connectivity_update_username = var.sql_login sql_connectivity_update_password = var.sql_password sql_instance { collation = "Latin1_General_CI_AS" } assessment { enabled = true run_immediately = true } storage_configuration { disk_type = "${var.disk_type}" storage_workload_type = "OLTP" data_settings { default_file_path = "F:\\DATA" luns = [1] } log_settings { default_file_path = "G:\\LOGS" luns = [2] } temp_db_settings { default_file_path = "K:\\TEMPDB" luns = [3] } } lifecycle { ignore_changes = [ tags, #assessment[0].schedule ] } tags = { "application owner" = var.application_owner_tag "environment" = var.environment_tag "department" = var.department_tag "technicalcontact" = var.technicalcontact_tag "application" = var.application_tag "service" = "SQL server" } }
错误信息
performing CreateOrUpdate: sqlvirtualmachines.SqlVirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=0 --
Original Error: Code="CRPNotAllowedOperation" Message="Operation cannot be completed due to the following error: VM Extension with publisher 'Microsoft.SqlServer.Management' and type 'SqlIaaSAgent' does not support setting enableAutomaticUpgrade property to true on this subscription.
已尝试的步骤
- 将SQL Server虚拟机重新注册到Azure订阅
- 关闭
azurerm_windows_virtual_machine的自动升级
解决方案
1. 显式禁用SqlIaaSAgent扩展的自动升级
在azurerm_mssql_virtual_machine资源中添加sql_iaas_agent块,强制关闭扩展自动升级功能,这是触发错误的核心原因:
resource "azurerm_mssql_virtual_machine" "mssql_vm" { # ... 原有代码 ... sql_iaas_agent { enable_automatic_upgrade = false } # ... 原有代码 ... }
2. 检查自定义镜像中的SqlIaaSAgent状态
如果自定义镜像已预装SqlIaaSAgent扩展,确认镜像内该扩展的enableAutomaticUpgrade属性为false,避免Terraform执行时尝试修改该属性。
3. 重新注册Microsoft.SqlServer资源提供者
执行Azure CLI命令重新注册SQL Server资源提供者,确保订阅与资源提供者的交互状态正常:
az provider register --namespace Microsoft.SqlServer
等待注册完成后,重新执行Terraform部署。
内容的提问来源于stack exchange,提问作者Callum Hester

