Terraform 试图替换现有资源
Terraform trying to replace existing resource
抱歉,如果这是一个虚拟问题,但我是 terraform 的新手,我可以找到任何文档或涵盖此场景的任何内容。
我有一个 资源组 = rg1,它有一个 vnet = vnet1 和一个 subnet = subnet1.
我正在尝试在同一个 vnet (vnet1) 中创建一个新的 子网 (subnet2),terraform 出现以下错误。
~ name = "subnet1" ->
"subnet2" # forces replacement
Plan: 1 to add, 0 to change, 1 to destroy.
谁能告诉我为什么 terraform 会替换已创建的子网?有可能的解决方法吗?
编辑 1
抱歉没有包含代码,请在下面找到相同的代码;
main.tf
resource "azurerm_resource_group" "rg" {
name = var.rgname
location = var.rglocation
}
resource "azurerm_virtual_network" "vnet" {
name = var.vnetname
address_space = [var.vnet_address_space]
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}
resource "azurerm_subnet" "vnet_subnets" {
name = var.subnet_name
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = [var.subnet_prefix]
}
Tfvars
rglocation = "westus"
rgname = "rg1"
vnetname = "vnet1"
vnet_address_space = "10.0.0.0/16"
subnet_name = "subnet2"
subnet_prefix = "10.0.2.0/24"
Variable.tf
variable "rglocation" {
}
variable "rgname" {
}
variable "vnetname" {
}
variable "vnet_address_space" {
}
variable "subnet_prefix" {
}
variable "subnet_name" {
}
错误信息
Terraform will perform the following actions:
# azurerm_subnet.vnet_subnets must be replaced
-/+ resource "azurerm_subnet" "vnet_subnets" {
~ address_prefix = "10.0.1.0/24" -> (known after apply)
~ address_prefixes = [
- "10.0.1.0/24",
+ "10.0.2.0/24",
]
~ id = "" -> (known after apply)
~ name = "subnet1" -> "subnet2" # forces replacement
- service_endpoint_policy_ids = [] -> null
- service_endpoints = [] -> null
# (4 unchanged attributes hidden)
}
Plan: 1 to add, 0 to change, 1 to destroy.
发生这种情况是因为您基本上是在覆盖您的 TF 文件。因此,先前的子网会因新子网而被破坏。 re-using 不同资源的相同代码的正确方法是通过 workspaces 或在不同文件夹中完全分开不同的设置。
但在我看来,在你的情况下你想添加新的子网,所以你最后会有两个。在这种情况下,您应该使用 count or for_each。这样您将使用循环创建两个子网。
为此,您的变量应该是列表:
subnet_name = ["subnet1", "subnet2"]
subnet_prefix = ["10.0.1.0/24","10.0.2.0/24"]
然后
resource "azurerm_subnet" "vnet_subnets" {
count = length(var.subnet_name)
name = var.subnet_name[count.index]
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = [var.subnet_prefix[count.index]]
}
抱歉,如果这是一个虚拟问题,但我是 terraform 的新手,我可以找到任何文档或涵盖此场景的任何内容。
我有一个 资源组 = rg1,它有一个 vnet = vnet1 和一个 subnet = subnet1.
我正在尝试在同一个 vnet (vnet1) 中创建一个新的 子网 (subnet2),terraform 出现以下错误。
~ name = "subnet1" -> "subnet2" # forces replacement
Plan: 1 to add, 0 to change, 1 to destroy.
谁能告诉我为什么 terraform 会替换已创建的子网?有可能的解决方法吗?
编辑 1
抱歉没有包含代码,请在下面找到相同的代码;
main.tf
resource "azurerm_resource_group" "rg" {
name = var.rgname
location = var.rglocation
}
resource "azurerm_virtual_network" "vnet" {
name = var.vnetname
address_space = [var.vnet_address_space]
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}
resource "azurerm_subnet" "vnet_subnets" {
name = var.subnet_name
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = [var.subnet_prefix]
}
Tfvars
rglocation = "westus"
rgname = "rg1"
vnetname = "vnet1"
vnet_address_space = "10.0.0.0/16"
subnet_name = "subnet2"
subnet_prefix = "10.0.2.0/24"
Variable.tf
variable "rglocation" {
}
variable "rgname" {
}
variable "vnetname" {
}
variable "vnet_address_space" {
}
variable "subnet_prefix" {
}
variable "subnet_name" {
}
错误信息
Terraform will perform the following actions:
# azurerm_subnet.vnet_subnets must be replaced
-/+ resource "azurerm_subnet" "vnet_subnets" {
~ address_prefix = "10.0.1.0/24" -> (known after apply)
~ address_prefixes = [
- "10.0.1.0/24",
+ "10.0.2.0/24",
]
~ id = "" -> (known after apply)
~ name = "subnet1" -> "subnet2" # forces replacement
- service_endpoint_policy_ids = [] -> null
- service_endpoints = [] -> null
# (4 unchanged attributes hidden)
}
Plan: 1 to add, 0 to change, 1 to destroy.
发生这种情况是因为您基本上是在覆盖您的 TF 文件。因此,先前的子网会因新子网而被破坏。 re-using 不同资源的相同代码的正确方法是通过 workspaces 或在不同文件夹中完全分开不同的设置。
但在我看来,在你的情况下你想添加新的子网,所以你最后会有两个。在这种情况下,您应该使用 count or for_each。这样您将使用循环创建两个子网。
为此,您的变量应该是列表:
subnet_name = ["subnet1", "subnet2"]
subnet_prefix = ["10.0.1.0/24","10.0.2.0/24"]
然后
resource "azurerm_subnet" "vnet_subnets" {
count = length(var.subnet_name)
name = var.subnet_name[count.index]
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = [var.subnet_prefix[count.index]]
}