Terraform
销毁基础设施
你现在已经使用 Terraform 创建和更新了 Azure 资源。在本教程中,你将使用 Terraform 销毁你的基础设施。
当你不再需要基础设施时,你可能需要销毁它以降低安全风险和成本。例如,你可能需要将生产环境从服务中移除或管理构建或测试系统等短寿命环境。除了构建和修改基础设施之外,Terraform 还可以销毁或重新创建它所管理的基础设施。
销毁
terraform destroy 命令终止由你的 Terraform 项目管理资源。该命令是 terraform apply 的反向操作,它终止 Terraform 状态中指定的所有资源。它不会销毁在其他地方运行且不由当前 Terraform 项目管理的资源。
销毁你创建的资源。当 Terraform 提示你时,输入 yes 来执行此计划并销毁基础设施。
$ terraform destroy
azurerm_resource_group.rg: Refreshing state... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup]
azurerm_virtual_network.vnet: Refreshing state... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup/providers/Microsoft.Network/virtualNetworks/myTFVnet]
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
# azurerm_resource_group.rg will be destroyed
- resource "azurerm_resource_group" "rg" {
- id = "/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup" -> null
- location = "westus2" -> null
- name = "myTFResourceGroup" -> null
- tags = {
- "Environment" = "Terraform Getting Started"
- "Team" = "DevOps"
} -> null
}
# azurerm_virtual_network.vnet will be destroyed
- resource "azurerm_virtual_network" "vnet" {
- address_space = [
- "10.0.0.0/16",
] -> null
- dns_servers = [] -> null
- guid = "1a4efd63-61e5-48bb-ad9d-9ceadd403ecb" -> null
- id = "/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup/providers/Microsoft.Network/virtualNetworks/myTFVnet" -> null
- location = "westus2" -> null
- name = "myTFVnet" -> null
- resource_group_name = "myTFResourceGroup" -> null
- subnet = [] -> null
- tags = {} -> null
- vm_protection_enabled = false -> null
}
Plan: 0 to add, 0 to change, 2 to destroy.
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
azurerm_virtual_network.vnet: Destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup/providers/Microsoft.Network/virtualNetworks/myTFVnet]
azurerm_virtual_network.vnet: Still destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-...osoft.Network/virtualNetworks/myTFVnet, 10s elapsed]
azurerm_virtual_network.vnet: Destruction complete after 11s
azurerm_resource_group.rg: Destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup]
azurerm_resource_group.rg: Still destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-...29/resourceGroups/myTFResourceGroup, 10s elapsed]
azurerm_resource_group.rg: Still destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-...29/resourceGroups/myTFResourceGroup, 20s elapsed]
azurerm_resource_group.rg: Still destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-...29/resourceGroups/myTFResourceGroup, 30s elapsed]
azurerm_resource_group.rg: Still destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-...29/resourceGroups/myTFResourceGroup, 40s elapsed]
azurerm_resource_group.rg: Destruction complete after 47s
Destroy complete! Resources: 2 destroyed.
输出中的 - 前缀表示该实例将被销毁。与 apply 一样,Terraform 会显示其执行计划并在进行任何更改之前等待批准。
就像 apply 一样,Terraform 确定了必须销毁的顺序。在涉及多个资源的更复杂情况下,Terraform 会按照适当的顺序销毁它们以尊重依赖关系。