terraform破坏了手臂模板资源,但是资源仍然存在吗?

时间:2020-11-03 09:31:49

标签: terraform terraform-provider-azure

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

我注意到一个奇怪的事情是,在部署之后,一切似乎都很好,我可以通过进行terraform状态显示来查看资源(在ARM模板中)。

但是,当我执行销毁操作时,它似乎并没有删除预算警报,它仍然存在;如果执行地形状态列表,它会显示所有内容都被删除,没有任何痕迹,但是实际资源仍然存在。 / p>

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

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

因此,如果我正确理解这一点,那么如果默认情况下我的功能块处于禁用状态,则在执行销毁操作时应删除实际资源。

为什么行为不正确?

代码如下

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


}

1 个答案:

答案 0 :(得分:0)

您的理解是正确的,但不幸的是,delete_nested_items_during_deletion字段对资源azurerm_subscription_template_deployment无效。运行.tfstate时,它将删除terraform destroy文件中的ARM模板分区。 document中也没有提及。

该字段仅在我验证后在azurerm_resource_group_template_deployment上可用。您可能有一个功能请求like this