azure - 无法使用 terraform 更改 azure 子网

标签 azure azure-devops terraform terraform-provider-azure

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

terraform apply -replace="azurerm_subnet.subnet1"
Terraform will perform the following actions:

         # module.network.azurerm_subnet.subnet[0] will be updated in-place
         ~ resource "azurerm_subnet" "subnet" {
         ~ address_prefixes                               = [
                - "10.0.2.0/24",
                + "10.0.4.0/24",
        ]
        id                                             = 
        "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/lab- 
        resources/providers/Microsoft.Network/virtualNetworks/acctvnet/subnets/subnet1"
        name                                           = "subnet1"
        # (7 unchanged attributes hidden)
            }

      Plan: 0 to add, 1 to change, 0 to destroy.

      Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.

我输入 yes 并收到此错误:

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=[]
│
│   with module.network.azurerm_subnet.subnet[0],
│   on .terraform/modules/network/main.tf line 15, in resource "azurerm_subnet" "subnet":
│   15: resource "azurerm_subnet" "subnet" {
│

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

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

有什么想法吗?我所做的事情是不可能的吗?

这是我的 main.tf

terraform {

  required_version = ">=0.12"
  
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "~>2.0"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "lab_autodeploy" {
  name      = "lab-resources"
  location  = "East US 2"
}

module "Windowsservers" {
  source              = "Azure/compute/azurerm"
  resource_group_name = azurerm_resource_group.lab_autodeploy.name
  is_windows_image    = true
  vm_hostname         = "new_ddc" // line can be removed if only one VM module per resource group
  size                = "Standard_F2"
  admin_password      = "$omePassw0rd"
  vm_os_simple        = "WindowsServer"
  public_ip_dns       = ["srv"] // change to a unique name per datacenter region
  vnet_subnet_id      = module.network.vnet_subnets[0]

  depends_on = [azurerm_resource_group.lab_autodeploy]
}

module "network" {
  source              = "Azure/network/azurerm"
  resource_group_name = azurerm_resource_group.lab_autodeploy.name
  subnet_prefixes     = ["10.4.0.0/24"]
  subnet_names        = ["subnet1"]

  depends_on = [azurerm_resource_group.lab_autodeploy]
}

output "windows_vm_public_name" {
  value = module.windowsservers.public_ip_dns_name
}

最佳答案

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

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

关于azure - 无法使用 terraform 更改 azure 子网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70747519/

相关文章:

docker - 是否可以在 Azure DevOps Pipeline 中运行 GitHub Workflow Actions?

azure - Powershell - 在模块中找到命令,但无法加载模块

Azure Terraform 错误 : creating/updating App Service Managed Certificate

terraform - 列出不通过 terraform 管理的资源

c# - 应用程序未调用方法

azure - 如何将 Azure Devops 库链接到 KeyVault

azure - 从 Blazor Web assembly 独立连接到受 Active Directory B2C 保护的 Azure Function App

javascript - Azure Function 不通过 context.res 返回响应

git - Azure DevOps : how to squash all commits of a PR as a patch to download for test?

openstack - Terraform,如何在现有资源上运行配置器?