gpt4 book ai didi

azurerm - Terraform 的行为不符合预期

转载 作者:行者123 更新时间:2023-12-03 06:56:59 24 4
gpt4 key购买 nike

我正在尝试创建一个 Terraform 项目来创建 Azure 订阅中所需的所有内容,例如资源组、vnet、子网和 VM。

但是,当我运行一次并重试时,它指出它无法删除正在使用的子网。我没有更改有关子网或连接到子网的虚拟机的任何内容。

错误:创建/更新虚拟网络:(名称“”/资源组“”):network.VirtualNetworksClient#CreateOrUpdate:发送请求失败:StatusCode=400 -- 原始错误:代码 =“InUseSubnetCannotBeDeleted”消息 =“子网构建” -agent 正在由/subscriptions/mysub/resourceGroups/myrg/providers/Microsoft.Network/networkInterfaces/mynic/ipConfigurations/internal 使用,无法删除。要删除子网,请删除子网内的所有资源。请参阅又名.ms/删除子网。”详情=[]

terraform {
required_version = ">= 1.1.0"
backend "azurerm" {
}
required_providers {
azurerm = {
version = "=3.5.0"
source = "hashicorp/azurerm" # https://registry.terraform.io/providers/hashicorp/azurerm/latest
}
}
}

# Configure the Microsoft Azure Provider
provider "azurerm" {
features {}
}

locals {
name_suffix = "<mysuffix>"
}

resource "azurerm_resource_group" "rg-infra" {
name = "rg-${local.name_suffix}"
location = "UK South"
}

resource "azurerm_virtual_network" "vnet-mgmt" {
name = "vnet-${local.name_suffix}"
location = azurerm_resource_group.rg-infra.location
resource_group_name = azurerm_resource_group.rg-infra.name
address_space = ["<myiprange>"]

subnet {
name = "virtual-machines"
address_prefix = "<myiprange>"
}

subnet {
name = "databases"
address_prefix = "<myiprange>"
}
}

data "azurerm_virtual_network" "network" {
name = "vnet-${local.name_suffix}"
resource_group_name = azurerm_resource_group.rg-infra.name
}

resource "azurerm_subnet" "sb-ansible" {
name = "build-agent"
resource_group_name = azurerm_resource_group.rg-infra.name
virtual_network_name = data.azurerm_virtual_network.network.name
address_prefixes = ["<myiprange>"]
depends_on = [azurerm_virtual_network.vnet-mgmt]

}

data "azurerm_subnet" "prd-subnet" {
name = "build-agent"
virtual_network_name = data.azurerm_virtual_network.network.name
resource_group_name = azurerm_resource_group.rg-infra.name
depends_on = [azurerm_subnet.sb-ansible]
}


resource "azurerm_network_interface" "ni-ansible" {
name = "nic-ansible-${local.name_suffix}"
location = azurerm_resource_group.rg-infra.location
resource_group_name = azurerm_resource_group.rg-infra.name

ip_configuration {
name = "internal"
subnet_id = data.azurerm_subnet.prd-subnet.id
private_ip_address_allocation = "Dynamic"
}

lifecycle {
ignore_changes = ["ip_configuration"]
}

depends_on = [azurerm_subnet.sb-ansible]
}

resource "azurerm_linux_virtual_machine" "ansible-vm" {
name = "ansible-build-agent"
resource_group_name = azurerm_resource_group.rg-infra.name
location = azurerm_resource_group.rg-infra.location
size = "Standard_D2as_v4"
admin_username = "myadminuser"
network_interface_ids = [
azurerm_network_interface.ni-ansible.id,
]

admin_ssh_key {
username = "myadminuser"
public_key = ""
}

os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}

lifecycle {
ignore_changes = ["source_image_reference"]
}

depends_on = [azurerm_network_interface.ni-ansible]
}

任何有关其行为原因的帮助或解决方法将不胜感激!

非常感谢

最佳答案

事实证明,您无法将 vnet block 中的嵌套子网与显式定义的 azurerm_subnet 混合使用

关于azurerm - Terraform 的行为不符合预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72855116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com