이 단계에서는 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 plan을 실행하여 Terraform 이 무엇을 할 것인지 확인합니다.
terraform plan
계획 (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.