【问题标题】:Getting Terraform resource already exists error with resource just created by Terraform获取 Terraform 资源已存在错误与 Terraform 刚刚创建的资源
【发布时间】: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


【解决方案1】:

我认为这是我第一次使用 Terraform,所以我在应用和销毁操作的中途遇到了很多错误。

我最终手动删除了 Azure 中的所有资源并删除了 Terraform 的本地缓存,然后一切都开始工作了。

【讨论】:

    猜你喜欢
    • 2021-01-22
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 2020-08-08
    • 2021-05-22
    • 2019-08-04
    • 2021-01-21
    • 1970-01-01
    相关资源
    最近更新 更多