【发布时间】:2021-01-03 20:28:45
【问题描述】:
我正在使用 Terraform 在 Azure 中设置一个虚拟网络。
我有几个 VNet,每个都有自己的网络安全组 100% 在 Terraform 中管理,在运行 Terraform 之前除了资源组之外没有其他资源。
当我第一次运行Terraform apply 时,所有资源都已正确创建。但是,如果我尝试再次运行 apply 来更新其他资源,我会收到一条错误消息,指出 NSG 资源已经存在。
Error: A resource with the ID
"/subscriptions/0000000000000000/resourceGroups/SynthArtInfra/providers/Microsoft.Network/networkSecurityGroups/SynthArtInfra_ServerPoolNSG"
already exists - to be managed via Terraform this resource needs to be
imported into the State. Please see the resource documentation for
"azurerm_network_security_group" for more information.
为什么 Terraform 抱怨现有资源应该已经在它的控制之下?
编辑: 这是与 NSG 相关的代码,其他一切都与 VPN 网关有关:
# Configure the Azure provider
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">= 2.26"
}
}
}
provider "azurerm" {
features {}
}
data "azurerm_resource_group" "SynthArtInfra" {
name = "SynthArtInfra"
location = "Somewhere" # not real
most_recent = true
}
resource "azurerm_virtual_network" "SynthArtInfra_ComputePool" {
name = "SynthArtInfra_ComputePool"
location = azurerm_resource_group.SynthArtInfra.location
resource_group_name = azurerm_resource_group.SynthArtInfra.name
address_space = ["10.6.0.0/16"]
}
resource "azurerm_subnet" "ComputePool_default" {
name = "ComputePool_default"
resource_group_name = azurerm_resource_group.SynthArtInfra.name
virtual_network_name = azurerm_virtual_network.SynthArtInfra_ComputePool.name
address_prefixes = ["10.6.0.0/24"]
}
resource "azurerm_network_security_group" "SynthArtInfra_ComputePoolNSG" {
name = "SynthArtInfra_ComputePoolNSG"
location = azurerm_resource_group.SynthArtInfra.location
resource_group_name = azurerm_resource_group.SynthArtInfra.name
security_rule {
name = "CustomSSH"
priority = 119
direction = "Inbound"
access = "Allow"
protocol = "*"
source_port_range = "*"
destination_port_range = "0000" # not the real port number
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
另一件奇怪的事情是我们的订阅有一个安全策略,它会自动将 NSG 添加到没有 NSG 的资源中。但奇怪的是,在应用我的 terraform 脚本后,创建了 NSG,但实际上并未与子网关联,并且安全策略创建了新的 NSG。这需要解决,但没想到会导致这个错误。
【问题讨论】:
-
你能展示你的完整代码吗?
-
能否请您编辑您的问题以包含计划输出?
-
您也可以发布政策吗?正如 ydaetskcoR 提到的,提供计划输出,但对于两次运行。听起来您的策略可能会做一些奇怪的事情,但通常导入错误意味着您的 terraform 没有状态信息并且想要创建新资源,但找到了已经创建的确切资源。
-
感谢您的意见!我弄清楚发生了什么,看看我的答案
-
我也遇到了这些错误,而且我也在 Azure 环境中。请参阅stackoverflow.com/questions/71159918/… 您是否设置了 skip_provider_registration = true ?我的公司不允许打开它。 Terraform 警告说这可能会导致问题,我想知道它是否相关。
标签: terraform terraform-provider-azure