In diesem Schritt lernen Sie, wie Sie eine Ressource in der Zustandsdatei mithilfe von terraform state mv umbenennen. Dies ist eine häufige Aufgabe beim Refactoring Ihres Terraform-Codes. Wenn Sie einen Ressourcenblock in Ihrer .tf-Datei umbenennen, geht Terraform davon aus, dass Sie die alte Ressource zerstören und eine neue erstellen möchten. Der Befehl state mv ermöglicht es Ihnen, die Zustandsdatei an die Änderungen in Ihrem Code anzupassen und so unnötige Zerstörung und Neuerstellung zu verhindern.
Zuerst benennen wir die Ressource in unserer Konfigurationsdatei um. Öffnen Sie main.tf mit dem Editor nano:
nano main.tf
Ändern Sie den Ressourcennamen von example zu example_renamed. Die Datei sollte wie folgt aussehen:
resource "local_file" "example_renamed" {
content = "This is an example file managed by Terraform."
filename = "${path.module}/example.txt"
}
Drücken Sie Ctrl+X, dann Y und Enter, um zu speichern und zu beenden.
Führen Sie nun terraform plan aus, um zu sehen, was Terraform tun möchte:
terraform plan
Der Plan zeigt, dass Terraform local_file.example zerstören und local_file.example_renamed erstellen möchte, was nicht das ist, was wir wollen.
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.
Um dies zu beheben, teilen wir Terraform mit, dass wir die Ressource lediglich verschoben haben. Führen Sie den Befehl terraform state mv mit der alten Adresse und der neuen Adresse aus:
terraform state mv local_file.example local_file.example_renamed
Sie erhalten eine Bestätigungsnachricht:
Move "local_file.example" to "local_file.example_renamed"
Successfully moved 1 object(s).
Führen Sie nun erneut terraform plan aus:
terraform plan
Dieses Mal meldet Terraform, dass keine Änderungen erforderlich sind, da der Zustand nun mit der Konfiguration übereinstimmt.
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.