解锁terraform state lock的方法

首先

我会在这里留下一个使用DynamoDB进行terraform的statelock,以防意外情况下被无意间锁定的解锁方法的例子。

$ terraform plan

Error: Error locking state: Error acquiring the state lock: ConditionalCheckFailedException: The conditional request failed
        status code: 400, request id: HUFMTA4D7PN40H4RVR3PBC2FJVVV4KQNSxxxxxxxxxxx
Lock Info:
  ID:        xxxxx-xxxxx-xxxxx-xxxxx-xxxxx
  Path:      tomy-stg-terraform-state/_/tomy-stg/terraform.tfstate
  Operation: OperationTypePlan
  Who:       tomy103@xxx.local
  Version:   0.11.10
  Created:   2018-12-20 23:46:18.414418 +0000 UTC
  Info:


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 apply / plan,均无法通过上述错误进行执行。

既知此锁定由于未执行过 `terraform apply / plan` 而需手动解除。

$ terraform force-unlock xxxxx-xxxxx-xxxxx-xxxxx-xxxxx

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.

现在可以使用Terraform apply / plan来执行了。

参考文件

广告
将在 10 秒后关闭
bannerAds