Terraform 解除 State Lock 的方法

2019-05-07 Terraform

這篇是記錄 Terraform 使用的小筆記,為了避免同時執行 Terraform 造成 resource 混亂,常常會利用 State Locking 的方法來做,那麼就會常遇到跟我一樣的狀況。

State Locking 會幫你在每一次執行 Terraform 的時候先寫入一筆 Lock 資料,以我為例 State backend 用的是 S3,搭配 DynamoDB 來做 Lock Table。

在一開始開發的時候一定會一直 try plan and apply,當你想要中途暫停的時候 (Ctrl + C/Z) 此時就會視為不正常結束,而 Lock 就不會被釋放,當你下次要執行的時候就會出現這樣的訊息:

Acquiring state lock. This may take a few moments…
Error: Error locking state: Error acquiring the state lock: ConditionalCheckFailedException: The conditional request failed
status code: 400, request id: JP7ENVVNM9TFJCJ1G0TCA5RFR7VV4KQNSO5AEMVJF66Q9ASUAAJG
Lock Info:
ID: 78db2f84-3ae5-cf86-78dd-036f3e7f6071
Path: terraform.tfstate
Operation: OperationTypePlan
Who: shazi.info
Version: 0.11.11
Created: 2019-05-06 20:13:59.167855 +0000 UTC

Terraform acquires a state lock to protect the state from being written
by multiple users at the same time. Please resolve the issue above and try
again. For most commands, you can disable locking with the “-lock=false”
flag, but this is not recommended.

因為上次暫停的 Terraform Lock 還存在,所以 Terraform 認為還有工作沒跑完,要解除 Lock 必須手動執行 force-unlock 來解鎖。

以上面的訊息為例,Lock ID 為 78db2f84-3ae5-cf86-78dd-036f3e7f6071,執行以下:

$ terraform force-unlock 78db2f84-3ae5-cf86-78dd-036f3e7f6071

就能解除 Lock 執行下一次的 Task。

Do you really want to force-unlock?
Terraform will remove the lock on the remote state.
This will allow local Terraform commands to modify this state, even though it
may be still be in use. Only ‘yes’ will be accepted to confirm.
Enter a value: yes
Terraform state has been successfully unlocked!
The state has been unlocked, and Terraform commands should now be able to
obtain a new lock on the remote state.

給 Mr. 沙先生一點建議



展開全部 | 收合全部


訂閱 Mr. 沙先生 的文章

輸入你的 email 用於訂閱

%d 位部落客按了讚: