terraform销毁arm模板资源,但资源仍然存在?
我正在尝试使用terraform为Azure部署预算警报,不幸的是,terraform中没有用于此目的的本机资源,因此我将使用azurerm_订阅_模板_部署以及ARM模板 我注意到一件奇怪的事情是在部署之后,一切似乎都很好,我可以通过terraform state show查看资源(在ARM模板中) 然而,当我销毁时,它似乎并没有删除预算警报,它仍然在那里,如果我执行terraform state list,它显示所有内容都被删除了,没有任何痕迹,但实际的资源仍然在那里 我注意到terraform在azurerm_资源_组_模板_部署中具有以下功能,我假设这也适用于子选项部署 “删除ARM模板时,此资源将自动尝试删除ARM模板部署的资源。您可以通过将功能块的模板部署块内的删除嵌套项目删除期间字段设置为false来选择不删除此资源。” 因此,如果我正确理解这一点,如果默认情况下我的功能块是,那么在执行销毁时,它应该删除实际的资源 为什么它的行为不正确 代码如下terraform销毁arm模板资源,但资源仍然存在?,terraform,terraform-provider-azure,Terraform,Terraform Provider Azure,我正在尝试使用terraform为Azure部署预算警报,不幸的是,terraform中没有用于此目的的本机资源,因此我将使用azurerm_订阅_模板_部署以及ARM模板 我注意到一件奇怪的事情是在部署之后,一切似乎都很好,我可以通过terraform state show查看资源(在ARM模板中) 然而,当我销毁时,它似乎并没有删除预算警报,它仍然在那里,如果我执行terraform state list,它显示所有内容都被删除了,没有任何痕迹,但实际的资源仍然在那里 我注意到terrafo
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
}
resource“azurerm\u订阅\u模板\u部署”“预算专家”{
name=“mcs\U预算\U警报”
位置=变量位置
template\u content=您的理解是正确的,但不幸的是,delete\u nested\u items\u during\u deletation
字段对资源azurerm\u订阅\u template\u部署
无效。当您运行terraform destroy
时,它会删除.tfstate
文件中的ARM模板分区本文件中未提及oes
在我验证后,该字段仅在azurerm\u资源组模板部署上可用。您可能有一个功能请求。谢谢。那么,当我运行destroy for azurerm\u订阅模板部署时,它会产生什么影响?它是否只删除terraform中的模板,而不删除实际资源?是的,Terraform无法在销毁期间删除这些资源,因为Terraform不知道Azure使用部署模板创建的单个资源。销毁模板部署将删除关联的部署操作,但不会删除部署创建的Azure资源。请参阅,您还有任何问题吗?您好Nancy,非常感谢。虽然让我困惑的是您提供的说明是针对azurerm_模板_部署的,但我没有看到关于azurerm_订阅_模板_部署的说明。奇怪的是,azurerm_资源组_模板_部署部署的资源可以在运行销毁后删除,但情况不同对于azurerm_订阅_模板_部署…这是常见的工作机制。Terraform确实不知道Azure使用部署模板创建的单个资源。而且,真正的知识来自实践。我们都面临相同的结果。我认为删除期间的删除嵌套项目字段可能会添加到然而,通常情况下,每个案例都有一个解决方案。你可以展示你的完整代码,我也可以验证它。