无法使用 terraform 更改 azure 子网

0 azure terraform azure-devops terraform-provider-azure

我是 terraform 新手,想要更改网络上的子网,但遇到了一个奇怪的错误。\ngoogle 什么也没得到。这是我要输入的内容(更改 main.tf 和运行计划后)

\n
terraform apply -replace="azurerm_subnet.subnet1"\nTerraform will perform the following actions:\n\n         # module.network.azurerm_subnet.subnet[0] will be updated in-place\n         ~ resource "azurerm_subnet" "subnet" {\n         ~ address_prefixes                               = [\n                - "10.0.2.0/24",\n                + "10.0.4.0/24",\n        ]\n        id                                             = \n        "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/lab- \n        resources/providers/Microsoft.Network/virtualNetworks/acctvnet/subnets/subnet1"\n        name                                           = "subnet1"\n        # (7 unchanged attributes hidden)\n            }\n\n      Plan: 0 to add, 1 to change, 0 to destroy.\n\n      Do you want to perform these actions?\n      Terraform will perform the actions described above.\n      Only 'yes' will be accepted to approve.\n\n
Run Code Online (Sandbox Code Playgroud)\n

我输入 yes 并收到此错误:

\n
Error: updating Subnet: (Name "subnet1" / Virtual Network Name "acctvnet" / Resource Group "lab-resources"): network.SubnetsClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="InUseSubnetCannotBeUpdated" Message="Subnet subnet1 is in use and cannot be updated." Details=[]\n\xe2\x94\x82\n\xe2\x94\x82   with module.network.azurerm_subnet.subnet[0],\n\xe2\x94\x82   on .terraform/modules/network/main.tf line 15, in resource "azurerm_subnet" "subnet":\n\xe2\x94\x82   15: resource "azurerm_subnet" "subnet" {\n\xe2\x94\x82\n
Run Code Online (Sandbox Code Playgroud)\n

虚拟机已关闭,我看不出还有什么可以使用它。

\n

我还尝试使用 terraform 污点“azurerm_subnet.subnet1”

\n

有任何想法吗?我所做的事情是不可能的吗?

\n

这是我的 main.tf

\n
terraform {\n\n  required_version = ">=0.12"\n  \n  required_providers {\n    azurerm = {\n      source = "hashicorp/azurerm"\n      version = "~>2.0"\n    }\n  }\n}\n\nprovider "azurerm" {\n  features {}\n}\n\nresource "azurerm_resource_group" "lab_autodeploy" {\n  name      = "lab-resources"\n  location  = "East US 2"\n}\n\nmodule "Windowsservers" {\n  source              = "Azure/compute/azurerm"\n  resource_group_name = azurerm_resource_group.lab_autodeploy.name\n  is_windows_image    = true\n  vm_hostname         = "new_ddc" // line can be removed if only one VM module per resource group\n  size                = "Standard_F2"\n  admin_password      = "$omePassw0rd"\n  vm_os_simple        = "WindowsServer"\n  public_ip_dns       = ["srv"] // change to a unique name per datacenter region\n  vnet_subnet_id      = module.network.vnet_subnets[0]\n\n  depends_on = [azurerm_resource_group.lab_autodeploy]\n}\n\nmodule "network" {\n  source              = "Azure/network/azurerm"\n  resource_group_name = azurerm_resource_group.lab_autodeploy.name\n  subnet_prefixes     = ["10.4.0.0/24"]\n  subnet_names        = ["subnet1"]\n\n  depends_on = [azurerm_resource_group.lab_autodeploy]\n}\n\noutput "windows_vm_public_name" {\n  value = module.windowsservers.public_ip_dns_name\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Chr*_*own 5

这不是 Terraform 特有的问题 - 在 Azure 中,您无法更改附加了内容的子网。虚拟机关闭的事实并没有什么区别。

要在不破坏 VM 的情况下解决此问题,您可以将 NIC 移至其他子网(如有必要,创建临时子网),执行地址空间更改,然后将 NIC 移回。