【问题标题】:terraform destroy arm template resource, but resource still there?terraform 销毁 arm 模板资源,但资源还在吗?
【发布时间】:2021-02-15 23:15:59
【问题描述】:

我正在尝试使用 terraform 为 Azure 部署预算警报,遗憾的是 terraform 中没有用于此的本机资源,因此我将 azurerm_subscription_template_deployment 与 ARM 模板一起使用。

我注意到一个奇怪的事情是部署后,一切似乎都很好,我可以通过 terraform state show 看到资源(在 ARM 模板中)。

但是,当我销毁时,它似乎并没有删除预算警报,它仍然存在,如果我执行 terraform state list,它显示所有内容都被删除而无影无踪,但实际资源仍然存在。

我注意到 terraform 对 azurerm_resource_group_template_deployment 有以下内容,我认为这也适用于订阅部署

“此资源在被删除时将自动尝试删除由 ARM 模板部署的资源。您可以通过将 features 块的 template_deployment 块中的 delete_nested_items_during_deletion 字段设置为 false 来选择退出。”

所以如果我理解正确的话,如果我的功能块默认情况下,它应该删除实际资源,当做销毁时。

为什么它的行为不正确?

代码如下

resource "azurerm_subscription_template_deployment" "budgetalert" {
  name     = "mcs_budget_alert"
  location = var.location

  template_content = <<-TEMPLATE
 {
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "client_initial": {
          "type": "string",
          "metadata": {
            "description": "Name of the Budget. It should be unique within a resource group."
          }
        },
        "amount": {
          "type": "string",
          "defaultValue": "1000",
          "metadata": {
            "description": "The total amount of cost or usage to track with the budget"
          }
        },
        "timeGrain": {
          "type": "string",
          "defaultValue": "Monthly",
          "allowedValues": [
            "Monthly",
            "Quarterly",
            "Annually"
          ],
          "metadata": {
            "description": "The time covered by a budget. Tracking of the amount will be reset based on the time grain."
          }
        },
        "startDate": {
          "type": "string",
          "metadata": {
            "description": "The start date must be first of the month in YYYY-MM-DD format. Future start date should not be more than three months. Past start date should be selected within the timegrain preiod."
          }
        },
        "endDate": {
          "type": "string",
          "metadata": {
            "description": "The end date for the budget in YYYY-MM-DD format. If not provided, we default this to 10 years from the start date."
          }
        },
        "operator": {
          "type": "string",
          "defaultValue": "GreaterThan",
          "allowedValues": [
            "EqualTo",
            "GreaterThan",
            "GreaterThanOrEqualTo"
          ],
          "metadata": {
            "description": "The comparison operator."
          }
        },
        "contactGroups": {
          "type": "string",
          "metadata": {
            "description": "The list of action groups to send the budget notification to when the threshold is exceeded. It accepts array of strings."
        }
        },
        "threshold": {
          "type": "string",
          "defaultValue": "90",
          "metadata": {
            "description": "Threshold value associated with a notification. Notification is sent when the cost exceeded the threshold. It is always percent and has to be between 0 and 1000."
          }
        }
    },
      "variables":{
        "resourcegroupID": "[concat(subscription().id, '/resourceGroups/', 'ccl_mcs_maintain')]",
        "actionGroupName": "budget Action Group"
    },
      "resources": [
        {
          "type": "Microsoft.Consumption/budgets",
          "apiVersion": "2019-10-01",
          "name": "[concat(parameters('client_initial'), '-MCS-BudgetAlert')]",
          "properties": {
            "category": "Cost",
            "amount": "[parameters('amount')]",
            "timeGrain": "[parameters('timeGrain')]",
            "timePeriod": {
              "startDate": "[parameters('startDate')]",
              "endDate": "[parameters('endDate')]"
            },
            "notifications": {
              "First-Notification": {
                "enabled": true,
                "operator": "[parameters('operator')]",
                "threshold": "[parameters('threshold')]",
                "contactGroups": "[array(parameters('contactGroups'))]"
              
               }
            }
          },
          "dependsOn": []
        }
      ]
 }
  TEMPLATE

  // NOTE: whilst we show an inline template here, we recommend
  // sourcing this from a file for readability/editor support
  #parameters_content = file("./modules/budget_alert/parameters.json")
  parameters_content = <<-PARAMETER
  {
    "client_initial": {
      "value": ${jsonencode(var.client_initial)}
    },
    "amount": {
      "value": ${jsonencode(var.amount)}
    },
    "timeGrain": {
      "value": ${jsonencode(var.timeGrain)}
    },
    "startDate": {
      "value": ${jsonencode(var.startDate)}
    },
    "endDate": {
      "value": ${jsonencode(var.endDate)}
    },
    "operator": {
      "value": ${jsonencode(var.operator)}
    },
    "threshold": {
      "value": ${jsonencode(var.threshold)}
    },
    "contactGroups": {
     "value": ${jsonencode(var.contactGroups)}
    }
  }
  PARAMETER


}

【问题讨论】:

    标签: terraform terraform-provider-azure


    【解决方案1】:

    您的理解是正确的,但很遗憾,delete_nested_items_during_deletion 字段对资源azurerm_subscription_template_deployment 不起作用。当您运行terraform destroy 时,它会删除.tfstate 文件中的ARM 模板分区。 document中也没有提及。

    经过我的验证,该字段仅在 azurerm_resource_group_template_deployment 上可用。您可能有功能请求like this

    【讨论】:

    • 谢谢。那么当我为 azurerm_subscription_template_deployment 运行 destroy 时它会产生什么影响?它是否只删除 terraform 中的模板,而不删除实际资源?
    • 是的,Terraform 无法在销毁期间删除这些资源,因为 Terraform 不知道 Azure 使用部署模板创建的各个资源。销毁模板部署会删除关联的部署操作,但不会删除部署创建的 Azure 资源。参考here
    • 您还有什么问题吗?
    • 嗨,南希,非常感谢。尽管让我感到困惑的是您提供的关于 azurerm_template_deployment 的说明,但我没有看到关于 azurerm_subscription_template_deployment 的相同说明。而且很奇怪azurerm_resource_group_template_deployment部署的资源在运行destroy后可以删除,但是azurerm_subscription_template_deployment的情况就不一样了……
    • 这是常见的工作机械。 Terraform 确实不知道 Azure 使用部署模板创建的各个资源。此外,真正的知识来自实践。我们都面临同样的结果。我认为delete_nested_items_during_deletion 字段将来可能会添加。但是,通常情况下,有一种解决方法。你可以展示你的完整代码,我也可以验证它。
    猜你喜欢
    • 2022-07-13
    • 2020-12-27
    • 2020-10-24
    • 1970-01-01
    • 2014-09-05
    • 2019-11-14
    • 2019-10-25
    • 2021-06-01
    • 1970-01-01
    相关资源
    最近更新 更多