介绍
欢迎来到关于 Terraform 资源销毁的这个实验。基础设施生命周期的一个关键部分不仅是创建和更新资源,还在不再需要它们时干净地移除它们。这个过程被称为销毁(destruction)。
Terraform 为销毁基础设施提供了一个直接且安全的工作流程。主要的命令是 terraform destroy。为了防止意外删除,Terraform 还提供了一种在执行操作前预览将要销毁内容的方法。
在这个实验中,你将学习如何:
- 使用
terraform plan -destroy来预览销毁计划。 - 解析销毁计划的输出。
- 执行
terraform destroy命令来移除资源。 - 验证资源是否已从系统和 Terraform 状态文件(state file)中成功移除。
我们将使用一个简单的 local_file 资源,这使我们能够专注于 Terraform 工作流程,而无需云服务提供商(cloud provider)的凭证。
确认当前项目状态
在开始销毁过程之前,我们先确认一下 Terraform 项目的当前状态。这将帮助你了解当前有哪些资源正被 Terraform 管理。
运行以下命令来检查当前的项目状态:
首先,我们列出项目目录中的文件:
ls -la
你应该会看到类似以下的输出:
total 20
drwxr-xr-x 1 labex labex 110 Oct 15 13:10 .
drwxr-x--- 1 labex labex 4096 Oct 15 13:10 ..
drwxr-xr-x 3 labex labex 23 Oct 15 13:07 .terraform
-rw-r--r-- 1 labex labex 1181 Oct 15 13:07 .terraform.lock.hcl
-rwxrwxr-x 1 labex labex 45 Oct 15 13:10 example.txt
-rw-rw-r-- 1 labex labex 258 Oct 15 13:10 main.tf
-rw-rw-r-- 1 labex labex 1088 Oct 15 13:10 terraform.tfstate
这显示了设置脚本创建的 main.tf 文件和 terraform.tfstate 文件,以及 Terraform 配置应用时创建的 example.txt 文件。
接下来,我们检查当前的 Terraform 状态:
terraform show
你应该会看到类似以下的输出:
## local_file.example:
resource "local_file" "example" {
content = "This is an example file managed by Terraform."
directory_permission = "0777"
file_permission = "0777"
filename = "./example.txt"
id = "ec3adcab998872def2df6200fb03992ac6f237a4"
}
这显示了当前被 Terraform 管理的 local_file.example 资源的信息。
运行 terraform plan -destroy 进行预览
在这一步,你将学习如何生成一个推测性的销毁计划。在销毁任何资源之前,最佳实践是预览 Terraform 确切打算做什么。这可以防止意外删除关键基础设施。terraform plan -destroy 命令会创建一个执行计划,显示哪些资源将被销毁,而不会实际执行销毁操作。
你所有的工作都将在 ~/project 目录下完成。设置脚本已经创建了 main.tf 文件并应用了它,从而创建了一个名为 example.txt 的文件。
现在,在你的终端中运行以下命令来查看销毁计划:
terraform plan -destroy
你将看到类似以下的输出。此输出详细说明了销毁我们配置所管理的一个资源的计划。
local_file.example: Refreshing state... [id=ec3adcab998872def2df6200fb03992ac6f237a4]
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:
## local_file.example will be destroyed
- resource "local_file" "example" {
- content = "This is an example file managed by Terraform." -> null
- directory_permission = "0777" -> null
- file_permission = "0777" -> null
- filename = "./example.txt" -> null
- id = "ec3adcab998872def2df6200fb03992ac6f237a4" -> null
}
Plan: 0 to add, 0 to change, 1 to destroy.
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't
guarantee to take exactly these actions if you run "terraform apply" now.
确认销毁计划输出
在这一步,我们将分析上一个命令的输出。理解这个计划对于安全地使用 Terraform 至关重要。
请查看 terraform plan -destroy 的输出。注意以下关键要素:
**资源操作 (Resource Action)**:
## local_file.example will be destroyed这行清楚地说明了预期的操作。在resource "local_file" "example"前面的-符号是 Terraform 表示销毁的标记。任何以-开头的行都表示将要移除某些内容。**属性变更 (Attribute Changes)**:输出显示了资源的所有属性,并指示它们将变更为
null,这表示删除。例如:- filename = "./example.txt" -> null。**计划摘要 (Plan Summary)**:最后一行
Plan: 0 to add, 0 to change, 1 to destroy.提供了整个计划的高层摘要。这是快速确认计划操作最重要的检查行。
通过审查此计划,你可以确信只有 local_file.example 资源会受到影响。在真实场景中,配置错误可能导致意外销毁重要资源,因此这个确认步骤至关重要。此步骤不需要运行任何命令;它专注于理解这个过程。
运行 terraform destroy 以移除资源
在这一步,你将执行销毁计划。现在你已经审查了计划并对变更感到放心,可以继续进行实际的销毁操作了。
terraform destroy 命令会首先展示相同的销毁计划以供最终审查,然后提示你确认后再继续。
在你的终端中运行以下命令:
terraform destroy
Terraform 将再次显示计划并请求你的批准。
local_file.example: Refreshing state... [id=ec3adcab998872def2df6200fb03992ac6f237a4]
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:
## local_file.example will be destroyed
- resource "local_file" "example" {
- content = "This is an example file managed by Terraform." -> null
- directory_permission = "0777" -> null
- file_permission = "0777" -> null
- filename = "./example.txt" -> null
- id = "ec3adcab998872def2df6200fb03992ac6f237a4" -> null
}
Plan: 0 to add, 0 to change, 1 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 并按 Enter 键。
yes
在你确认后,Terraform 将继续销毁资源并输出进度。
local_file.example: Destroying... [id=ec3adcab998872def2df6200fb03992ac6f237a4]
local_file.example: Destruction complete after 0s
Destroy complete! Resources: 1 destroyed.
这确认了 local_file 资源已被成功销毁。
验证文件已从文件系统中移除
在这一步,你将验证资源销毁是否对系统产生了预期的效果。因为我们的资源是一个名为 example.txt 的本地文件,销毁该资源应该已经将此文件从文件系统中删除了。
你可以通过尝试使用 ls 命令列出该文件来验证这一点。
在你的终端中运行以下命令:
ls example.txt
由于文件已被删除,该命令会失败,你将看到操作系统返回的错误信息。这个错误是预期的结果,并确认资源已成功销毁。
ls: cannot access 'example.txt': No such file or directory
这个验证步骤很重要,因为它确认了 Terraform 在状态文件(state file)中的操作已反映在现实世界中。
检查空的 terraform.tfstate 文件
在这一步,你将检查 Terraform 状态文件(terraform.tfstate),看看它是如何反映资源销毁的。状态文件是一个 JSON 文件,用于跟踪 Terraform 所管理的资源及其当前状态。
当配置中的所有资源都被销毁后,状态文件不会被删除。相反,它会被更新以反映它不再管理任何资源。状态文件中的 resources 数组将变为空。
让我们检查一下状态文件的内容。使用 cat 命令来显示它:
cat terraform.tfstate
输出将是一个 JSON 对象。请注意,resources 键现在指向一个空数组 []。
{
"version": 4,
"terraform_version": "1.13.3",
"serial": 3,
"lineage": "f25aaab8-c186-2b16-1bae-fe9ba25f81e4",
"outputs": {},
"resources": [],
"check_results": null
}
这从 Terraform 的角度确认了对于此配置,它不再管理任何资源了。状态现在是干净的。
总结
恭喜你完成了本次实验!你已成功了解了销毁 Terraform 所管理的基础设施的整个过程。
在本次实验中,你学习了:
- 使用
terraform plan -destroy预览变更的重要性,以防止意外删除。 - 如何阅读和理解销毁计划,重点关注摘要和资源级别的变更。
- 如何使用
terraform destroy命令执行资源销毁,以及所需的确认步骤。 - 如何通过检查真实世界系统和 Terraform 状态文件(
terraform.tfstate)来验证资源是否已成功销毁。
掌握销毁工作流程与掌握资源创建同等重要。它确保你能干净、安全地管理基础设施的完整生命周期。



