我正在尝试创建一个Terraform项目来创建Azure订阅中所需的一切,包括资源组,虚拟网络,子网和虚拟机。
然而,当我运行一次并再次尝试时,它指出它无法删除正在使用的子网。我没有改变任何关于子网或连接到它的虚拟机。
错误:创建/更新虚拟网络:(名称"/资源组"):网络。VirtualNetworksClient#CreateOrUpdate: Failure sending request: StatusCode=400——原始错误:Code="InUseSubnetCannotBeDeleted"消息="子网构建代理正在被/subscriptions/mysub/resourceGroups/myg/providers/Microsoft使用。"Network/networkInterfaces/mynic/ipConfigurations/internal",不能删除。如果要删除子网,需要删除子网内的所有资源。看到aka.ms/deletesubnet !"详细信息= []
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块中的嵌套子网不能与显式定义的azurerm_subnet混合使用