在这一步中,你将学习如何使用 terraform state mv 来重命名状态文件(state file)中的资源。这是重构(refactoring)Terraform 代码时常见的操作。如果你重命名 .tf 文件中的资源块(resource block),Terraform 会认为你想销毁旧资源并创建一个新资源。state mv 命令允许你更新状态文件以匹配代码更改,从而避免不必要的销毁和创建。
首先,让我们在配置文件中重命名该资源。使用 nano 编辑器打开 main.tf:
nano main.tf
将资源名称从 example 更改为 example_renamed。文件应如下所示:
resource "local_file" "example_renamed" {
content = "This is an example file managed by Terraform."
filename = "${path.module}/example.txt"
}
按 Ctrl+X,然后按 Y,再按 Enter 保存并退出。
现在,运行 terraform plan 查看 Terraform 打算执行的操作:
terraform plan
计划将显示 Terraform 想要销毁 local_file.example 并创建 local_file.example_renamed,这不是我们期望的结果。
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:
+ create
- destroy
Terraform will perform the following actions:
## local_file.example will be destroyed
## (because local_file.example is not in configuration)
- resource "local_file" "example" {
- content = "This is an example file managed by Terraform." -> null
- content_base64sha256 = "F5EYZhFNzSXdE4CUftwQoDVqvdiufZpVyiLMqyZVOcQ=" -> null
- content_base64sha512 = "m1hvaxMuc/EhaKintyI54NSTTJ5yXpqHPCBNoHubF0rvF3JAj36lMj20aPcv21+3/OK+SqkiTlnT/LdvLCwqDA==" -> null
- content_md5 = "a43bdd236c2f0f4d87452ba2ef0867e4" -> null
- content_sha1 = "ec3adcab998872def2df6200fb03992ac6f237a4" -> null
- content_sha256 = "17911866114dcd25dd1380947edc10a0356abdd8ae7d9a55ca22ccab265539c4" -> null
- content_sha512 = "9b586f6b132e73f12168a8a7b72239e0d4934c9e725e9a873c204da07b9b174aef1772408f7ea5323db468f72fdb5fb7fce2be4aa9224e59d3fcb76f2c2c2a0c" -> null
- directory_permission = "0777" -> null
- file_permission = "0777" -> null
- filename = "./example.txt" -> null
- id = "ec3adcab998872def2df6200fb03992ac6f237a4" -> null
}
## local_file.example_renamed will be created
+ resource "local_file" "example_renamed" {
+ content = "This is an example file managed by Terraform."
+ content_base64sha256 = (known after apply)
+ content_base64sha512 = (known after apply)
+ content_md5 = (known after apply)
+ content_sha1 = (known after apply)
+ content_sha256 = (known after apply)
+ content_sha512 = (known after apply)
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "./example.txt"
+ id = (known after apply)
}
Plan: 1 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 state mv 命令,提供旧地址和新地址:
terraform state mv local_file.example local_file.example_renamed
你将看到一条确认消息:
Move "local_file.example" to "local_file.example_renamed"
Successfully moved 1 object(s).
现在,再次运行 terraform plan:
terraform plan
这次,Terraform 报告不需要更改,因为状态现在与配置匹配。
local_file.example_renamed: Refreshing state... [id=ec3adcab998872def2df6200fb03992ac6f237a4]
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.