【问题标题】:Why is the Network Watcher on Azure not destroyed by Terraform?为什么 Azure 上的 Network Watcher 没有被 Terraform 销毁?
【发布时间】:2022-06-13 03:33:51
【问题描述】:

我有一个简单的 Terraform 配置来创建 azure 虚拟网络。当我执行 plan 然后 apply 时,会按预期在资源组内创建一个虚拟网络。但除了这个资源组之外,还有一个名为 NetworkWatcherRG 的资源组,在其中我看到了一个网络观察者。

还有网络观察者。

现在,当我运行 Terraform destroy 命令时,我希望所有东西都被清理干净,所有资源组都被销毁。但是,除了 NetworkWatcherRG 和其中的 Network Watcher 之外的所有内容都被销毁

看起来 Network Watcher 及其资源组不受 Terraform 管理。我错过了什么?

网络观察者不是很明显。它没有立即陶醉。所以要看到这一点,你需要去simplified view of the resource groups。您需要至少点击刷新按钮 5 次(每次有 2 秒的时间间隔),否则您必须等待很长时间才能点击刷新。

那么这个网络观察者是什么?是 Azure 自己创建它而不是由 Terraform 管理的吗?

我的 Terraform 配置文件如下。


# Terraform settings Block
terraform {
  required_version = ">= 1.0.0"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">= 2.0"
    }
  }
}

# Provider Block
provider "azurerm" {
  features {}
}

# create virtual network
resource "azurerm_virtual_network" "myvnet" {
  name                = "vivek-1-vnet"
  address_space       = ["10.0.0.0/16"] # This is a list, it has []. If it has { }, then its a map.
  location            = azurerm_resource_group.myrg.location
  resource_group_name = azurerm_resource_group.myrg.name
  tags = { # This is a map. This is {}
    "name" = "vivek-1-vnet"
  }
}

# Resource-1: Azure Resource Group
resource "azurerm_resource_group" "myrg" {
  name     = "vivek-vnet-rg"
  location = var.resource_group_location
}


variable "resource_group_location" {
  default     = "centralindia"
  description = "Location of the resource group."
}


最后我使用的命令如下。


terraform fmt

terraform init

terraform validate

terraform plan -out main.tfplan

terraform apply main.tfplan

terraform plan -destroy -out main.destroy.tfplan

terraform apply main.destroy.tfplan

【问题讨论】:

    标签: terraform-provider-azure


    【解决方案1】:

    在应用 terraform code 之前,我为我签入了名为 network watcher resource group 的资源组,默认情况下,此资源组由 Azure 端创建。

    Network Watcher 资源位于隐藏的 NetworkWatcherRG 自动创建的资源组。例如,NSG Flow Logs 资源是 Network Watcher 的子资源,是 在 NetworkWatcherRG 中启用。

    Network Watcher 资源代表后端服务 网络观察程序,完全由 Azure 管理。客户无需 管理它。资源不支持移动等操作。 但是,the resource can be deleted

    所以 terraform destroy 只会删除你创建的资源(在.tfstate文件中提到)。这是你无法删除NetworkWatcherRG资源组的区域。

    参考:https://docs.microsoft.com/en-us/answers/questions/27211/what-is-the-networkwatcherrg.html

    【讨论】:

      【解决方案2】:

      我阅读了@RahulKumarShaw-MT 的回复。我相信答案,并且 terraform 不会破坏它没有创建的资源是完全有道理的(除非有人可以证明不是这样)。也就是说,我能够使用 terraform 删除 NetworkWatcherRG 组!为了实现这一点,我确保使用 azurerm_network_watcher(请参阅https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_watcher)添加一个网络观察程序作为我声明的资源之一。我将资源组命名为我想要的任何名称;不必是“NetworkWatcherRG”。我观察到使用 Terraform 成功创建和销毁资源组(当然,分别使用 'terraform apply' 和 'terraform destroy')。我不是 Azure 专家,但我怀疑如果 Azure 已经看到存在网络观察者,那么当你 terraform 创建虚拟机时它不会创建额外的,因为只要 terraform 创建它就已经存在在 Azure 有机会之前先获取资源。

      【讨论】:

        猜你喜欢
        • 2015-04-24
        • 2016-08-09
        • 1970-01-01
        • 2015-08-16
        • 1970-01-01
        • 2022-01-14
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        相关资源
        最近更新 更多