はじめに
terraformのstatelockにDynamoDBを使っていて、何かの拍子に意図せずロックされたままの状態になってしまったときのロック解除方法の例を残しておきます。
$ 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 を実行しても上記Errorで実行できません。
他で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が実行できるようになりました。
参考ドキュメント