このステップでは、terraform state mv を使用してステートファイル内のリソース名を変更する方法を学習します。これは、Terraform コードをリファクタリングする際によくある作業です。.tf ファイル内のリソースブロックの名前を変更すると、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 が実行しようとしていることを確認するために terraform plan を実行します。
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.